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