comment
[bpt/emacs.git] / lisp / gnus / nnkiboze.el
CommitLineData
eec82323 1;;; nnkiboze.el --- select virtual news access for Gnus
16409b0b 2
ec4eb560 3;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
16409b0b 4;; Free Software Foundation, Inc.
eec82323 5
6748645f 6;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
ec4eb560 7;; Maintainer: bugs@gnus.org
eec82323
LMI
8;; Keywords: news
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation; either version 2, or (at your option)
15;; any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs; see the file COPYING. If not, write to the
24;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25;; Boston, MA 02111-1307, USA.
26
27;;; Commentary:
28
29;; The other access methods (nntp, nnspool, etc) are general news
30;; access methods. This module relies on Gnus and can't be used
31;; separately.
32
33;;; Code:
34
35(require 'nntp)
36(require 'nnheader)
37(require 'gnus)
38(require 'gnus-score)
39(require 'nnoo)
8b93df01 40(require 'mm-util)
eec82323
LMI
41(eval-when-compile (require 'cl))
42
43(nnoo-declare nnkiboze)
44(defvoo nnkiboze-directory (nnheader-concat gnus-directory "kiboze/")
45 "nnkiboze will put its files in this directory.")
46
47(defvoo nnkiboze-level 9
48 "The maximum level to be searched for articles.")
49
50(defvoo nnkiboze-remove-read-articles t
51 "If non-nil, nnkiboze will remove read articles from the kiboze group.")
52
53(defvoo nnkiboze-ephemeral nil
54 "If non-nil, don't store any data anywhere.")
55
56(defvoo nnkiboze-scores nil
57 "Score rules for generating the nnkiboze group.")
58
59(defvoo nnkiboze-regexp nil
60 "Regexp for matching component groups.")
61
8b93df01
DL
62(defvoo nnkiboze-file-coding-system mm-text-coding-system
63 "Coding system for nnkiboze files.")
64
eec82323
LMI
65\f
66
67(defconst nnkiboze-version "nnkiboze 1.0")
68
69(defvoo nnkiboze-current-group nil)
70(defvoo nnkiboze-status-string "")
71
72(defvoo nnkiboze-headers nil)
73
74\f
75
76;;; Interface functions.
77
78(nnoo-define-basics nnkiboze)
79
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))
84 'headers
85 (let ((nov (nnkiboze-nov-file-name)))
86 (when (file-exists-p nov)
87 (save-excursion
88 (set-buffer nntp-server-buffer)
89 (erase-buffer)
8b93df01
DL
90 (let ((nnheader-file-coding-system nnkiboze-file-coding-system))
91 (nnheader-insert-file-contents nov))
eec82323
LMI
92 (nnheader-nov-delete-outside-range
93 (car articles) (car (last articles)))
94 'nov))))))
95
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))
ec4eb560
DL
104 (xref (mail-header-xref header))
105 num group)
eec82323
LMI
106 (unless xref
107 (error "nnkiboze: No xref"))
108 (unless (string-match " \\([^ ]+\\):\\([0-9]+\\)" xref)
109 (error "nnkiboze: Malformed xref"))
ec4eb560
DL
110 (setq num (string-to-int (match-string 2 xref))
111 group (match-string 1 xref))
112 (or (with-current-buffer buffer
113 (gnus-cache-request-article num group))
114 (gnus-request-article num group buffer)))))
eec82323
LMI
115
116(deffoo nnkiboze-request-scan (&optional group server)
117 (nnkiboze-generate-group (concat "nnkiboze:" group)))
118
119(deffoo nnkiboze-request-group (group &optional server dont-check)
120 "Make GROUP the current newsgroup."
121 (nnkiboze-possibly-change-group group)
122 (if dont-check
123 t
124 (let ((nov-file (nnkiboze-nov-file-name))
125 beg end total)
126 (save-excursion
127 (set-buffer nntp-server-buffer)
128 (erase-buffer)
6748645f
LMI
129 (unless (file-exists-p nov-file)
130 (nnkiboze-request-scan group))
eec82323
LMI
131 (if (not (file-exists-p nov-file))
132 (nnheader-report 'nnkiboze "Can't select group %s" group)
8b93df01
DL
133 (let ((nnheader-file-coding-system nnkiboze-file-coding-system))
134 (nnheader-insert-file-contents nov-file))
eec82323
LMI
135 (if (zerop (buffer-size))
136 (nnheader-insert "211 0 0 0 %s\n" group)
137 (goto-char (point-min))
138 (when (looking-at "[0-9]+")
139 (setq beg (read (current-buffer))))
140 (goto-char (point-max))
141 (when (re-search-backward "^[0-9]" nil t)
142 (setq end (read (current-buffer))))
143 (setq total (count-lines (point-min) (point-max)))
144 (nnheader-insert "211 %d %d %d %s\n" total beg end group)))))))
145
146(deffoo nnkiboze-close-group (group &optional server)
147 (nnkiboze-possibly-change-group group)
148 ;; Remove NOV lines of articles that are marked as read.
149 (when (and (file-exists-p (nnkiboze-nov-file-name))
150 nnkiboze-remove-read-articles)
8b93df01
DL
151 (let ((coding-system-for-write nnkiboze-file-coding-system))
152 (with-temp-file (nnkiboze-nov-file-name)
153 (let ((cur (current-buffer))
154 (nnheader-file-coding-system nnkiboze-file-coding-system))
155 (nnheader-insert-file-contents (nnkiboze-nov-file-name))
156 (goto-char (point-min))
157 (while (not (eobp))
158 (if (not (gnus-article-read-p (read cur)))
159 (forward-line 1)
160 (gnus-delete-line))))))
161 (setq nnkiboze-current-group nil)))
eec82323
LMI
162
163(deffoo nnkiboze-open-server (server &optional defs)
164 (unless (assq 'nnkiboze-regexp defs)
165 (push `(nnkiboze-regexp ,server)
166 defs))
167 (nnoo-change-server 'nnkiboze server defs))
168
169(deffoo nnkiboze-request-delete-group (group &optional force server)
170 (nnkiboze-possibly-change-group group)
171 (when force
16409b0b
GM
172 (let ((files (nconc
173 (nnkiboze-score-file group)
174 (list (nnkiboze-nov-file-name)
175 (nnkiboze-nov-file-name ".newsrc")))))
176 (while files
177 (and (file-exists-p (car files))
178 (file-writable-p (car files))
179 (delete-file (car files)))
180 (setq files (cdr files)))))
6748645f
LMI
181 (setq nnkiboze-current-group nil)
182 t)
eec82323
LMI
183
184(nnoo-define-skeleton nnkiboze)
185
186\f
187;;; Internal functions.
188
189(defun nnkiboze-possibly-change-group (group)
190 (setq nnkiboze-current-group group))
191
192(defun nnkiboze-prefixed-name (group)
193 (gnus-group-prefixed-name group '(nnkiboze "")))
194
195;;;###autoload
196(defun nnkiboze-generate-groups ()
6748645f 197 "\"Usage: emacs -batch -l nnkiboze -f nnkiboze-generate-groups\".
eec82323
LMI
198Finds out what articles are to be part of the nnkiboze groups."
199 (interactive)
200 (let ((nnmail-spool-file nil)
16409b0b 201 (mail-sources nil)
eec82323
LMI
202 (gnus-use-dribble-file nil)
203 (gnus-read-active-file t)
204 (gnus-expert-user t))
205 (gnus))
206 (let* ((gnus-newsrc-alist (gnus-copy-sequence gnus-newsrc-alist))
207 (newsrc (cdr gnus-newsrc-alist))
208 gnus-newsrc-hashtb info)
209 (gnus-make-hashtable-from-newsrc-alist)
210 ;; We have copied all the newsrc alist info over to local copies
211 ;; so that we can mess all we want with these lists.
212 (while (setq info (pop newsrc))
213 (when (string-match "nnkiboze" (gnus-info-group info))
214 ;; For each kiboze group, we call this function to generate
215 ;; it.
ec4eb560
DL
216 (nnkiboze-generate-group (gnus-info-group info) t))))
217 (save-excursion
218 (set-buffer gnus-group-buffer)
219 (gnus-group-list-groups)))
eec82323
LMI
220
221(defun nnkiboze-score-file (group)
222 (list (expand-file-name
223 (concat (file-name-as-directory gnus-kill-files-directory)
224 (nnheader-translate-file-chars
225 (concat (nnkiboze-prefixed-name nnkiboze-current-group)
226 "." gnus-score-file-suffix))))))
227
ec4eb560 228(defun nnkiboze-generate-group (group &optional inhibit-list-groups)
eec82323 229 (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
16409b0b 230 (newsrc-file (concat nnkiboze-directory
a8151ef7
LMI
231 (nnheader-translate-file-chars
232 (concat group ".newsrc"))))
233 (nov-file (concat nnkiboze-directory
234 (nnheader-translate-file-chars
235 (concat group ".nov"))))
eec82323
LMI
236 method nnkiboze-newsrc gname newsrc active
237 ginfo lowest glevel orig-info nov-buffer
238 ;; Bind various things to nil to make group entry faster.
239 (gnus-expert-user t)
240 (gnus-large-newsgroup nil)
241 (gnus-score-find-score-files-function 'nnkiboze-score-file)
ec4eb560
DL
242 ;; Use only nnkiboze-score-file!
243 (gnus-score-use-all-scores nil)
244 (gnus-use-scoring t)
eec82323
LMI
245 (gnus-verbose (min gnus-verbose 3))
246 gnus-select-group-hook gnus-summary-prepare-hook
247 gnus-thread-sort-functions gnus-show-threads
6748645f 248 gnus-visual gnus-suppress-duplicates num-unread)
eec82323
LMI
249 (unless info
250 (error "No such group: %s" group))
251 ;; Load the kiboze newsrc file for this group.
252 (when (file-exists-p newsrc-file)
253 (load newsrc-file))
8b93df01
DL
254 (let ((coding-system-for-write nnkiboze-file-coding-system))
255 (with-temp-file nov-file
256 (when (file-exists-p nov-file)
257 (insert-file-contents nov-file))
258 (setq nov-buffer (current-buffer))
259 ;; Go through the active hashtb and add new all groups that match the
260 ;; kiboze regexp.
261 (mapatoms
262 (lambda (group)
263 (and (string-match nnkiboze-regexp
264 (setq gname (symbol-name group))) ; Match
265 (not (assoc gname nnkiboze-newsrc)) ; It isn't registered
266 (numberp (car (symbol-value group))) ; It is active
267 (or (> nnkiboze-level 7)
268 (and (setq glevel (nth 1 (nth 2 (gnus-gethash
269 gname gnus-newsrc-hashtb))))
270 (>= nnkiboze-level glevel)))
271 (not (string-match "^nnkiboze:" gname)) ; Exclude kibozes
272 (push (cons gname (1- (car (symbol-value group))))
273 nnkiboze-newsrc)))
274 gnus-active-hashtb)
275 ;; `newsrc' is set to the list of groups that possibly are
276 ;; component groups to this kiboze group. This list has elements
277 ;; on the form `(GROUP . NUMBER)', where NUMBER is the highest
278 ;; number that has been kibozed in GROUP in this kiboze group.
279 (setq newsrc nnkiboze-newsrc)
280 (while newsrc
281 (if (not (setq active (gnus-gethash
282 (caar newsrc) gnus-active-hashtb)))
283 ;; This group isn't active after all, so we remove it from
284 ;; the list of component groups.
285 (setq nnkiboze-newsrc (delq (car newsrc) nnkiboze-newsrc))
286 (setq lowest (cdar newsrc))
287 ;; Ok, we have a valid component group, so we jump to it.
288 (switch-to-buffer gnus-group-buffer)
289 (gnus-group-jump-to-group (caar newsrc))
290 (gnus-message 3 "nnkiboze: Checking %s..." (caar newsrc))
291 (setq ginfo (gnus-get-info (gnus-group-group-name))
292 orig-info (gnus-copy-sequence ginfo)
293 num-unread (car (gnus-gethash (caar newsrc)
294 gnus-newsrc-hashtb)))
295 (unwind-protect
296 (progn
297 ;; We set all list of article marks to nil. Since we operate
298 ;; on copies of the real lists, we can destroy anything we
299 ;; want here.
300 (when (nth 3 ginfo)
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 (when ginfo
305 (setcar (nthcdr 2 ginfo)
306 (and (not (= lowest 1)) (cons 1 lowest))))
307 (when (and (or (not ginfo)
308 (> (length (gnus-list-of-unread-articles
309 (car ginfo)))
310 0))
311 (progn
312 (ignore-errors
313 (gnus-group-select-group nil))
314 (eq major-mode 'gnus-summary-mode)))
315 ;; We are now in the group where we want to be.
316 (setq method (gnus-find-method-for-group
317 gnus-newsgroup-name))
318 (when (eq method gnus-select-method)
319 (setq method nil))
320 ;; We go through the list of scored articles.
321 (while gnus-newsgroup-scored
322 (when (> (caar gnus-newsgroup-scored) lowest)
323 ;; If it has a good score, then we enter this article
324 ;; into the kiboze group.
325 (nnkiboze-enter-nov
326 nov-buffer
327 (gnus-summary-article-header
328 (caar gnus-newsgroup-scored))
329 gnus-newsgroup-name))
330 (setq gnus-newsgroup-scored (cdr gnus-newsgroup-scored)))
331 ;; That's it. We exit this group.
332 (when (eq major-mode 'gnus-summary-mode)
333 (kill-buffer (current-buffer)))))
334 ;; Restore the proper info.
335 (when ginfo
336 (setcdr ginfo (cdr orig-info)))
337 (setcar (gnus-gethash (caar newsrc) gnus-newsrc-hashtb)
338 num-unread)))
339 (setcdr (car newsrc) (car active))
340 (gnus-message 3 "nnkiboze: Checking %s...done" (caar newsrc))
341 (setq newsrc (cdr newsrc)))))
eec82323 342 ;; We save the kiboze newsrc for this group.
16409b0b 343 (with-temp-file newsrc-file
eec82323
LMI
344 (insert "(setq nnkiboze-newsrc '")
345 (gnus-prin1 nnkiboze-newsrc)
6748645f 346 (insert ")\n")))
ec4eb560
DL
347 (unless inhibit-list-groups
348 (save-excursion
349 (set-buffer gnus-group-buffer)
350 (gnus-group-list-groups)))
6748645f 351 t)
eec82323
LMI
352
353(defun nnkiboze-enter-nov (buffer header group)
354 (save-excursion
355 (set-buffer buffer)
356 (goto-char (point-max))
6748645f 357 (let ((prefix (gnus-group-real-prefix group))
eec82323 358 (oheader (copy-sequence header))
eec82323
LMI
359 article)
360 (if (zerop (forward-line -1))
361 (progn
362 (setq article (1+ (read (current-buffer))))
363 (forward-line 1))
364 (setq article 1))
365 (mail-header-set-number oheader article)
8b93df01 366 (with-temp-buffer
ec4eb560 367 (insert (or (mail-header-xref oheader) ""))
8b93df01
DL
368 (goto-char (point-min))
369 (if (re-search-forward " [^ ]+:[0-9]+" nil t)
370 (goto-char (match-beginning 0))
ec4eb560 371 (or (eobp) (forward-char 1)))
8b93df01
DL
372 ;; The first Xref has to be the group this article
373 ;; really came for - this is the article nnkiboze
374 ;; will request when it is asked for the article.
375 (insert " " group ":"
376 (int-to-string (mail-header-number header)) " ")
377 (while (re-search-forward " [^ ]+:[0-9]+" nil t)
378 (goto-char (1+ (match-beginning 0)))
379 (insert prefix))
380 (mail-header-set-xref oheader (buffer-string)))
381 (nnheader-insert-nov oheader))))
eec82323 382
6748645f 383(defun nnkiboze-nov-file-name (&optional suffix)
eec82323
LMI
384 (concat (file-name-as-directory nnkiboze-directory)
385 (nnheader-translate-file-chars
6748645f
LMI
386 (concat (nnkiboze-prefixed-name nnkiboze-current-group)
387 (or suffix ".nov")))))
eec82323
LMI
388
389(provide 'nnkiboze)
390
391;;; nnkiboze.el ends here