681b0067625368273e8684d276a84820067b4a68
[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             (version-control 'never))
161         (set-buffer (get-buffer-create "*nnkiboze work*"))
162         (buffer-disable-undo (current-buffer))
163         (erase-buffer)
164         (let ((cur (current-buffer))
165               article)
166           (insert-file-contents (nnkiboze-nov-file-name))
167           (goto-char (point-min))
168           (while (looking-at "[0-9]+")
169             (if (or (memq (setq article (read cur)) unreads)
170                     (memq article unselected))
171                 (forward-line 1)
172               (delete-region (progn (beginning-of-line) (point))
173                              (progn (forward-line 1) (point)))))
174           (write-file (nnkiboze-nov-file-name))
175           (kill-buffer (current-buffer)))))
176     (setq nnkiboze-current-group nil)))
177
178 (defun nnkiboze-request-list (&optional server) 
179   (setq nnkiboze-status-string "nnkiboze: LIST is not implemented.")
180   nil)
181
182 (defun nnkiboze-request-newgroups (date &optional server)
183   "List new groups."
184   (setq nnkiboze-status-string "NEWGROUPS is not supported.")
185   nil)
186
187 (defun nnkiboze-request-list-newsgroups (&optional server)
188   (setq nnkiboze-status-string "nnkiboze: LIST NEWSGROUPS is not implemented.")
189   nil)
190
191 (defalias 'nnkiboze-request-post 'nntp-request-post)
192
193 \f
194 ;;; Internal functions.
195
196 (defun nnkiboze-possibly-change-newsgroups (group)
197   (setq nnkiboze-current-group group))
198
199 (defun nnkiboze-prefixed-name (group)
200   (gnus-group-prefixed-name group '(nnkiboze "")))
201
202 ;;;###autoload
203 (defun nnkiboze-generate-groups ()
204   "Usage: emacs -batch -l nnkiboze -f nnkiboze-generate-groups
205 Finds out what articles are to be part of the nnkiboze groups."
206   (interactive)
207   (let ((nnmail-spool-file nil)
208         (gnus-use-dribble-file nil)
209         (gnus-read-active-file t)
210         (gnus-expert-user t))
211     (gnus))
212   (let* ((gnus-newsrc-alist (gnus-copy-sequence gnus-newsrc-alist))
213          (newsrc gnus-newsrc-alist)
214          gnus-newsrc-hashtb)
215     (gnus-make-hashtable-from-newsrc-alist)
216     ;; We have copied all the newsrc alist info over to local copies
217     ;; so that we can mess all we want with these lists.
218     (while newsrc
219       (if (string-match "nnkiboze" (car (car newsrc)))
220           ;; For each kiboze group, we call this function to generate
221           ;; it.  
222           (nnkiboze-generate-group (car (car newsrc))))
223       (setq newsrc (cdr newsrc)))))
224
225 (defun nnkiboze-score-file (group)
226   (list (expand-file-name
227          (concat gnus-kill-files-directory nnkiboze-current-score-group 
228                  "." gnus-score-file-suffix))))
229
230 (defun nnkiboze-generate-group (group) 
231   (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
232          (newsrc-file (concat nnkiboze-directory group ".newsrc"))
233          (nov-file (concat nnkiboze-directory group ".nov"))
234          (regexp (nth 1 (nth 4 info)))
235          (gnus-expert-user t)
236          (gnus-large-newsgroup nil)
237          (version-control 'never)
238          (gnus-score-find-score-files-function 'nnkiboze-score-file)
239          gnus-select-group-hook gnus-summary-prepare-hook 
240          gnus-thread-sort-functions gnus-show-threads 
241          gnus-visual
242          method nnkiboze-newsrc nov-buffer gname newsrc active
243          ginfo lowest glevel)
244     (setq nnkiboze-current-score-group group)
245     (or info (error "No such group: %s" group))
246     ;; Load the kiboze newsrc file for this group.
247     (and (file-exists-p newsrc-file) (load newsrc-file))
248     ;; We also load the nov file for this group.
249     (save-excursion
250       (set-buffer (setq nov-buffer (find-file-noselect nov-file)))
251       (buffer-disable-undo (current-buffer)))
252     ;; Go through the active hashtb and add new all groups that match the 
253     ;; kiboze regexp.
254     (mapatoms
255      (lambda (group)
256        (and (string-match regexp (setq gname (symbol-name group))) ; Match
257             (not (assoc gname nnkiboze-newsrc)) ; It isn't registered
258             (numberp (car (symbol-value group))) ; It is active
259             (or (> nnkiboze-level 7)
260                 (and (setq glevel (nth 1 (nth 2 (gnus-gethash
261                                                  gname gnus-newsrc-hashtb))))
262                      (>= nnkiboze-level glevel)))
263             (not (string-match "^nnkiboze:" gname)) ; Exclude kibozes
264             (setq nnkiboze-newsrc 
265                   (cons (cons gname (1- (car (symbol-value group))))
266                         nnkiboze-newsrc))))
267      gnus-active-hashtb)
268     ;; `newsrc' is set to the list of groups that possibly are
269     ;; component groups to this kiboze group.  This list has elements
270     ;; on the form `(GROUP . NUMBER)', where NUMBER is the highest
271     ;; number that has been kibozed in GROUP in this kiboze group.
272     (setq newsrc nnkiboze-newsrc)
273     (while newsrc
274       (if (not (setq active (gnus-gethash 
275                              (car (car newsrc)) gnus-active-hashtb)))
276           ;; This group isn't active after all, so we remove it from
277           ;; the list of component groups.
278           (setq nnkiboze-newsrc (delq (car newsrc) nnkiboze-newsrc))
279         (setq lowest (cdr (car newsrc)))
280         ;; Ok, we have a valid component group, so we jump to it. 
281         (switch-to-buffer gnus-group-buffer)
282         (gnus-group-jump-to-group (car (car newsrc)))
283         ;; We set all list of article marks to nil.  Since we operate
284         ;; on copies of the real lists, we can destroy anything we
285         ;; want here.
286         (and (setq ginfo (nth 2 (gnus-gethash (gnus-group-group-name)
287                                               gnus-newsrc-hashtb)))
288              (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         (and ginfo (setcar (nthcdr 2 ginfo)
293                            (and (not (= lowest 1)) (cons 1 lowest))))
294         (if (not (and (or (not ginfo)
295                           (> (length (gnus-list-of-unread-articles 
296                                       (car ginfo))) 0))
297                       (progn
298                         (gnus-group-select-group nil)
299                         (eq major-mode 'gnus-summary-mode))))
300             () ; No unread articles, or we couldn't enter this group.
301           ;; We are now in the group where we want to be.
302           (setq method (gnus-find-method-for-group gnus-newsgroup-name))
303           (and (eq method gnus-select-method) (setq method nil))
304           ;; We go through the list of scored articles.
305           (while gnus-newsgroup-scored
306             (if (> (car (car gnus-newsgroup-scored)) lowest)
307                 ;; If it has a good score, then we enter this article
308                 ;; into the kiboze group.
309                 (nnkiboze-enter-nov 
310                  nov-buffer
311                  (gnus-summary-article-header 
312                   (car (car gnus-newsgroup-scored)))
313                  (if method
314                      (gnus-group-prefixed-name gnus-newsgroup-name method)
315                    gnus-newsgroup-name)))
316             (setq gnus-newsgroup-scored (cdr gnus-newsgroup-scored)))
317           ;; That's it.  We exit this group.
318           (gnus-summary-exit-no-update)))
319       (setcdr (car newsrc) (car active))
320       (setq newsrc (cdr newsrc)))
321     ;; We save the nov file.
322     (set-buffer nov-buffer)
323     (save-buffer)
324     (kill-buffer (current-buffer))
325     ;; We save the kiboze newsrc for this group.
326     (set-buffer (get-buffer-create "*nnkiboze work*"))
327     (buffer-disable-undo (current-buffer))
328     (erase-buffer)
329     (insert "(setq nnkiboze-newsrc '" (prin1-to-string nnkiboze-newsrc)
330             ")\n")
331     (write-file newsrc-file)
332     (kill-buffer (current-buffer))
333     (switch-to-buffer gnus-group-buffer)
334     (gnus-group-list-groups 5 nil)))
335     
336 (defun nnkiboze-enter-nov (buffer header group)
337   (save-excursion
338     (set-buffer buffer)
339     (goto-char (point-max))
340     (let ((xref (mail-header-xref header))
341           (prefix (gnus-group-real-prefix group))
342           (first t)
343           article)
344       (if (zerop (forward-line -1))
345           (progn
346             (setq article (1+ (read (current-buffer))))
347             (forward-line 1))
348         (setq article 1))
349       (insert (int-to-string article) "\t"
350               (or (mail-header-subject header) "") "\t"
351               (or (mail-header-from header) "") "\t"
352               (or (mail-header-date header) "") "\t"
353               (or (mail-header-id header) "") "\t"
354               (or (mail-header-references header) "") "\t"
355               (int-to-string (or (mail-header-chars header) 0)) "\t"
356               (int-to-string (or (mail-header-lines header) 0)) "\t")
357       (if (or (not xref) (equal "" xref))
358           (insert "Xref: " (system-name) " " group ":" 
359                   (int-to-string (mail-header-number header))
360                   "\t\n")
361         (insert (mail-header-xref header) "\t\n")
362         (search-backward "\t" nil t)
363         (search-backward "\t" nil t)
364         (while (re-search-forward 
365                 "[^ ]+:[0-9]+"
366                 (save-excursion (end-of-line) (point)) t)
367           (if first
368               ;; The first xref has to be the group this article
369               ;; really came for - this is the article nnkiboze
370               ;; will request when it is asked for the article.
371               (save-excursion
372                 (goto-char (match-beginning 0))
373                 (insert prefix group ":" 
374                         (int-to-string (mail-header-number header)) " ")
375                 (setq first nil)))
376           (save-excursion
377             (goto-char (match-beginning 0))
378             (insert prefix)))))))
379
380 (defun nnkiboze-nov-file-name ()
381   (concat nnkiboze-directory
382           (nnkiboze-prefixed-name nnkiboze-current-group) ".nov"))
383
384 (provide 'nnkiboze)
385
386 ;;; nnkiboze.el ends here