8fb0b6c50b56cabd29520d099c94b78975ee7df0
[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 (eval-when-compile (require 'cl))
35 (eval-when-compile (require 'gnus-clfns))
36
37 (require 'nntp)
38 (require 'nnheader)
39 (require 'gnus)
40 (require 'gnus-score)
41 (require 'nnoo)
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
62 (defvoo nnkiboze-file-coding-system nnheader-text-coding-system
63   "Coding system for nnkiboze files.")
64
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)
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)))
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))
104            (xref (mail-header-xref header))
105            num group)
106       (unless xref
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             (gnus-cache-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           (output-coding-system nnkiboze-file-coding-system))
153       (with-temp-file (nnkiboze-nov-file-name)
154         (let ((cur (current-buffer)) 
155               (nnheader-file-coding-system nnkiboze-file-coding-system))
156           (nnheader-insert-file-contents (nnkiboze-nov-file-name))
157           (goto-char (point-min))
158           (while (not (eobp))
159             (if (not (gnus-article-read-p (read cur)))
160                 (forward-line 1)
161               (gnus-delete-line))))))
162     (setq nnkiboze-current-group nil)))
163
164 (deffoo nnkiboze-open-server (server &optional defs)
165   (unless (assq 'nnkiboze-regexp defs)
166     (push `(nnkiboze-regexp ,server)
167           defs))
168   (nnoo-change-server 'nnkiboze server defs))
169
170 (deffoo nnkiboze-request-delete-group (group &optional force server)
171   (nnkiboze-possibly-change-group group)
172   (when force
173     (let ((files (nconc
174                   (nnkiboze-score-file group)
175                   (list (nnkiboze-nov-file-name)
176                         (nnkiboze-nov-file-name ".newsrc")))))
177       (while files
178         (and (file-exists-p (car files))
179              (file-writable-p (car files))
180              (delete-file (car files)))
181         (setq files (cdr files)))))
182   (setq nnkiboze-current-group nil)
183   t)
184
185 (nnoo-define-skeleton nnkiboze)
186
187 \f
188 ;;; Internal functions.
189
190 (defun nnkiboze-possibly-change-group (group)
191   (setq nnkiboze-current-group group))
192
193 (defun nnkiboze-prefixed-name (group)
194   (gnus-group-prefixed-name group '(nnkiboze "")))
195
196 ;;;###autoload
197 (defun nnkiboze-generate-groups ()
198   "\"Usage: emacs -batch -l nnkiboze -f nnkiboze-generate-groups\".
199 Finds out what articles are to be part of the nnkiboze groups."
200   (interactive)
201   (let ((nnmail-spool-file nil)
202         (mail-sources nil)
203         (gnus-use-dribble-file nil)
204         (gnus-read-active-file t)
205         (gnus-expert-user t))
206     (gnus))
207   (let* ((gnus-newsrc-alist (gnus-copy-sequence gnus-newsrc-alist))
208          (newsrc (cdr gnus-newsrc-alist))
209          gnus-newsrc-hashtb info)
210     (gnus-make-hashtable-from-newsrc-alist)
211     ;; We have copied all the newsrc alist info over to local copies
212     ;; so that we can mess all we want with these lists.
213     (while (setq info (pop newsrc))
214       (when (string-match "nnkiboze" (gnus-info-group info))
215         ;; For each kiboze group, we call this function to generate
216         ;; it.
217         (nnkiboze-generate-group (gnus-info-group info) t))))
218   (save-excursion
219     (set-buffer gnus-group-buffer)
220     (gnus-group-list-groups)))
221
222 (defun nnkiboze-score-file (group)
223   (list (expand-file-name
224          (concat (file-name-as-directory gnus-kill-files-directory)
225                  (nnheader-translate-file-chars
226                   (concat (nnkiboze-prefixed-name nnkiboze-current-group)
227                           "." gnus-score-file-suffix))))))
228
229 (defun nnkiboze-generate-group (group &optional inhibit-list-groups)
230   (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
231          (newsrc-file (concat nnkiboze-directory
232                               (nnheader-translate-file-chars
233                                (concat group ".newsrc"))))
234          (nov-file (concat nnkiboze-directory
235                            (nnheader-translate-file-chars
236                             (concat group ".nov"))))
237          method nnkiboze-newsrc gname newsrc active
238          ginfo lowest glevel orig-info nov-buffer
239          ;; Bind various things to nil to make group entry faster.
240          (gnus-expert-user t)
241          (gnus-large-newsgroup nil)
242          (gnus-score-find-score-files-function 'nnkiboze-score-file)
243          ;; Use only nnkiboze-score-file!
244          (gnus-score-use-all-scores nil)
245          (gnus-use-scoring t)
246          (gnus-verbose (min gnus-verbose 3))
247          gnus-select-group-hook gnus-summary-prepare-hook
248          gnus-thread-sort-functions gnus-show-threads
249          gnus-visual gnus-suppress-duplicates num-unread)
250     (unless info
251       (error "No such group: %s" group))
252     ;; Load the kiboze newsrc file for this group.
253     (when (file-exists-p newsrc-file)
254       (load newsrc-file))
255     (let ((coding-system-for-write nnkiboze-file-coding-system)
256           (output-coding-system nnkiboze-file-coding-system))
257       (with-temp-file nov-file
258         (when (file-exists-p nov-file)
259           (nnheader-insert-file-contents nov-file))
260         (setq nov-buffer (current-buffer))
261         ;; Go through the active hashtb and add new all groups that match the
262         ;; kiboze regexp.
263         (mapatoms
264          (lambda (group)
265            (and (string-match nnkiboze-regexp
266                               (setq gname (symbol-name group))) ; Match
267                 (not (assoc gname nnkiboze-newsrc)) ; It isn't registered
268                 (numberp (car (symbol-value group))) ; It is active
269                 (or (> nnkiboze-level 7)
270                     (and (setq glevel (nth 1 (nth 2 (gnus-gethash
271                                                      gname gnus-newsrc-hashtb))))
272                          (>= nnkiboze-level glevel)))
273                 (not (string-match "^nnkiboze:" gname)) ; Exclude kibozes
274                 (push (cons gname (1- (car (symbol-value group))))
275                       nnkiboze-newsrc)))
276          gnus-active-hashtb)
277         ;; `newsrc' is set to the list of groups that possibly are
278         ;; component groups to this kiboze group.  This list has elements
279         ;; on the form `(GROUP . NUMBER)', where NUMBER is the highest
280         ;; number that has been kibozed in GROUP in this kiboze group.
281         (setq newsrc nnkiboze-newsrc)
282         (while newsrc
283           (if (not (setq active (gnus-gethash
284                                  (caar newsrc) gnus-active-hashtb)))
285               ;; This group isn't active after all, so we remove it from
286               ;; the list of component groups.
287               (setq nnkiboze-newsrc (delq (car newsrc) nnkiboze-newsrc))
288             (setq lowest (cdar newsrc))
289             ;; Ok, we have a valid component group, so we jump to it.
290             (switch-to-buffer gnus-group-buffer)
291             (gnus-group-jump-to-group (caar newsrc))
292             (gnus-message 3 "nnkiboze: Checking %s..." (caar newsrc))
293             (setq ginfo (gnus-get-info (gnus-group-group-name))
294                   orig-info (gnus-copy-sequence ginfo)
295                   num-unread (car (gnus-gethash (caar newsrc)
296                                                 gnus-newsrc-hashtb)))
297             (unwind-protect
298                 (progn
299                   ;; We set all list of article marks to nil.  Since we operate
300                   ;; on copies of the real lists, we can destroy anything we
301                   ;; want here.
302                   (when (nth 3 ginfo)
303                     (setcar (nthcdr 3 ginfo) nil))
304                   ;; We set the list of read articles to be what we expect for
305                   ;; this kiboze group -- either nil or `(1 . LOWEST)'.
306                   (when ginfo
307                     (setcar (nthcdr 2 ginfo)
308                             (and (not (= lowest 1)) (cons 1 lowest))))
309                   (when (and (or (not ginfo)
310                                  (> (length (gnus-list-of-unread-articles
311                                              (car ginfo)))
312                                     0))
313                              (progn
314                                (ignore-errors
315                                  (gnus-group-select-group nil))
316                                (eq major-mode 'gnus-summary-mode)))
317                     ;; We are now in the group where we want to be.
318                     (setq method (gnus-find-method-for-group
319                                   gnus-newsgroup-name))
320                     (when (eq method gnus-select-method)
321                       (setq method nil))
322                     ;; We go through the list of scored articles.
323                     (while gnus-newsgroup-scored
324                       (when (> (caar gnus-newsgroup-scored) lowest)
325                         ;; If it has a good score, then we enter this article
326                         ;; into the kiboze group.
327                         (nnkiboze-enter-nov
328                          nov-buffer
329                          (gnus-summary-article-header
330                           (caar gnus-newsgroup-scored))
331                          gnus-newsgroup-name))
332                       (setq gnus-newsgroup-scored (cdr gnus-newsgroup-scored)))
333                     ;; That's it.  We exit this group.
334                     (when (eq major-mode 'gnus-summary-mode)
335                       (kill-buffer (current-buffer)))))
336               ;; Restore the proper info.
337               (when ginfo
338                 (setcdr ginfo (cdr orig-info)))
339               (setcar (gnus-gethash (caar newsrc) gnus-newsrc-hashtb)
340                       num-unread)))
341           (setcdr (car newsrc) (car active))
342           (gnus-message 3 "nnkiboze: Checking %s...done" (caar newsrc))
343           (setq newsrc (cdr newsrc)))))
344     ;; We save the kiboze newsrc for this group.
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