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