13401
|
1 ;;; nnkiboze.el --- select virtual news access for Gnus
|
14169
|
2
|
13401
|
3 ;; Copyright (C) 1995 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
|
|
6 ;; Keywords: news
|
|
7
|
|
8 ;; This file is part of GNU Emacs.
|
|
9
|
|
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
11 ;; it under the terms of the GNU General Public License as published by
|
|
12 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
13 ;; any later version.
|
|
14
|
|
15 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18 ;; GNU General Public License for more details.
|
|
19
|
|
20 ;; You should have received a copy of the GNU General Public License
|
14169
|
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
23 ;; Boston, MA 02111-1307, USA.
|
13401
|
24
|
|
25 ;;; Commentary:
|
|
26
|
|
27 ;; The other access methods (nntp, nnspool, etc) are general news
|
|
28 ;; access methods. This module relies on Gnus and can not be used
|
|
29 ;; separately.
|
|
30
|
|
31 ;;; Code:
|
|
32
|
|
33 (require 'nntp)
|
|
34 (require 'nnheader)
|
|
35 (require 'gnus)
|
|
36 (require 'gnus-score)
|
|
37
|
|
38 (defvar nnkiboze-directory
|
|
39 (expand-file-name (or gnus-article-save-directory "~/News/"))
|
|
40 "nnkiboze will put its files in this directory.")
|
|
41
|
|
42
|
|
43
|
|
44 (defconst nnkiboze-version "nnkiboze 1.0"
|
|
45 "Version numbers of this version of nnkiboze.")
|
|
46
|
|
47 (defvar nnkiboze-current-group nil)
|
|
48 (defvar nnkiboze-current-score-group "")
|
|
49 (defvar nnkiboze-status-string "")
|
|
50
|
|
51
|
|
52
|
|
53 ;;; Interface functions.
|
|
54
|
|
55 (defun nnkiboze-retrieve-headers (articles &optional group server)
|
|
56 (nnkiboze-possibly-change-newsgroups group)
|
|
57 (if gnus-nov-is-evil
|
|
58 nil
|
|
59 (if (stringp (car articles))
|
|
60 'headers
|
|
61 (let ((first (car articles))
|
|
62 (last (progn (while (cdr articles) (setq articles (cdr articles)))
|
|
63 (car articles)))
|
|
64 (nov (nnkiboze-nov-file-name)))
|
|
65 (if (file-exists-p nov)
|
|
66 (save-excursion
|
|
67 (set-buffer nntp-server-buffer)
|
|
68 (erase-buffer)
|
|
69 (insert-file-contents nov)
|
|
70 (goto-char (point-min))
|
|
71 (while (and (not (eobp)) (< first (read (current-buffer))))
|
|
72 (forward-line 1))
|
|
73 (beginning-of-line)
|
|
74 (if (not (eobp)) (delete-region 1 (point)))
|
|
75 (while (and (not (eobp)) (>= last (read (current-buffer))))
|
|
76 (forward-line 1))
|
|
77 (beginning-of-line)
|
|
78 (if (not (eobp)) (delete-region (point) (point-max)))
|
|
79 'nov))))))
|
|
80
|
|
81 (defun nnkiboze-open-server (newsgroups &optional something)
|
|
82 "Open a virtual newsgroup that contains NEWSGROUPS."
|
|
83 (gnus-make-directory nnkiboze-directory)
|
|
84 (nnheader-init-server-buffer))
|
|
85
|
|
86 (defun nnkiboze-close-server (&rest dum)
|
|
87 "Close news server."
|
|
88 t)
|
|
89
|
|
90 (defalias 'nnkiboze-request-quit (symbol-function 'nnkiboze-close-server))
|
|
91
|
|
92 (defun nnkiboze-server-opened (&optional server)
|
|
93 "Return server process status, T or NIL.
|
|
94 If the stream is opened, return T, otherwise return NIL."
|
|
95 (and nntp-server-buffer
|
|
96 (get-buffer nntp-server-buffer)))
|
|
97
|
|
98 (defun nnkiboze-status-message (&optional server)
|
|
99 "Return server status response as string."
|
|
100 nnkiboze-status-string)
|
|
101
|
|
102 (defun nnkiboze-request-article (article &optional newsgroup server buffer)
|
|
103 "Select article by message number."
|
|
104 (nnkiboze-possibly-change-newsgroups newsgroup)
|
|
105 (if (not (numberp article))
|
14040
|
106 ;; This is a real kludge. It might not work at times, but it
|
13401
|
107 ;; does no harm I think. The only alternative is to offer no
|
|
108 ;; article fetching by message-id at all.
|
|
109 (nntp-request-article article newsgroup gnus-nntp-server buffer)
|
|
110 (let* ((header (gnus-get-header-by-number article))
|
|
111 (xref (mail-header-xref header))
|
|
112 igroup iarticle)
|
|
113 (or xref (error "nnkiboze: No xref"))
|
|
114 (or (string-match " \\([^ ]+\\):\\([0-9]+\\)" xref)
|
|
115 (error "nnkiboze: Malformed xref"))
|
|
116 (setq igroup (substring xref (match-beginning 1) (match-end 1)))
|
|
117 (setq iarticle (string-to-int
|
|
118 (substring xref (match-beginning 2) (match-end 2))))
|
|
119 (and (gnus-request-group igroup t)
|
|
120 (gnus-request-article iarticle igroup buffer)))))
|
|
121
|
|
122 (defun nnkiboze-request-group (group &optional server dont-check)
|
|
123 "Make GROUP the current newsgroup."
|
|
124 (nnkiboze-possibly-change-newsgroups group)
|
|
125 (if dont-check
|
|
126 ()
|
|
127 (let ((nov-file (nnkiboze-nov-file-name))
|
|
128 beg end total)
|
|
129 (save-excursion
|
|
130 (set-buffer nntp-server-buffer)
|
|
131 (erase-buffer)
|
|
132 (if (not (file-exists-p nov-file))
|
|
133 (insert (format "211 0 0 0 %s\n" group))
|
|
134 (insert-file-contents nov-file)
|
|
135 (if (zerop (buffer-size))
|
|
136 (insert (format "211 0 0 0 %s\n" group))
|
|
137 (goto-char (point-min))
|
|
138 (and (looking-at "[0-9]+") (setq beg (read (current-buffer))))
|
|
139 (goto-char (point-max))
|
|
140 (and (re-search-backward "^[0-9]" nil t)
|
|
141 (setq end (read (current-buffer))))
|
|
142 (setq total (count-lines (point-min) (point-max)))
|
|
143 (erase-buffer)
|
|
144 (insert (format "211 %d %d %d %s\n" total beg end group)))))))
|
|
145 t)
|
|
146
|
|
147 (defun nnkiboze-close-group (group &optional server)
|
|
148 (nnkiboze-possibly-change-newsgroups group)
|
|
149 ;; Remove NOV lines of articles that are marked as read.
|
|
150 (if (not (file-exists-p (nnkiboze-nov-file-name)))
|
|
151 ()
|
|
152 (save-excursion
|
|
153 (let ((unreads gnus-newsgroup-unreads)
|
|
154 (unselected gnus-newsgroup-unselected))
|
|
155 (set-buffer (get-buffer-create "*nnkiboze work*"))
|
|
156 (buffer-disable-undo (current-buffer))
|
|
157 (erase-buffer)
|
|
158 (let ((cur (current-buffer))
|
|
159 article)
|
|
160 (insert-file-contents (nnkiboze-nov-file-name))
|
|
161 (goto-char (point-min))
|
|
162 (while (looking-at "[0-9]+")
|
|
163 (if (or (memq (setq article (read cur)) unreads)
|
|
164 (memq article unselected))
|
|
165 (forward-line 1)
|
|
166 (delete-region (progn (beginning-of-line) (point))
|
|
167 (progn (forward-line 1) (point)))))
|
|
168 (write-file (nnkiboze-nov-file-name))
|
|
169 (kill-buffer (current-buffer)))))
|
|
170 (setq nnkiboze-current-group nil)))
|
|
171
|
|
172 (defun nnkiboze-request-list (&optional server)
|
|
173 (setq nnkiboze-status-string "nnkiboze: LIST is not implemented.")
|
|
174 nil)
|
|
175
|
|
176 (defun nnkiboze-request-newgroups (date &optional server)
|
|
177 "List new groups."
|
|
178 (setq nnkiboze-status-string "NEWGROUPS is not supported.")
|
|
179 nil)
|
|
180
|
|
181 (defun nnkiboze-request-list-newsgroups (&optional server)
|
|
182 (setq nnkiboze-status-string "nnkiboze: LIST NEWSGROUPS is not implemented.")
|
|
183 nil)
|
|
184
|
|
185 (defalias 'nnkiboze-request-post 'nntp-request-post)
|
|
186
|
|
187 (defalias 'nnkiboze-request-post-buffer 'nntp-request-post-buffer)
|
|
188
|
|
189
|
|
190 ;;; Internal functions.
|
|
191
|
|
192 (defun nnkiboze-possibly-change-newsgroups (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 (gnus-use-dribble-file nil)
|
|
205 (gnus-read-active-file t)
|
|
206 (gnus-expert-user t))
|
|
207 (gnus))
|
|
208 (let* ((gnus-newsrc-alist (gnus-copy-sequence gnus-newsrc-alist))
|
|
209 (newsrc gnus-newsrc-alist))
|
|
210 (while newsrc
|
|
211 (if (string-match "nnkiboze" (car (car newsrc)))
|
|
212 (nnkiboze-generate-group (car (car newsrc))))
|
|
213 (setq newsrc (cdr newsrc)))))
|
|
214
|
|
215 (defun nnkiboze-score-file (group)
|
|
216 (list (expand-file-name
|
|
217 (concat gnus-kill-files-directory nnkiboze-current-score-group
|
|
218 "." gnus-score-file-suffix))))
|
|
219
|
|
220 (defun nnkiboze-generate-group (group)
|
|
221 (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
|
|
222 (newsrc-file (concat nnkiboze-directory group ".newsrc"))
|
|
223 (nov-file (concat nnkiboze-directory group ".nov"))
|
|
224 (regexp (nth 1 (nth 4 info)))
|
|
225 (gnus-expert-user t)
|
|
226 (gnus-large-newsgroup nil)
|
|
227 (gnus-score-find-score-files-function 'nnkiboze-score-file)
|
|
228 gnus-select-group-hook gnus-summary-prepare-hook
|
|
229 gnus-thread-sort-functions gnus-show-threads
|
|
230 gnus-visual
|
|
231 method nnkiboze-newsrc nov-buffer gname newsrc active
|
|
232 ginfo lowest)
|
|
233 (setq nnkiboze-current-score-group group)
|
|
234 (or info (error "No such group: %s" group))
|
|
235 (and (file-exists-p newsrc-file) (load newsrc-file))
|
|
236 (save-excursion
|
|
237 (set-buffer (setq nov-buffer (find-file-noselect nov-file)))
|
|
238 (buffer-disable-undo (current-buffer)))
|
|
239 ;; Go through the active hashtb and add new all groups that match the
|
|
240 ;; kiboze regexp.
|
|
241 (mapatoms
|
|
242 (lambda (group)
|
|
243 (if (and (string-match regexp (setq gname (symbol-name group))) ; Match
|
|
244 (not (assoc gname nnkiboze-newsrc)) ; It isn't registered
|
|
245 (numberp (car (symbol-value group))) ; It is active
|
|
246 (not (string-match "^nnkiboze:" gname))) ; Exclude kibozes
|
|
247 (setq nnkiboze-newsrc
|
|
248 (cons (cons gname (1- (car (symbol-value group))))
|
|
249 nnkiboze-newsrc))))
|
|
250 gnus-active-hashtb)
|
|
251 (setq newsrc nnkiboze-newsrc)
|
|
252 (while newsrc
|
|
253 (if (not (setq active (gnus-gethash
|
|
254 (car (car newsrc)) gnus-active-hashtb)))
|
|
255 (setq nnkiboze-newsrc (delq (car newsrc) nnkiboze-newsrc))
|
|
256 (switch-to-buffer gnus-group-buffer)
|
|
257 (gnus-group-jump-to-group (car (car newsrc)))
|
|
258 (if (and (setq ginfo (nth 2 (gnus-gethash (gnus-group-group-name)
|
|
259 gnus-newsrc-hashtb)))
|
|
260 (nth 3 ginfo))
|
|
261 (setcar (nthcdr 3 ginfo) nil))
|
|
262 (if (not (and (or (not ginfo)
|
|
263 (> (length (gnus-list-of-unread-articles
|
|
264 (car ginfo))) 0))
|
|
265 (progn
|
|
266 (gnus-group-select-group nil)
|
|
267 (eq major-mode 'gnus-summary-mode))))
|
|
268 ()
|
|
269 (setq lowest (cdr (car newsrc)))
|
|
270 (setq method (gnus-find-method-for-group gnus-newsgroup-name))
|
|
271 (and (eq method gnus-select-method) (setq method nil))
|
|
272 (while gnus-newsgroup-scored
|
|
273 (if (> (car (car gnus-newsgroup-scored)) lowest)
|
|
274 (nnkiboze-enter-nov
|
|
275 nov-buffer
|
|
276 (gnus-get-header-by-number (car (car gnus-newsgroup-scored)))
|
|
277 (if method
|
|
278 (gnus-group-prefixed-name gnus-newsgroup-name method)
|
|
279 gnus-newsgroup-name)))
|
|
280 (setq gnus-newsgroup-scored (cdr gnus-newsgroup-scored)))
|
|
281 (gnus-summary-quit)))
|
|
282 (setcdr (car newsrc) (car active))
|
|
283 (setq newsrc (cdr newsrc)))
|
|
284 (set-buffer nov-buffer)
|
|
285 (save-buffer)
|
|
286 (kill-buffer (current-buffer))
|
|
287 (set-buffer (get-buffer-create "*nnkiboze work*"))
|
|
288 (buffer-disable-undo (current-buffer))
|
|
289 (erase-buffer)
|
|
290 (insert "(setq nnkiboze-newsrc '" (prin1-to-string nnkiboze-newsrc)
|
|
291 ")\n")
|
|
292 (write-file newsrc-file)
|
|
293 (kill-buffer (current-buffer))
|
|
294 (switch-to-buffer gnus-group-buffer)
|
|
295 (gnus-group-list-groups 5 nil)))
|
|
296
|
|
297 (defun nnkiboze-enter-nov (buffer header group)
|
|
298 (save-excursion
|
|
299 (set-buffer buffer)
|
|
300 (goto-char (point-max))
|
|
301 (let ((xref (mail-header-xref header))
|
|
302 (prefix (gnus-group-real-prefix group))
|
|
303 (first t)
|
|
304 article)
|
|
305 (if (zerop (forward-line -1))
|
|
306 (progn
|
|
307 (setq article (1+ (read (current-buffer))))
|
|
308 (forward-line 1))
|
|
309 (setq article 1))
|
|
310 (insert (int-to-string article) "\t"
|
|
311 (or (mail-header-subject header) "") "\t"
|
|
312 (or (mail-header-from header) "") "\t"
|
|
313 (or (mail-header-date header) "") "\t"
|
|
314 (or (mail-header-id header) "") "\t"
|
|
315 (or (mail-header-references header) "") "\t"
|
|
316 (int-to-string (or (mail-header-chars header) 0)) "\t"
|
|
317 (int-to-string (or (mail-header-lines header) 0)) "\t")
|
|
318 (if (or (not xref) (equal "" xref))
|
|
319 (insert "Xref: " (system-name) " " group ":"
|
|
320 (int-to-string (mail-header-number header))
|
|
321 "\t\n")
|
|
322 (insert (mail-header-xref header) "\t\n")
|
|
323 (search-backward "\t" nil t)
|
|
324 (search-backward "\t" nil t)
|
|
325 (while (re-search-forward
|
|
326 "[^ ]+:[0-9]+"
|
|
327 (save-excursion (end-of-line) (point)) t)
|
|
328 (if first
|
|
329 ;; The first xref has to be the group this article
|
|
330 ;; really came for - this is the article nnkiboze
|
|
331 ;; will request when it is asked for the article.
|
|
332 (save-excursion
|
|
333 (goto-char (match-beginning 0))
|
|
334 (insert prefix group ":"
|
|
335 (int-to-string (mail-header-number header)) " ")
|
|
336 (setq first nil)))
|
|
337 (save-excursion
|
|
338 (goto-char (match-beginning 0))
|
|
339 (insert prefix)))))))
|
|
340
|
|
341 (defun nnkiboze-nov-file-name ()
|
|
342 (concat nnkiboze-directory
|
|
343 (nnkiboze-prefixed-name nnkiboze-current-group) ".nov"))
|
|
344
|
|
345 (provide 'nnkiboze)
|
|
346
|
|
347 ;;; nnkiboze.el ends here
|