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