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