1 ;;; nnkiboze.el --- select virtual news access for Gnus
2 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
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 not be used
36 (eval-when-compile (require 'cl))
38 (defvar nnkiboze-directory
39 (expand-file-name (or gnus-article-save-directory "~/News/"))
40 "nnkiboze will put its files in this directory.")
42 (defvar nnkiboze-level 9
43 "*The maximum level to be searched for articles.")
45 (defvar nnkiboze-remove-read-articles t
46 "*If non-nil, nnkiboze will remove read articles from the kiboze group.")
50 (defconst nnkiboze-version "nnkiboze 1.0"
51 "Version numbers of this version of nnkiboze.")
53 (defvar nnkiboze-current-group nil)
54 (defvar nnkiboze-current-score-group "")
55 (defvar nnkiboze-status-string "")
59 ;;; Interface functions.
61 (defun nnkiboze-retrieve-headers (articles &optional group server fetch-old)
62 (nnkiboze-possibly-change-newsgroups group)
65 (if (stringp (car articles))
67 (let ((first (car articles))
68 (last (progn (while (cdr articles) (setq articles (cdr articles)))
70 (nov (nnkiboze-nov-file-name)))
71 (if (file-exists-p nov)
73 (set-buffer nntp-server-buffer)
75 (insert-file-contents nov)
76 (goto-char (point-min))
77 (while (and (not (eobp)) (< first (read (current-buffer))))
80 (if (not (eobp)) (delete-region 1 (point)))
81 (while (and (not (eobp)) (>= last (read (current-buffer))))
84 (if (not (eobp)) (delete-region (point) (point-max)))
87 (defun nnkiboze-open-server (newsgroups &optional something)
88 "Open a virtual newsgroup that contains NEWSGROUPS."
89 (gnus-make-directory nnkiboze-directory)
90 (nnheader-init-server-buffer))
92 (defun nnkiboze-close-server (&rest dum)
96 (defalias 'nnkiboze-request-quit (symbol-function 'nnkiboze-close-server))
98 (defun nnkiboze-server-opened (&optional server)
99 "Return server process status, T or NIL.
100 If the stream is opened, return T, otherwise return NIL."
101 (and nntp-server-buffer
102 (get-buffer nntp-server-buffer)))
104 (defun nnkiboze-status-message (&optional server)
105 "Return server status response as string."
106 nnkiboze-status-string)
108 (defun nnkiboze-request-article (article &optional newsgroup server buffer)
109 "Select article by message number."
110 (nnkiboze-possibly-change-newsgroups newsgroup)
111 (if (not (numberp article))
112 ;; This is a real kludge. It might not work at times, but it
113 ;; does no harm I think. The only alternative is to offer no
114 ;; article fetching by message-id at all.
115 (nntp-request-article article newsgroup gnus-nntp-server buffer)
116 (let* ((header (gnus-summary-article-header article))
117 (xref (mail-header-xref header))
119 (or xref (error "nnkiboze: No xref"))
120 (or (string-match " \\([^ ]+\\):\\([0-9]+\\)" xref)
121 (error "nnkiboze: Malformed xref"))
122 (setq igroup (substring xref (match-beginning 1) (match-end 1)))
123 (setq iarticle (string-to-int
124 (substring xref (match-beginning 2) (match-end 2))))
125 (and (gnus-request-group igroup t)
126 (gnus-request-article iarticle igroup buffer)))))
128 (defun nnkiboze-request-group (group &optional server dont-check)
129 "Make GROUP the current newsgroup."
130 (nnkiboze-possibly-change-newsgroups group)
133 (let ((nov-file (nnkiboze-nov-file-name))
136 (set-buffer nntp-server-buffer)
138 (if (not (file-exists-p nov-file))
139 (insert (format "211 0 0 0 %s\n" group))
140 (insert-file-contents nov-file)
141 (if (zerop (buffer-size))
142 (insert (format "211 0 0 0 %s\n" group))
143 (goto-char (point-min))
144 (and (looking-at "[0-9]+") (setq beg (read (current-buffer))))
145 (goto-char (point-max))
146 (and (re-search-backward "^[0-9]" nil t)
147 (setq end (read (current-buffer))))
148 (setq total (count-lines (point-min) (point-max)))
150 (insert (format "211 %d %d %d %s\n" total beg end group)))))))
153 (defun nnkiboze-close-group (group &optional server)
154 (nnkiboze-possibly-change-newsgroups group)
155 ;; Remove NOV lines of articles that are marked as read.
156 (when (and (file-exists-p (nnkiboze-nov-file-name))
157 nnkiboze-remove-read-articles
158 (eq major-mode 'gnus-summary-mode))
160 (let ((unreads gnus-newsgroup-unreads)
161 (unselected gnus-newsgroup-unselected)
162 (version-control 'never))
163 (set-buffer (get-buffer-create "*nnkiboze work*"))
164 (buffer-disable-undo (current-buffer))
166 (let ((cur (current-buffer))
168 (insert-file-contents (nnkiboze-nov-file-name))
169 (goto-char (point-min))
170 (while (looking-at "[0-9]+")
171 (if (or (memq (setq article (read cur)) unreads)
172 (memq article unselected))
174 (delete-region (progn (beginning-of-line) (point))
175 (progn (forward-line 1) (point)))))
176 (write-file (nnkiboze-nov-file-name))
177 (kill-buffer (current-buffer)))))
178 (setq nnkiboze-current-group nil)))
180 (defun nnkiboze-request-list (&optional server)
181 (nnheader-report 'nnkiboze "LIST is not implemented."))
183 (defun nnkiboze-request-newgroups (date &optional server)
185 (nnheader-report 'nnkiboze "NEWGROUPS is not supported."))
187 (defun nnkiboze-request-list-newsgroups (&optional server)
188 (nnheader-report 'nnkiboze "LIST NEWSGROUPS is not implemented."))
190 (defun nnkiboze-request-delete-group (group &optional force server)
191 (nnkiboze-possibly-change-newsgroups group)
193 (let ((files (list (nnkiboze-nov-file-name)
194 (concat nnkiboze-directory group ".newsrc")
195 (nnkiboze-score-file group))))
197 (and (file-exists-p (car files))
198 (file-writable-p (car files))
199 (delete-file (car files)))
200 (setq files (cdr files)))))
201 (setq nnkiboze-current-group nil))
204 ;;; Internal functions.
206 (defun nnkiboze-possibly-change-newsgroups (group)
207 (setq nnkiboze-current-group group))
209 (defun nnkiboze-prefixed-name (group)
210 (gnus-group-prefixed-name group '(nnkiboze "")))
213 (defun nnkiboze-generate-groups ()
214 "Usage: emacs -batch -l nnkiboze -f nnkiboze-generate-groups
215 Finds out what articles are to be part of the nnkiboze groups."
217 (let ((nnmail-spool-file nil)
218 (gnus-use-dribble-file nil)
219 (gnus-read-active-file t)
220 (gnus-expert-user t))
222 (let* ((gnus-newsrc-alist (gnus-copy-sequence gnus-newsrc-alist))
223 (newsrc gnus-newsrc-alist)
225 (gnus-make-hashtable-from-newsrc-alist)
226 ;; We have copied all the newsrc alist info over to local copies
227 ;; so that we can mess all we want with these lists.
229 (if (string-match "nnkiboze" (car (car newsrc)))
230 ;; For each kiboze group, we call this function to generate
232 (nnkiboze-generate-group (car (car newsrc))))
233 (setq newsrc (cdr newsrc)))))
235 (defun nnkiboze-score-file (group)
236 (list (expand-file-name
237 (concat (file-name-as-directory gnus-kill-files-directory)
238 (nnheader-translate-file-chars
239 (concat nnkiboze-current-score-group
240 "." gnus-score-file-suffix))))))
242 (defun nnkiboze-generate-group (group)
243 (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
244 (newsrc-file (concat nnkiboze-directory group ".newsrc"))
245 (nov-file (concat nnkiboze-directory group ".nov"))
246 (regexp (nth 1 (nth 4 info)))
248 (gnus-large-newsgroup nil)
249 (version-control 'never)
250 (gnus-score-find-score-files-function 'nnkiboze-score-file)
251 gnus-select-group-hook gnus-summary-prepare-hook
252 gnus-thread-sort-functions gnus-show-threads
254 method nnkiboze-newsrc nov-buffer gname newsrc active
256 (setq nnkiboze-current-score-group group)
257 (or info (error "No such group: %s" group))
258 ;; Load the kiboze newsrc file for this group.
259 (and (file-exists-p newsrc-file) (load newsrc-file))
260 ;; We also load the nov file for this group.
262 (set-buffer (setq nov-buffer (find-file-noselect nov-file)))
263 (buffer-disable-undo (current-buffer)))
264 ;; Go through the active hashtb and add new all groups that match the
268 (and (string-match regexp (setq gname (symbol-name group))) ; Match
269 (not (assoc gname nnkiboze-newsrc)) ; It isn't registered
270 (numberp (car (symbol-value group))) ; It is active
271 (or (> nnkiboze-level 7)
272 (and (setq glevel (nth 1 (nth 2 (gnus-gethash
273 gname gnus-newsrc-hashtb))))
274 (>= nnkiboze-level glevel)))
275 (not (string-match "^nnkiboze:" gname)) ; Exclude kibozes
276 (setq nnkiboze-newsrc
277 (cons (cons gname (1- (car (symbol-value group))))
280 ;; `newsrc' is set to the list of groups that possibly are
281 ;; component groups to this kiboze group. This list has elements
282 ;; on the form `(GROUP . NUMBER)', where NUMBER is the highest
283 ;; number that has been kibozed in GROUP in this kiboze group.
284 (setq newsrc nnkiboze-newsrc)
286 (if (not (setq active (gnus-gethash
287 (car (car newsrc)) gnus-active-hashtb)))
288 ;; This group isn't active after all, so we remove it from
289 ;; the list of component groups.
290 (setq nnkiboze-newsrc (delq (car newsrc) nnkiboze-newsrc))
291 (setq lowest (cdr (car newsrc)))
292 ;; Ok, we have a valid component group, so we jump to it.
293 (switch-to-buffer gnus-group-buffer)
294 (gnus-group-jump-to-group (car (car newsrc)))
295 ;; We set all list of article marks to nil. Since we operate
296 ;; on copies of the real lists, we can destroy anything we
298 (and (setq ginfo (nth 2 (gnus-gethash (gnus-group-group-name)
299 gnus-newsrc-hashtb)))
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)'.
304 (and ginfo (setcar (nthcdr 2 ginfo)
305 (and (not (= lowest 1)) (cons 1 lowest))))
306 (if (not (and (or (not ginfo)
307 (> (length (gnus-list-of-unread-articles
310 (gnus-group-select-group nil)
311 (eq major-mode 'gnus-summary-mode))))
312 () ; No unread articles, or we couldn't enter this group.
313 ;; We are now in the group where we want to be.
314 (setq method (gnus-find-method-for-group gnus-newsgroup-name))
315 (and (eq method gnus-select-method) (setq method nil))
316 ;; We go through the list of scored articles.
317 (while gnus-newsgroup-scored
318 (if (> (car (car gnus-newsgroup-scored)) lowest)
319 ;; If it has a good score, then we enter this article
320 ;; into the kiboze group.
323 (gnus-summary-article-header
324 (car (car gnus-newsgroup-scored)))
326 (gnus-group-prefixed-name gnus-newsgroup-name method)
327 gnus-newsgroup-name)))
328 (setq gnus-newsgroup-scored (cdr gnus-newsgroup-scored)))
329 ;; That's it. We exit this group.
330 (gnus-summary-exit-no-update)))
331 (setcdr (car newsrc) (car active))
332 (setq newsrc (cdr newsrc)))
333 ;; We save the nov file.
334 (set-buffer nov-buffer)
336 (kill-buffer (current-buffer))
337 ;; We save the kiboze newsrc for this group.
338 (set-buffer (get-buffer-create "*nnkiboze work*"))
339 (buffer-disable-undo (current-buffer))
341 (insert "(setq nnkiboze-newsrc '" (prin1-to-string nnkiboze-newsrc)
343 (write-file newsrc-file)
344 (kill-buffer (current-buffer))
345 (switch-to-buffer gnus-group-buffer)
346 (gnus-group-list-groups 5 nil)))
348 (defun nnkiboze-enter-nov (buffer header group)
351 (goto-char (point-max))
352 (let ((xref (mail-header-xref header))
353 (prefix (gnus-group-real-prefix group))
356 (if (zerop (forward-line -1))
358 (setq article (1+ (read (current-buffer))))
361 (insert (int-to-string article) "\t"
362 (or (mail-header-subject header) "") "\t"
363 (or (mail-header-from header) "") "\t"
364 (or (mail-header-date header) "") "\t"
365 (or (mail-header-id header) "") "\t"
366 (or (mail-header-references header) "") "\t"
367 (int-to-string (or (mail-header-chars header) 0)) "\t"
368 (int-to-string (or (mail-header-lines header) 0)) "\t")
369 (if (or (not xref) (equal "" xref))
370 (insert "Xref: " (system-name) " " group ":"
371 (int-to-string (mail-header-number header))
373 (insert (mail-header-xref header) "\t\n")
374 (search-backward "\t" nil t)
375 (search-backward "\t" nil t)
376 (while (re-search-forward
378 (save-excursion (end-of-line) (point)) t)
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.
384 (goto-char (match-beginning 0))
385 (insert prefix group ":"
386 (int-to-string (mail-header-number header)) " ")
389 (goto-char (match-beginning 0))
390 (insert prefix)))))))
392 (defun nnkiboze-nov-file-name ()
393 (concat (file-name-as-directory nnkiboze-directory)
394 (nnheader-translate-file-chars
395 (concat (nnkiboze-prefixed-name nnkiboze-current-group) ".nov"))))
399 ;;; nnkiboze.el ends here