Importing Oort Gnus v0.05.
[elisp/gnus.git-] / lisp / nnkiboze.el
1 ;;; nnkiboze.el --- select virtual news access for Gnus
2
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
4 ;;      Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
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)
14 ;; any later version.
15
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.
20
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.
25
26 ;;; Commentary:
27
28 ;; The other access methods (nntp, nnspool, etc) are general news
29 ;; access methods.  This module relies on Gnus and can't be used
30 ;; separately.
31
32 ;;; Code:
33
34 (require 'nntp)
35 (require 'nnheader)
36 (require 'gnus)
37 (require 'gnus-score)
38 (require 'nnoo)
39 (require 'mm-util)
40 (eval-when-compile (require 'cl))
41
42 (nnoo-declare nnkiboze)
43 (defvoo nnkiboze-directory (nnheader-concat gnus-directory "kiboze/")
44   "nnkiboze will put its files in this directory.")
45
46 (defvoo nnkiboze-level 9
47   "The maximum level to be searched for articles.")
48
49 (defvoo nnkiboze-remove-read-articles t
50   "If non-nil, nnkiboze will remove read articles from the kiboze group.")
51
52 (defvoo nnkiboze-ephemeral nil
53   "If non-nil, don't store any data anywhere.")
54
55 (defvoo nnkiboze-scores nil
56   "Score rules for generating the nnkiboze group.")
57
58 (defvoo nnkiboze-regexp nil
59   "Regexp for matching component groups.")
60
61 (defvoo nnkiboze-file-coding-system mm-text-coding-system
62   "Coding system for nnkiboze files.")
63
64 \f
65
66 (defconst nnkiboze-version "nnkiboze 1.0")
67
68 (defvoo nnkiboze-current-group nil)
69 (defvoo nnkiboze-status-string "")
70
71 (defvoo nnkiboze-headers nil)
72
73 \f
74
75 ;;; Interface functions.
76
77 (nnoo-define-basics nnkiboze)
78
79 (deffoo nnkiboze-retrieve-headers (articles &optional group server fetch-old)
80   (nnkiboze-possibly-change-group group)
81   (unless gnus-nov-is-evil
82     (if (stringp (car articles))
83         'headers
84       (let ((nov (nnkiboze-nov-file-name)))
85         (when (file-exists-p nov)
86           (save-excursion
87             (set-buffer nntp-server-buffer)
88             (erase-buffer)
89             (let ((nnheader-file-coding-system nnkiboze-file-coding-system))
90               (nnheader-insert-file-contents nov))
91             (nnheader-nov-delete-outside-range
92              (car articles) (car (last articles)))
93             'nov))))))
94
95 (deffoo nnkiboze-request-article (article &optional newsgroup server buffer)
96   (nnkiboze-possibly-change-group newsgroup)
97   (if (not (numberp article))
98       ;; This is a real kludge.  It might not work at times, but it
99       ;; does no harm I think.  The only alternative is to offer no
100       ;; article fetching by message-id at all.
101       (nntp-request-article article newsgroup gnus-nntp-server buffer)
102     (let* ((header (gnus-summary-article-header article))
103            (xref (mail-header-xref header))
104            num group)
105       (unless xref
106         (error "nnkiboze: No xref"))
107       (unless (string-match " \\([^ ]+\\):\\([0-9]+\\)" xref)
108         (error "nnkiboze: Malformed xref"))
109       (setq num (string-to-int (match-string 2 xref))
110             group (match-string 1 xref))
111       (or (with-current-buffer buffer
112             (or (gnus-cache-request-article num group)
113                 (gnus-agent-request-article num group)))
114           (gnus-request-article num group buffer)))))
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)
129         (unless (file-exists-p nov-file)
130           (nnkiboze-request-scan group))
131         (if (not (file-exists-p nov-file))
132             (nnheader-report 'nnkiboze "Can't select group %s" group)
133           (let ((nnheader-file-coding-system nnkiboze-file-coding-system))
134             (nnheader-insert-file-contents nov-file))
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)
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)))
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
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)))))
181   (setq nnkiboze-current-group nil)
182   t)
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 ()
197   "\"Usage: emacs -batch -l nnkiboze -f nnkiboze-generate-groups\".
198 Finds out what articles are to be part of the nnkiboze groups."
199   (interactive)
200   (let ((nnmail-spool-file nil)
201         (mail-sources nil)
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.
216         (nnkiboze-generate-group (gnus-info-group info) t))))
217   (save-excursion
218     (set-buffer gnus-group-buffer)
219     (gnus-group-list-groups)))
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
228 (defun nnkiboze-generate-group (group &optional inhibit-list-groups)
229   (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
230          (newsrc-file (concat nnkiboze-directory
231                               (nnheader-translate-file-chars
232                                (concat group ".newsrc"))))
233          (nov-file (concat nnkiboze-directory
234                            (nnheader-translate-file-chars
235                             (concat group ".nov"))))
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)
242          ;; Use only nnkiboze-score-file!
243          (gnus-score-use-all-scores nil)
244          (gnus-use-scoring t)
245          (gnus-verbose (min gnus-verbose 3))
246          gnus-select-group-hook gnus-summary-prepare-hook
247          gnus-thread-sort-functions gnus-show-threads
248          gnus-visual gnus-suppress-duplicates num-unread)
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))
254     (let ((coding-system-for-write nnkiboze-file-coding-system))
255       (gnus-make-directory (file-name-directory nov-file))
256       (with-temp-file nov-file
257         (when (file-exists-p nov-file)
258           (insert-file-contents nov-file))
259         (setq nov-buffer (current-buffer))
260         ;; Go through the active hashtb and add new all groups that match the
261         ;; kiboze regexp.
262         (mapatoms
263          (lambda (group)
264            (and (string-match nnkiboze-regexp
265                               (setq gname (symbol-name group))) ; Match
266                 (not (assoc gname nnkiboze-newsrc)) ; It isn't registered
267                 (numberp (car (symbol-value group))) ; It is active
268                 (or (> nnkiboze-level 7)
269                     (and (setq glevel (nth 1 (nth 2 (gnus-gethash
270                                                      gname gnus-newsrc-hashtb))))
271                          (>= nnkiboze-level glevel)))
272                 (not (string-match "^nnkiboze:" gname)) ; Exclude kibozes
273                 (push (cons gname (1- (car (symbol-value group))))
274                       nnkiboze-newsrc)))
275          gnus-active-hashtb)
276         ;; `newsrc' is set to the list of groups that possibly are
277         ;; component groups to this kiboze group.  This list has elements
278         ;; on the form `(GROUP . NUMBER)', where NUMBER is the highest
279         ;; number that has been kibozed in GROUP in this kiboze group.
280         (setq newsrc nnkiboze-newsrc)
281         (while newsrc
282           (if (not (setq active (gnus-gethash
283                                  (caar newsrc) gnus-active-hashtb)))
284             ;; This group isn't active after all, so we remove it from
285               ;; the list of component groups.
286               (setq nnkiboze-newsrc (delq (car newsrc) nnkiboze-newsrc))
287             (setq lowest (cdar newsrc))
288             ;; Ok, we have a valid component group, so we jump to it.
289             (switch-to-buffer gnus-group-buffer)
290             (gnus-group-jump-to-group (caar newsrc))
291             (gnus-message 3 "nnkiboze: Checking %s..." (caar newsrc))
292             (setq ginfo (gnus-get-info (gnus-group-group-name))
293                   orig-info (gnus-copy-sequence ginfo)
294                   num-unread (car (gnus-gethash (caar newsrc)
295                                                 gnus-newsrc-hashtb)))
296             (unwind-protect
297                 (progn
298                   ;; We set all list of article marks to nil.  Since we operate
299                   ;; on copies of the real lists, we can destroy anything we
300                   ;; want here.
301                   (when (nth 3 ginfo)
302                     (setcar (nthcdr 3 ginfo) nil))
303                   ;; We set the list of read articles to be what we expect for
304                   ;; this kiboze group -- either nil or `(1 . LOWEST)'.
305                   (when ginfo
306                     (setcar (nthcdr 2 ginfo)
307                             (and (not (= lowest 1)) (cons 1 lowest))))
308                   (when (and (or (not ginfo)
309                                  (> (length (gnus-list-of-unread-articles
310                                              (car ginfo)))
311                                     0))
312                              (progn
313                                (ignore-errors
314                                  (gnus-group-select-group nil))
315                                (eq major-mode 'gnus-summary-mode)))
316                     ;; We are now in the group where we want to be.
317                     (setq method (gnus-find-method-for-group
318                                   gnus-newsgroup-name))
319                     (when (eq method gnus-select-method)
320                       (setq method nil))
321                     ;; We go through the list of scored articles.
322                     (while gnus-newsgroup-scored
323                       (when (> (caar gnus-newsgroup-scored) lowest)
324                         ;; If it has a good score, then we enter this article
325                         ;; into the kiboze group.
326                         (nnkiboze-enter-nov
327                          nov-buffer
328                          (gnus-summary-article-header
329                           (caar gnus-newsgroup-scored))
330                          gnus-newsgroup-name))
331                       (setq gnus-newsgroup-scored (cdr gnus-newsgroup-scored)))
332                     ;; That's it.  We exit this group.
333                     (when (eq major-mode 'gnus-summary-mode)
334                       (kill-buffer (current-buffer)))))
335               ;; Restore the proper info.
336               (when ginfo
337                 (setcdr ginfo (cdr orig-info)))
338               (setcar (gnus-gethash (caar newsrc) gnus-newsrc-hashtb)
339                       num-unread)))
340           (setcdr (car newsrc) (car active))
341           (gnus-message 3 "nnkiboze: Checking %s...done" (caar newsrc))
342           (setq newsrc (cdr newsrc)))))
343     ;; We save the kiboze newsrc for this group.
344     (gnus-make-directory (file-name-directory newsrc-file))
345     (with-temp-file newsrc-file
346       (insert "(setq nnkiboze-newsrc '")
347       (gnus-prin1 nnkiboze-newsrc)
348       (insert ")\n")))
349   (unless inhibit-list-groups
350     (save-excursion
351       (set-buffer gnus-group-buffer)
352       (gnus-group-list-groups)))
353   t)
354
355 (defun nnkiboze-enter-nov (buffer header group)
356   (save-excursion
357     (set-buffer buffer)
358     (goto-char (point-max))
359     (let ((prefix (gnus-group-real-prefix group))
360           (oheader (copy-sequence header))
361           article)
362       (if (zerop (forward-line -1))
363           (progn
364             (setq article (1+ (read (current-buffer))))
365             (forward-line 1))
366         (setq article 1))
367       (mail-header-set-number oheader article)
368       (with-temp-buffer
369         (insert (or (mail-header-xref oheader) ""))
370         (goto-char (point-min))
371         (if (re-search-forward " [^ ]+:[0-9]+" nil t)
372             (goto-char (match-beginning 0))
373           (or (eobp) (forward-char 1)))
374         ;; The first Xref has to be the group this article
375         ;; really came for - this is the article nnkiboze
376         ;; will request when it is asked for the article.
377         (insert " " group ":"
378                 (int-to-string (mail-header-number header)) " ")
379         (while (re-search-forward " [^ ]+:[0-9]+" nil t)
380           (goto-char (1+ (match-beginning 0)))
381           (insert prefix))
382         (mail-header-set-xref oheader (buffer-string)))
383       (nnheader-insert-nov oheader))))
384
385 (defun nnkiboze-nov-file-name (&optional suffix)
386   (concat (file-name-as-directory nnkiboze-directory)
387           (nnheader-translate-file-chars
388            (concat (nnkiboze-prefixed-name nnkiboze-current-group)
389                    (or suffix ".nov")))))
390
391 (provide 'nnkiboze)
392
393 ;;; nnkiboze.el ends here