Synch with Gnus.
[elisp/gnus.git-] / lisp / nnkiboze.el
1 ;;; nnkiboze.el --- select virtual news access for Gnus
2 ;; Copyright (C) 1995,96,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
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)
12 ;; any later version.
13
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.
18
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.
23
24 ;;; Commentary:
25
26 ;; The other access methods (nntp, nnspool, etc) are general news
27 ;; access methods.  This module relies on Gnus and can't be used
28 ;; separately.
29
30 ;;; Code:
31
32 (eval-when-compile (require 'cl))
33 (eval-when-compile (require 'gnus-clfns))
34
35 (require 'nntp)
36 (require 'nnheader)
37 (require 'gnus)
38 (require 'gnus-score)
39 (require 'nnoo)
40
41 (nnoo-declare nnkiboze)
42 (defvoo nnkiboze-directory (nnheader-concat gnus-directory "kiboze/")
43   "nnkiboze will put its files in this directory.")
44
45 (defvoo nnkiboze-level 9
46   "The maximum level to be searched for articles.")
47
48 (defvoo nnkiboze-remove-read-articles t
49   "If non-nil, nnkiboze will remove read articles from the kiboze group.")
50
51 (defvoo nnkiboze-ephemeral nil
52   "If non-nil, don't store any data anywhere.")
53
54 (defvoo nnkiboze-scores nil
55   "Score rules for generating the nnkiboze group.")
56
57 (defvoo nnkiboze-regexp nil
58   "Regexp for matching component groups.")
59
60 (defvoo nnkiboze-file-coding-system nnheader-text-coding-system
61   "Coding system for nnkiboze files.")
62
63 \f
64
65 (defconst nnkiboze-version "nnkiboze 1.0")
66
67 (defvoo nnkiboze-current-group nil)
68 (defvoo nnkiboze-status-string "")
69
70 (defvoo nnkiboze-headers nil)
71
72 \f
73
74 ;;; Interface functions.
75
76 (nnoo-define-basics nnkiboze)
77
78 (deffoo nnkiboze-retrieve-headers (articles &optional group server fetch-old)
79   (nnkiboze-possibly-change-group group)
80   (unless gnus-nov-is-evil
81     (if (stringp (car articles))
82         'headers
83       (let ((nov (nnkiboze-nov-file-name)))
84         (when (file-exists-p nov)
85           (save-excursion
86             (set-buffer nntp-server-buffer)
87             (erase-buffer)
88             (let ((nnheader-file-coding-system nnkiboze-file-coding-system))
89               (nnheader-insert-file-contents nov))
90             (nnheader-nov-delete-outside-range
91              (car articles) (car (last articles)))
92             'nov))))))
93
94 (deffoo nnkiboze-request-article (article &optional newsgroup server buffer)
95   (nnkiboze-possibly-change-group newsgroup)
96   (if (not (numberp article))
97       ;; This is a real kludge.  It might not work at times, but it
98       ;; does no harm I think.  The only alternative is to offer no
99       ;; article fetching by message-id at all.
100       (nntp-request-article article newsgroup gnus-nntp-server buffer)
101     (let* ((header (gnus-summary-article-header article))
102            (xref (mail-header-xref header))
103            num group)
104       (unless xref
105         (error "nnkiboze: No xref"))
106       (unless (string-match " \\([^ ]+\\):\\([0-9]+\\)" xref)
107         (error "nnkiboze: Malformed xref"))
108       (setq num (string-to-int (match-string 2 xref))
109             group (match-string 1 xref))
110       (or (with-current-buffer buffer
111             (gnus-cache-request-article num group))
112           (gnus-request-article num group buffer)))))
113
114 (deffoo nnkiboze-request-scan (&optional group server)
115   (nnkiboze-generate-group (concat "nnkiboze:" group)))
116
117 (deffoo nnkiboze-request-group (group &optional server dont-check)
118   "Make GROUP the current newsgroup."
119   (nnkiboze-possibly-change-group group)
120   (if dont-check
121       t
122     (let ((nov-file (nnkiboze-nov-file-name))
123           beg end total)
124       (save-excursion
125         (set-buffer nntp-server-buffer)
126         (erase-buffer)
127         (unless (file-exists-p nov-file)
128           (nnkiboze-request-scan group))
129         (if (not (file-exists-p nov-file))
130             (nnheader-report 'nnkiboze "Can't select group %s" group)
131           (let ((nnheader-file-coding-system nnkiboze-file-coding-system))
132             (nnheader-insert-file-contents nov-file))
133           (if (zerop (buffer-size))
134               (nnheader-insert "211 0 0 0 %s\n" group)
135             (goto-char (point-min))
136             (when (looking-at "[0-9]+")
137               (setq beg (read (current-buffer))))
138             (goto-char (point-max))
139             (when (re-search-backward "^[0-9]" nil t)
140               (setq end (read (current-buffer))))
141             (setq total (count-lines (point-min) (point-max)))
142             (nnheader-insert "211 %d %d %d %s\n" total beg end group)))))))
143
144 (deffoo nnkiboze-close-group (group &optional server)
145   (nnkiboze-possibly-change-group group)
146   ;; Remove NOV lines of articles that are marked as read.
147   (when (and (file-exists-p (nnkiboze-nov-file-name))
148              nnkiboze-remove-read-articles)
149     (let ((coding-system-for-write nnkiboze-file-coding-system)
150           (output-coding-system nnkiboze-file-coding-system))
151       (with-temp-file (nnkiboze-nov-file-name)
152         (let ((cur (current-buffer)) 
153               (nnheader-file-coding-system nnkiboze-file-coding-system))
154           (nnheader-insert-file-contents (nnkiboze-nov-file-name))
155           (goto-char (point-min))
156           (while (not (eobp))
157             (if (not (gnus-article-read-p (read cur)))
158                 (forward-line 1)
159               (gnus-delete-line))))))
160     (setq nnkiboze-current-group nil)))
161
162 (deffoo nnkiboze-open-server (server &optional defs)
163   (unless (assq 'nnkiboze-regexp defs)
164     (push `(nnkiboze-regexp ,server)
165           defs))
166   (nnoo-change-server 'nnkiboze server defs))
167
168 (deffoo nnkiboze-request-delete-group (group &optional force server)
169   (nnkiboze-possibly-change-group group)
170   (when force
171     (let ((files (nconc
172                   (nnkiboze-score-file group)
173                   (list (nnkiboze-nov-file-name)
174                         (nnkiboze-nov-file-name ".newsrc")))))
175       (while files
176         (and (file-exists-p (car files))
177              (file-writable-p (car files))
178              (delete-file (car files)))
179         (setq files (cdr files)))))
180   (setq nnkiboze-current-group nil)
181   t)
182
183 (nnoo-define-skeleton nnkiboze)
184
185 \f
186 ;;; Internal functions.
187
188 (defun nnkiboze-possibly-change-group (group)
189   (setq nnkiboze-current-group group))
190
191 (defun nnkiboze-prefixed-name (group)
192   (gnus-group-prefixed-name group '(nnkiboze "")))
193
194 ;;;###autoload
195 (defun nnkiboze-generate-groups ()
196   "\"Usage: emacs -batch -l nnkiboze -f nnkiboze-generate-groups\".
197 Finds out what articles are to be part of the nnkiboze groups."
198   (interactive)
199   (let ((nnmail-spool-file nil)
200         (mail-sources nil)
201         (gnus-use-dribble-file nil)
202         (gnus-read-active-file t)
203         (gnus-expert-user t))
204     (gnus))
205   (let* ((gnus-newsrc-alist (gnus-copy-sequence gnus-newsrc-alist))
206          (newsrc (cdr gnus-newsrc-alist))
207          gnus-newsrc-hashtb info)
208     (gnus-make-hashtable-from-newsrc-alist)
209     ;; We have copied all the newsrc alist info over to local copies
210     ;; so that we can mess all we want with these lists.
211     (while (setq info (pop newsrc))
212       (when (string-match "nnkiboze" (gnus-info-group info))
213         ;; For each kiboze group, we call this function to generate
214         ;; it.
215         (nnkiboze-generate-group (gnus-info-group info) t))))
216   (save-excursion
217     (set-buffer gnus-group-buffer)
218     (gnus-group-list-groups)))
219
220 (defun nnkiboze-score-file (group)
221   (list (expand-file-name
222          (concat (file-name-as-directory gnus-kill-files-directory)
223                  (nnheader-translate-file-chars
224                   (concat (nnkiboze-prefixed-name nnkiboze-current-group)
225                           "." gnus-score-file-suffix))))))
226
227 (defun nnkiboze-generate-group (group &optional inhibit-list-groups)
228   (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
229          (newsrc-file (concat nnkiboze-directory
230                               (nnheader-translate-file-chars
231                                (concat group ".newsrc"))))
232          (nov-file (concat nnkiboze-directory
233                            (nnheader-translate-file-chars
234                             (concat group ".nov"))))
235          method nnkiboze-newsrc gname newsrc active
236          ginfo lowest glevel orig-info nov-buffer
237          ;; Bind various things to nil to make group entry faster.
238          (gnus-expert-user t)
239          (gnus-large-newsgroup nil)
240          (gnus-score-find-score-files-function 'nnkiboze-score-file)
241          ;; Use only nnkiboze-score-file!
242          (gnus-score-use-all-scores nil)
243          (gnus-use-scoring t)
244          (gnus-verbose (min gnus-verbose 3))
245          gnus-select-group-hook gnus-summary-prepare-hook
246          gnus-thread-sort-functions gnus-show-threads
247          gnus-visual gnus-suppress-duplicates num-unread)
248     (unless info
249       (error "No such group: %s" group))
250     ;; Load the kiboze newsrc file for this group.
251     (when (file-exists-p newsrc-file)
252       (load newsrc-file))
253     (let ((coding-system-for-write nnkiboze-file-coding-system)
254           (output-coding-system nnkiboze-file-coding-system))
255       (with-temp-file nov-file
256         (when (file-exists-p nov-file)
257           (nnheader-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)))))
342     ;; We save the kiboze newsrc for this group.
343     (with-temp-file newsrc-file
344       (insert "(setq nnkiboze-newsrc '")
345       (gnus-prin1 nnkiboze-newsrc)
346       (insert ")\n")))
347   (unless inhibit-list-groups
348     (save-excursion
349       (set-buffer gnus-group-buffer)
350       (gnus-group-list-groups)))
351   t)
352
353 (defun nnkiboze-enter-nov (buffer header group)
354   (save-excursion
355     (set-buffer buffer)
356     (goto-char (point-max))
357     (let ((prefix (gnus-group-real-prefix group))
358           (oheader (copy-sequence header))
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)
366       (with-temp-buffer
367         (insert (or (mail-header-xref oheader) ""))
368         (goto-char (point-min))
369         (if (re-search-forward " [^ ]+:[0-9]+" nil t)
370             (goto-char (match-beginning 0))
371           (or (eobp) (forward-char 1)))
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))))
382
383 (defun nnkiboze-nov-file-name (&optional suffix)
384   (concat (file-name-as-directory nnkiboze-directory)
385           (nnheader-translate-file-chars
386            (concat (nnkiboze-prefixed-name nnkiboze-current-group)
387                    (or suffix ".nov")))))
388
389 (provide 'nnkiboze)
390
391 ;;; nnkiboze.el ends here