*** empty log message ***
[gnus] / lisp / nnkiboze.el
1 ;;; nnkiboze.el --- select virtual news access for Gnus
2 ;; Copyright (C) 1995 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
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 ;;; Commentary:
24
25 ;; The other access methods (nntp, nnspool, etc) are general news
26 ;; access methods. This module relies on Gnus and can not be used
27 ;; separately.
28
29 ;;; Code:
30
31 (require 'nntp)
32 (require 'nnheader)
33 (require 'gnus)
34 (require 'gnus-score)
35
36 (defvar nnkiboze-directory 
37   (expand-file-name (or gnus-article-save-directory "~/News/"))
38   "nnkiboze will put its files in this directory.")
39
40 (defvar nnkiboze-level 9
41   "*The maximum level to be searched for articles.")
42
43 (defvar nnkiboze-remove-read-articles t
44   "*If non-nil, nnkiboze will remove read articles from the kiboze group.")
45
46 \f
47
48 (defconst nnkiboze-version "nnkiboze 1.0"
49   "Version numbers of this version of nnkiboze.")
50
51 (defvar nnkiboze-current-group nil)
52 (defvar nnkiboze-current-score-group "")
53 (defvar nnkiboze-status-string "")
54
55 \f
56
57 ;;; Interface functions.
58
59 (defun nnkiboze-retrieve-headers (articles &optional group server fetch-old)
60   (nnkiboze-possibly-change-newsgroups group)
61   (if gnus-nov-is-evil
62       nil
63     (if (stringp (car articles))
64         'headers
65       (let ((first (car articles))
66             (last (progn (while (cdr articles) (setq articles (cdr articles)))
67                          (car articles)))
68             (nov (nnkiboze-nov-file-name)))
69         (if (file-exists-p nov)
70             (save-excursion
71               (set-buffer nntp-server-buffer)
72               (erase-buffer)
73               (insert-file-contents nov)
74               (goto-char (point-min))
75               (while (and (not (eobp)) (< first (read (current-buffer))))
76                 (forward-line 1))
77               (beginning-of-line)
78               (if (not (eobp)) (delete-region 1 (point)))
79               (while (and (not (eobp)) (>= last (read (current-buffer))))
80                 (forward-line 1))
81               (beginning-of-line)
82               (if (not (eobp)) (delete-region (point) (point-max)))
83               'nov))))))
84
85 (defun nnkiboze-open-server (newsgroups &optional something)
86   "Open a virtual newsgroup that contains NEWSGROUPS."
87   (gnus-make-directory nnkiboze-directory)
88   (nnheader-init-server-buffer))
89
90 (defun nnkiboze-close-server (&rest dum)
91   "Close news server."
92   t)
93
94 (defalias 'nnkiboze-request-quit (symbol-function 'nnkiboze-close-server))
95
96 (defun nnkiboze-server-opened (&optional server)
97   "Return server process status, T or NIL.
98 If the stream is opened, return T, otherwise return NIL."
99   (and nntp-server-buffer
100        (get-buffer nntp-server-buffer)))
101
102 (defun nnkiboze-status-message (&optional server)
103   "Return server status response as string."
104   nnkiboze-status-string)
105
106 (defun nnkiboze-request-article (article &optional newsgroup server buffer)
107   "Select article by message number."
108   (nnkiboze-possibly-change-newsgroups newsgroup)
109   (if (not (numberp article))
110       ;; This is a real cludge. It might not work at times, but it
111       ;; does no harm I think. The only alternative is to offer no
112       ;; article fetching by message-id at all.
113       (nntp-request-article article newsgroup gnus-nntp-server buffer)
114     (let* ((header (gnus-summary-article-header article))
115            (xref (mail-header-xref header))
116            igroup iarticle)
117       (or xref (error "nnkiboze: No xref"))
118       (or (string-match " \\([^ ]+\\):\\([0-9]+\\)" xref)
119           (error "nnkiboze: Malformed xref"))
120       (setq igroup (substring xref (match-beginning 1) (match-end 1)))
121       (setq iarticle (string-to-int 
122                       (substring xref (match-beginning 2) (match-end 2))))
123       (and (gnus-request-group igroup t)
124            (gnus-request-article iarticle igroup buffer)))))
125
126 (defun nnkiboze-request-group (group &optional server dont-check)
127   "Make GROUP the current newsgroup."
128   (nnkiboze-possibly-change-newsgroups group)
129   (if dont-check
130       ()
131     (let ((nov-file (nnkiboze-nov-file-name))
132           beg end total)
133       (save-excursion
134         (set-buffer nntp-server-buffer)
135         (erase-buffer)
136         (if (not (file-exists-p nov-file))
137             (insert (format "211 0 0 0 %s\n" group))
138           (insert-file-contents nov-file)
139           (if (zerop (buffer-size))
140               (insert (format "211 0 0 0 %s\n" group))
141             (goto-char (point-min))
142             (and (looking-at "[0-9]+") (setq beg (read (current-buffer))))
143             (goto-char (point-max))
144             (and (re-search-backward "^[0-9]" nil t)
145                  (setq end (read (current-buffer))))
146             (setq total (count-lines (point-min) (point-max)))
147             (erase-buffer)
148             (insert (format "211 %d %d %d %s\n" total beg end group)))))))
149   t)
150
151 (defun nnkiboze-close-group (group &optional server)
152   (nnkiboze-possibly-change-newsgroups group)
153   ;; Remove NOV lines of articles that are marked as read.
154   (if (or (not (file-exists-p (nnkiboze-nov-file-name)))
155           (not nnkiboze-remove-read-articles))
156       ()
157     (save-excursion
158       (let ((unreads gnus-newsgroup-unreads)
159             (unselected gnus-newsgroup-unselected))
160         (set-buffer (get-buffer-create "*nnkiboze work*"))
161         (buffer-disable-undo (current-buffer))
162         (erase-buffer)
163         (let ((cur (current-buffer))
164               article)
165           (insert-file-contents (nnkiboze-nov-file-name))
166           (goto-char (point-min))
167           (while (looking-at "[0-9]+")
168             (if (or (memq (setq article (read cur)) unreads)
169                     (memq article unselected))
170                 (forward-line 1)
171               (delete-region (progn (beginning-of-line) (point))
172                              (progn (forward-line 1) (point)))))
173           (write-file (nnkiboze-nov-file-name))
174           (kill-buffer (current-buffer)))))
175     (setq nnkiboze-current-group nil)))
176
177 (defun nnkiboze-request-list (&optional server) 
178   (setq nnkiboze-status-string "nnkiboze: LIST is not implemented.")
179   nil)
180
181 (defun nnkiboze-request-newgroups (date &optional server)
182   "List new groups."
183   (setq nnkiboze-status-string "NEWGROUPS is not supported.")
184   nil)
185
186 (defun nnkiboze-request-list-newsgroups (&optional server)
187   (setq nnkiboze-status-string "nnkiboze: LIST NEWSGROUPS is not implemented.")
188   nil)
189
190 (defalias 'nnkiboze-request-post 'nntp-request-post)
191
192 \f
193 ;;; Internal functions.
194
195 (defun nnkiboze-possibly-change-newsgroups (group)
196   (setq nnkiboze-current-group group))
197
198 (defun nnkiboze-prefixed-name (group)
199   (gnus-group-prefixed-name group '(nnkiboze "")))
200
201 ;;;###autoload
202 (defun nnkiboze-generate-groups ()
203   "Usage: emacs -batch -l nnkiboze -f nnkiboze-generate-groups
204 Finds out what articles are to be part of the nnkiboze groups."
205   (interactive)
206   (let ((nnmail-spool-file nil)
207         (gnus-use-dribble-file nil)
208         (gnus-read-active-file t)
209         (gnus-expert-user t))
210     (gnus))
211   (let* ((gnus-newsrc-alist (gnus-copy-sequence gnus-newsrc-alist))
212          (newsrc gnus-newsrc-alist)
213          gnus-newsrc-hashtb)
214     (gnus-make-hashtable-from-newsrc-alist)
215     ;; We have copied all the newsrc alist info over to local copies
216     ;; so that we can mess all we want with these lists.
217     (while newsrc
218       (if (string-match "nnkiboze" (car (car newsrc)))
219           ;; For each kiboze group, we call this function to generate
220           ;; it.  
221           (nnkiboze-generate-group (car (car newsrc))))
222       (setq newsrc (cdr newsrc)))))
223
224 (defun nnkiboze-score-file (group)
225   (list (expand-file-name
226          (concat gnus-kill-files-directory nnkiboze-current-score-group 
227                  "." gnus-score-file-suffix))))
228
229 (defun nnkiboze-generate-group (group) 
230   (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
231          (newsrc-file (concat nnkiboze-directory group ".newsrc"))
232          (nov-file (concat nnkiboze-directory group ".nov"))
233          (regexp (nth 1 (nth 4 info)))
234          (gnus-expert-user t)
235          (gnus-large-newsgroup nil)
236          (gnus-score-find-score-files-function 'nnkiboze-score-file)
237          gnus-select-group-hook gnus-summary-prepare-hook 
238          gnus-thread-sort-functions gnus-show-threads 
239          gnus-visual
240          method nnkiboze-newsrc nov-buffer gname newsrc active
241          ginfo lowest glevel)
242     (setq nnkiboze-current-score-group group)
243     (or info (error "No such group: %s" group))
244     ;; Load the kiboze newsrc file for this group.
245     (and (file-exists-p newsrc-file) (load newsrc-file))
246     ;; We also load the nov file for this group.
247     (save-excursion
248       (set-buffer (setq nov-buffer (find-file-noselect nov-file)))
249       (buffer-disable-undo (current-buffer)))
250     ;; Go through the active hashtb and add new all groups that match the 
251     ;; kiboze regexp.
252     (mapatoms
253      (lambda (group)
254        (and (string-match regexp (setq gname (symbol-name group))) ; Match
255             (not (assoc gname nnkiboze-newsrc)) ; It isn't registered
256             (numberp (car (symbol-value group))) ; It is active
257             (or (> nnkiboze-level 7)
258                 (and (setq glevel (nth 1 (nth 2 (gnus-gethash
259                                                  gname gnus-newsrc-hashtb))))
260                      (>= nnkiboze-level glevel)))
261             (not (string-match "^nnkiboze:" gname)) ; Exclude kibozes
262             (setq nnkiboze-newsrc 
263                   (cons (cons gname (1- (car (symbol-value group))))
264                         nnkiboze-newsrc))))
265      gnus-active-hashtb)
266     ;; `newsrc' is set to the list of groups that possibly are
267     ;; component groups to this kiboze group.  This list has elements
268     ;; on the form `(GROUP . NUMBER)', where NUMBER is the highest
269     ;; number that has been kibozed in GROUP in this kiboze group.
270     (setq newsrc nnkiboze-newsrc)
271     (while newsrc
272       (if (not (setq active (gnus-gethash 
273                              (car (car newsrc)) gnus-active-hashtb)))
274           ;; This group isn't active after all, so we remove it from
275           ;; the list of component groups.
276           (setq nnkiboze-newsrc (delq (car newsrc) nnkiboze-newsrc))
277         (setq lowest (cdr (car newsrc)))
278         ;; Ok, we have a valid component group, so we jump to it. 
279         (switch-to-buffer gnus-group-buffer)
280         (gnus-group-jump-to-group (car (car newsrc)))
281         ;; We set all list of article marks to nil.  Since we operate
282         ;; on copies of the real lists, we can destroy anything we
283         ;; want here.
284         (and (setq ginfo (nth 2 (gnus-gethash (gnus-group-group-name)
285                                               gnus-newsrc-hashtb)))
286              (nth 3 ginfo)
287              (setcar (nthcdr 3 ginfo) nil))
288         ;; We set the list of read articles to be what we expect for
289         ;; this kiboze group -- either nil or `(1 . LOWEST)'. 
290         (and ginfo (setcar (nthcdr 2 ginfo)
291                            (and (not (= lowest 1)) (cons 1 lowest))))
292         (if (not (and (or (not ginfo)
293                           (> (length (gnus-list-of-unread-articles 
294                                       (car ginfo))) 0))
295                       (progn
296                         (gnus-group-select-group nil)
297                         (eq major-mode 'gnus-summary-mode))))
298             () ; No unread articles, or we couldn't enter this group.
299           ;; We are now in the group where we want to be.
300           (setq method (gnus-find-method-for-group gnus-newsgroup-name))
301           (and (eq method gnus-select-method) (setq method nil))
302           ;; We go through the list of scored articles.
303           (while gnus-newsgroup-scored
304             (if (> (car (car gnus-newsgroup-scored)) lowest)
305                 ;; If it has a good score, then we enter this article
306                 ;; into the kiboze group.
307                 (nnkiboze-enter-nov 
308                  nov-buffer
309                  (gnus-summary-article-header 
310                   (car (car gnus-newsgroup-scored)))
311                  (if method
312                      (gnus-group-prefixed-name gnus-newsgroup-name method)
313                    gnus-newsgroup-name)))
314             (setq gnus-newsgroup-scored (cdr gnus-newsgroup-scored)))
315           ;; That's it.  We exit this group.
316           (gnus-summary-exit-no-update)))
317       (setcdr (car newsrc) (car active))
318       (setq newsrc (cdr newsrc)))
319     ;; We save the nov file.
320     (set-buffer nov-buffer)
321     (save-buffer)
322     (kill-buffer (current-buffer))
323     ;; We save the kiboze newsrc for this group.
324     (set-buffer (get-buffer-create "*nnkiboze work*"))
325     (buffer-disable-undo (current-buffer))
326     (erase-buffer)
327     (insert "(setq nnkiboze-newsrc '" (prin1-to-string nnkiboze-newsrc)
328             ")\n")
329     (write-file newsrc-file)
330     (kill-buffer (current-buffer))
331     (switch-to-buffer gnus-group-buffer)
332     (gnus-group-list-groups 5 nil)))
333     
334 (defun nnkiboze-enter-nov (buffer header group)
335   (save-excursion
336     (set-buffer buffer)
337     (goto-char (point-max))
338     (let ((xref (mail-header-xref header))
339           (prefix (gnus-group-real-prefix group))
340           (first t)
341           article)
342       (if (zerop (forward-line -1))
343           (progn
344             (setq article (1+ (read (current-buffer))))
345             (forward-line 1))
346         (setq article 1))
347       (insert (int-to-string article) "\t"
348               (or (mail-header-subject header) "") "\t"
349               (or (mail-header-from header) "") "\t"
350               (or (mail-header-date header) "") "\t"
351               (or (mail-header-id header) "") "\t"
352               (or (mail-header-references header) "") "\t"
353               (int-to-string (or (mail-header-chars header) 0)) "\t"
354               (int-to-string (or (mail-header-lines header) 0)) "\t")
355       (if (or (not xref) (equal "" xref))
356           (insert "Xref: " (system-name) " " group ":" 
357                   (int-to-string (mail-header-number header))
358                   "\t\n")
359         (insert (mail-header-xref header) "\t\n")
360         (search-backward "\t" nil t)
361         (search-backward "\t" nil t)
362         (while (re-search-forward 
363                 "[^ ]+:[0-9]+"
364                 (save-excursion (end-of-line) (point)) t)
365           (if first
366               ;; The first xref has to be the group this article
367               ;; really came for - this is the article nnkiboze
368               ;; will request when it is asked for the article.
369               (save-excursion
370                 (goto-char (match-beginning 0))
371                 (insert prefix group ":" 
372                         (int-to-string (mail-header-number header)) " ")
373                 (setq first nil)))
374           (save-excursion
375             (goto-char (match-beginning 0))
376             (insert prefix)))))))
377
378 (defun nnkiboze-nov-file-name ()
379   (concat nnkiboze-directory
380           (nnkiboze-prefixed-name nnkiboze-current-group) ".nov"))
381
382 (provide 'nnkiboze)
383
384 ;;; nnkiboze.el ends here