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