1 ;;; nnkiboze.el --- select virtual news access for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003
4 ;; Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; The other access methods (nntp, nnspool, etc) are general news
29 ;; access methods. This module relies on Gnus and can't be used
34 (eval-when-compile (require 'cl))
35 (eval-when-compile (require 'gnus-clfns))
43 (nnoo-declare nnkiboze)
44 (defvoo nnkiboze-directory (nnheader-concat gnus-directory "kiboze/")
45 "nnkiboze will put its files in this directory.")
47 (defvoo nnkiboze-level 9
48 "The maximum level to be searched for articles.")
50 (defvoo nnkiboze-remove-read-articles t
51 "If non-nil, nnkiboze will remove read articles from the kiboze group.")
53 (defvoo nnkiboze-ephemeral nil
54 "If non-nil, don't store any data anywhere.")
56 (defvoo nnkiboze-scores nil
57 "Score rules for generating the nnkiboze group.")
59 (defvoo nnkiboze-regexp nil
60 "Regexp for matching component groups.")
62 (defvoo nnkiboze-file-coding-system nnheader-text-coding-system
63 "Coding system for nnkiboze files.")
67 (defconst nnkiboze-version "nnkiboze 1.0")
69 (defvoo nnkiboze-current-group nil)
70 (defvoo nnkiboze-status-string "")
72 (defvoo nnkiboze-headers nil)
76 ;;; Interface functions.
78 (nnoo-define-basics nnkiboze)
80 (deffoo nnkiboze-retrieve-headers (articles &optional group server fetch-old)
81 (nnkiboze-possibly-change-group group)
82 (unless gnus-nov-is-evil
83 (if (stringp (car articles))
85 (let ((nov (nnkiboze-nov-file-name)))
86 (when (file-exists-p nov)
88 (set-buffer nntp-server-buffer)
90 (let ((nnheader-file-coding-system nnkiboze-file-coding-system))
91 (nnheader-insert-file-contents nov))
92 (nnheader-nov-delete-outside-range
93 (car articles) (car (last articles)))
96 (deffoo nnkiboze-request-article (article &optional newsgroup server buffer)
97 (nnkiboze-possibly-change-group newsgroup)
98 (if (not (numberp article))
99 ;; This is a real kludge. It might not work at times, but it
100 ;; does no harm I think. The only alternative is to offer no
101 ;; article fetching by message-id at all.
102 (nntp-request-article article newsgroup gnus-nntp-server buffer)
103 (let* ((header (gnus-summary-article-header article))
104 (xref (mail-header-xref header))
107 (error "nnkiboze: No xref"))
108 (unless (string-match " \\([^ ]+\\):\\([0-9]+\\)" xref)
109 (error "nnkiboze: Malformed xref"))
110 (setq num (string-to-int (match-string 2 xref))
111 group (match-string 1 xref))
112 (or (with-current-buffer buffer
113 (or (and gnus-use-cache (gnus-cache-request-article num group))
114 (gnus-agent-request-article num group)))
115 (gnus-request-article num group buffer)))))
117 (deffoo nnkiboze-request-scan (&optional group server)
118 (nnkiboze-possibly-change-group group)
119 (nnkiboze-generate-group (concat "nnkiboze:" group)))
121 (deffoo nnkiboze-request-group (group &optional server dont-check)
122 "Make GROUP the current newsgroup."
123 (nnkiboze-possibly-change-group group)
126 (let ((nov-file (nnkiboze-nov-file-name))
129 (set-buffer nntp-server-buffer)
131 (unless (file-exists-p nov-file)
132 (nnkiboze-request-scan group))
133 (if (not (file-exists-p nov-file))
134 (nnheader-report 'nnkiboze "Can't select group %s" group)
135 (let ((nnheader-file-coding-system nnkiboze-file-coding-system))
136 (nnheader-insert-file-contents nov-file))
137 (if (zerop (buffer-size))
138 (nnheader-insert "211 0 0 0 %s\n" group)
139 (goto-char (point-min))
140 (when (looking-at "[0-9]+")
141 (setq beg (read (current-buffer))))
142 (goto-char (point-max))
143 (when (re-search-backward "^[0-9]" nil t)
144 (setq end (read (current-buffer))))
145 (setq total (count-lines (point-min) (point-max)))
146 (nnheader-insert "211 %d %d %d %s\n" total beg end group)))))))
148 (deffoo nnkiboze-close-group (group &optional server)
149 (nnkiboze-possibly-change-group group)
150 ;; Remove NOV lines of articles that are marked as read.
151 (when (and (file-exists-p (nnkiboze-nov-file-name))
152 nnkiboze-remove-read-articles)
153 (let ((coding-system-for-write nnkiboze-file-coding-system)
154 (output-coding-system nnkiboze-file-coding-system))
155 (with-temp-file (nnkiboze-nov-file-name)
156 (let ((cur (current-buffer))
157 (nnheader-file-coding-system nnkiboze-file-coding-system))
158 (nnheader-insert-file-contents (nnkiboze-nov-file-name))
159 (goto-char (point-min))
161 (if (not (gnus-article-read-p (read cur)))
163 (gnus-delete-line))))))
164 (setq nnkiboze-current-group nil)))
166 (deffoo nnkiboze-open-server (server &optional defs)
167 (unless (assq 'nnkiboze-regexp defs)
168 (push `(nnkiboze-regexp ,server)
170 (nnoo-change-server 'nnkiboze server defs))
172 (deffoo nnkiboze-request-delete-group (group &optional force server)
173 (nnkiboze-possibly-change-group group)
176 (nnkiboze-score-file group)
177 (list (nnkiboze-nov-file-name)
178 (nnkiboze-nov-file-name ".newsrc")))))
180 (and (file-exists-p (car files))
181 (file-writable-p (car files))
182 (delete-file (car files)))
183 (setq files (cdr files)))))
184 (setq nnkiboze-current-group nil)
187 (nnoo-define-skeleton nnkiboze)
190 ;;; Internal functions.
192 (defun nnkiboze-possibly-change-group (group)
193 (setq nnkiboze-current-group group))
195 (defun nnkiboze-prefixed-name (group)
196 (gnus-group-prefixed-name group '(nnkiboze "")))
199 (defun nnkiboze-generate-groups ()
200 "\"Usage: emacs -batch -l nnkiboze -f nnkiboze-generate-groups\".
201 Finds out what articles are to be part of the nnkiboze groups."
203 (let ((nnmail-spool-file nil)
205 (gnus-use-dribble-file nil)
206 (gnus-read-active-file t)
207 (gnus-expert-user t))
209 (let* ((gnus-newsrc-alist (gnus-copy-sequence gnus-newsrc-alist))
210 (newsrc (cdr gnus-newsrc-alist))
211 gnus-newsrc-hashtb info)
212 (gnus-make-hashtable-from-newsrc-alist)
213 ;; We have copied all the newsrc alist info over to local copies
214 ;; so that we can mess all we want with these lists.
215 (while (setq info (pop newsrc))
216 (when (string-match "nnkiboze" (gnus-info-group info))
217 ;; For each kiboze group, we call this function to generate
219 (nnkiboze-generate-group (gnus-info-group info) t))))
221 (set-buffer gnus-group-buffer)
222 (gnus-group-list-groups)))
224 (defun nnkiboze-score-file (group)
225 (list (expand-file-name
226 (concat (file-name-as-directory gnus-kill-files-directory)
227 (nnheader-translate-file-chars
228 (concat (nnkiboze-prefixed-name nnkiboze-current-group)
229 "." gnus-score-file-suffix))))))
231 (defun nnkiboze-generate-group (group &optional inhibit-list-groups)
232 (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
233 (newsrc-file (concat nnkiboze-directory
234 (nnheader-translate-file-chars
235 (concat group ".newsrc"))))
236 (nov-file (concat nnkiboze-directory
237 (nnheader-translate-file-chars
238 (concat group ".nov"))))
239 method nnkiboze-newsrc gname newsrc active
240 ginfo lowest glevel orig-info nov-buffer
241 ;; Bind various things to nil to make group entry faster.
243 (gnus-large-newsgroup nil)
244 (gnus-score-find-score-files-function 'nnkiboze-score-file)
245 ;; Use only nnkiboze-score-file!
246 (gnus-score-use-all-scores nil)
248 (gnus-verbose (min gnus-verbose 3))
249 gnus-select-group-hook gnus-summary-prepare-hook
250 gnus-thread-sort-functions gnus-show-threads
251 gnus-visual gnus-suppress-duplicates num-unread)
253 (error "No such group: %s" group))
254 ;; Load the kiboze newsrc file for this group.
256 (when (file-exists-p newsrc-file)
258 (let ((coding-system-for-write nnkiboze-file-coding-system)
259 (output-coding-system nnkiboze-file-coding-system))
260 (gnus-make-directory (file-name-directory nov-file))
261 (with-temp-file nov-file
262 (when (file-exists-p nov-file)
263 (nnheader-insert-file-contents nov-file))
264 (setq nov-buffer (current-buffer))
265 ;; Go through the active hashtb and add new all groups that match the
269 (and (string-match nnkiboze-regexp
270 (setq gname (symbol-name group))) ; Match
271 (not (assoc gname nnkiboze-newsrc)) ; It isn't registered
272 (numberp (car (symbol-value group))) ; It is active
273 (or (> nnkiboze-level 7)
275 (nth 1 (nth 2 (gnus-gethash
276 gname gnus-newsrc-hashtb))))
277 (>= nnkiboze-level glevel)))
278 (not (string-match "^nnkiboze:" gname)) ; Exclude kibozes
279 (push (cons gname (1- (car (symbol-value group))))
282 ;; `newsrc' is set to the list of groups that possibly are
283 ;; component groups to this kiboze group. This list has elements
284 ;; on the form `(GROUP . NUMBER)', where NUMBER is the highest
285 ;; number that has been kibozed in GROUP in this kiboze group.
286 (setq newsrc nnkiboze-newsrc)
288 (if (not (setq active (gnus-gethash
289 (caar newsrc) gnus-active-hashtb)))
290 ;; This group isn't active after all, so we remove it from
291 ;; the list of component groups.
292 (setq nnkiboze-newsrc (delq (car newsrc) nnkiboze-newsrc))
293 (setq lowest (cdar newsrc))
294 ;; Ok, we have a valid component group, so we jump to it.
295 (switch-to-buffer gnus-group-buffer)
296 (gnus-group-jump-to-group (caar newsrc))
297 (gnus-message 3 "nnkiboze: Checking %s..." (caar newsrc))
298 (setq ginfo (gnus-get-info (gnus-group-group-name))
299 orig-info (gnus-copy-sequence ginfo)
300 num-unread (car (gnus-gethash (caar newsrc)
301 gnus-newsrc-hashtb)))
304 ;; We set all list of article marks to nil. Since we operate
305 ;; on copies of the real lists, we can destroy anything we
308 (setcar (nthcdr 3 ginfo) nil))
309 ;; We set the list of read articles to be what we expect for
310 ;; this kiboze group -- either nil or `(1 . LOWEST)'.
312 (setcar (nthcdr 2 ginfo)
313 (and (not (= lowest 1)) (cons 1 lowest))))
314 (when (and (or (not ginfo)
315 (> (length (gnus-list-of-unread-articles
320 (gnus-group-select-group nil))
321 (eq major-mode 'gnus-summary-mode)))
322 ;; We are now in the group where we want to be.
323 (setq method (gnus-find-method-for-group
324 gnus-newsgroup-name))
325 (when (eq method gnus-select-method)
327 ;; We go through the list of scored articles.
328 (while gnus-newsgroup-scored
329 (when (> (caar gnus-newsgroup-scored) lowest)
330 ;; If it has a good score, then we enter this article
331 ;; into the kiboze group.
334 (gnus-summary-article-header
335 (caar gnus-newsgroup-scored))
336 gnus-newsgroup-name))
337 (setq gnus-newsgroup-scored (cdr gnus-newsgroup-scored)))
338 ;; That's it. We exit this group.
339 (when (eq major-mode 'gnus-summary-mode)
340 (kill-buffer (current-buffer)))))
341 ;; Restore the proper info.
343 (setcdr ginfo (cdr orig-info)))
344 (setcar (gnus-gethash (caar newsrc) gnus-newsrc-hashtb)
346 (setcdr (car newsrc) (cdr active))
347 (gnus-message 3 "nnkiboze: Checking %s...done" (caar newsrc))
348 (setq newsrc (cdr newsrc)))))
349 ;; We save the kiboze newsrc for this group.
350 (gnus-make-directory (file-name-directory newsrc-file))
351 (with-temp-file newsrc-file
352 (insert "(setq nnkiboze-newsrc '")
353 (gnus-prin1 nnkiboze-newsrc)
355 (unless inhibit-list-groups
357 (set-buffer gnus-group-buffer)
358 (gnus-group-list-groups)))
361 (defun nnkiboze-enter-nov (buffer header group)
364 (goto-char (point-max))
365 (let ((prefix (gnus-group-real-prefix group))
366 (oheader (copy-sequence header))
368 (if (zerop (forward-line -1))
370 (setq article (1+ (read (current-buffer))))
373 (mail-header-set-number oheader article)
375 (insert (or (mail-header-xref oheader) ""))
376 (goto-char (point-min))
377 (if (re-search-forward " [^ ]+:[0-9]+" nil t)
378 (goto-char (match-beginning 0))
379 (or (eobp) (forward-char 1)))
380 ;; The first Xref has to be the group this article
381 ;; really came for - this is the article nnkiboze
382 ;; will request when it is asked for the article.
383 (insert " " group ":"
384 (int-to-string (mail-header-number header)) " ")
385 (while (re-search-forward " [^ ]+:[0-9]+" nil t)
386 (goto-char (1+ (match-beginning 0)))
388 (mail-header-set-xref oheader (buffer-string)))
389 (nnheader-insert-nov oheader))))
391 (defun nnkiboze-nov-file-name (&optional suffix)
392 (concat (file-name-as-directory nnkiboze-directory)
393 (nnheader-translate-file-chars
394 (concat (nnkiboze-prefixed-name nnkiboze-current-group)
395 (or suffix ".nov")))))
399 ;;; nnkiboze.el ends here