1 ;;; nndoc.el --- single file access for Gnus
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
8 ;; This file is part of GNU Emacs.
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)
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
32 (defvar nndoc-article-type 'mbox
33 "*Type of the file - one of `mbox', `babyl' or `digest'.")
35 (defconst nndoc-type-to-regexp
37 (concat "^" rmail-unix-mail-delimiter)
38 (concat "^" rmail-unix-mail-delimiter)
40 (list 'babyl "\^_\^L *\n" "\^_" nil "^$" nil nil
41 "\\*\\*\\* EOOH \\*\\*\\*\n\\(^.+\n\\)*")
43 "^------------------------------*[\n \t]+"
44 "^------------------------------[\n \t]+"
46 "^------------------------------*[\n \t]+"
48 "Regular expressions for articles of the various types.")
52 (defvar nndoc-article-begin nil)
53 (defvar nndoc-article-end nil)
54 (defvar nndoc-head-begin nil)
55 (defvar nndoc-head-end nil)
56 (defvar nndoc-first-article nil)
57 (defvar nndoc-end-of-file nil)
58 (defvar nndoc-body-begin nil)
60 (defvar nndoc-current-server nil)
61 (defvar nndoc-server-alist nil)
62 (defvar nndoc-server-variables
64 (list 'nndoc-article-type nndoc-article-type)
65 '(nndoc-article-begin nil)
66 '(nndoc-article-end nil)
67 '(nndoc-head-begin nil)
69 '(nndoc-first-article nil)
70 '(nndoc-current-buffer nil)
71 '(nndoc-group-alist nil)
72 '(nndoc-end-of-file nil)
73 '(nndoc-body-begin nil)
74 '(nndoc-address nil)))
76 (defconst nndoc-version "nndoc 0.1"
79 (defvar nndoc-current-buffer nil
80 "Current nndoc news buffer.")
82 (defvar nndoc-address nil)
86 (defvar nndoc-status-string "")
88 (defvar nndoc-group-alist nil)
90 ;;; Interface functions
92 (defun nndoc-retrieve-headers (sequence &optional newsgroup server)
94 (set-buffer nntp-server-buffer)
98 (nndoc-possibly-change-buffer newsgroup server)
99 (if (stringp (car sequence))
101 (set-buffer nndoc-current-buffer)
102 (goto-char (point-min))
103 (re-search-forward (or nndoc-first-article
104 nndoc-article-begin) nil t)
105 (or (not nndoc-head-begin)
106 (re-search-forward nndoc-head-begin nil t))
107 (re-search-forward nndoc-head-end nil t)
109 (setq article (car sequence))
110 (set-buffer nndoc-current-buffer)
111 (if (not (nndoc-forward-article (max 0 (- article prev))))
115 (re-search-backward nndoc-article-begin nil t)
119 (setq lines (count-lines
122 (and (re-search-forward nndoc-article-end nil t)
123 (goto-char (match-beginning 0)))
124 (goto-char (point-max)))))
126 (set-buffer nntp-server-buffer)
127 (insert (format "221 %d Article retrieved.\n" article))
128 (insert-buffer-substring nndoc-current-buffer beg p)
129 (goto-char (point-max))
130 (insert (format "Lines: %d\n" lines))
134 sequence (cdr sequence)))
136 ;; Fold continuation lines.
137 (goto-char (point-min))
138 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
139 (replace-match " " t t))
142 (defun nndoc-open-server (server &optional defs)
143 (nnheader-init-server-buffer)
144 (if (equal server nndoc-current-server)
146 (if nndoc-current-server
147 (setq nndoc-server-alist
148 (cons (list nndoc-current-server
149 (nnheader-save-variables nndoc-server-variables))
150 nndoc-server-alist)))
151 (let ((state (assoc server nndoc-server-alist)))
154 (nnheader-restore-variables (nth 1 state))
155 (setq nndoc-server-alist (delq state nndoc-server-alist)))
156 (nnheader-set-init-variables nndoc-server-variables defs)))
157 (setq nndoc-current-server server)
158 (let ((defs (cdr (assq nndoc-article-type nndoc-type-to-regexp))))
159 (setq nndoc-article-begin (nth 0 defs))
160 (setq nndoc-article-end (nth 1 defs))
161 (setq nndoc-head-begin (nth 2 defs))
162 (setq nndoc-head-end (nth 3 defs))
163 (setq nndoc-first-article (nth 4 defs))
164 (setq nndoc-end-of-file (nth 5 defs))
165 (setq nndoc-body-begin (nth 6 defs)))
168 (defun nndoc-close-server (&optional server)
171 (defun nndoc-server-opened (&optional server)
172 (and (equal server nndoc-current-server)
174 (buffer-name nntp-server-buffer)))
176 (defun nndoc-status-message (&optional server)
179 (defun nndoc-request-article (article &optional newsgroup server buffer)
180 (nndoc-possibly-change-buffer newsgroup server)
182 (let ((buffer (or buffer nntp-server-buffer)))
185 (if (stringp article)
187 (nndoc-insert-article article)
190 (defun nndoc-request-group (group &optional server dont-check)
193 (if (not (nndoc-possibly-change-buffer group server))
195 (setq nndoc-status-string "No such file or buffer")
200 (set-buffer nntp-server-buffer)
202 (let ((number (nndoc-number-of-articles)))
205 (nndoc-close-group group)
207 (insert (format "211 %d %d %d %s\n" number 1 number group))
210 (defun nndoc-close-group (group &optional server)
211 (nndoc-possibly-change-buffer group server)
212 (kill-buffer nndoc-current-buffer)
213 (setq nndoc-group-alist (delq (assoc group nndoc-group-alist)
215 (setq nndoc-current-buffer nil)
218 (defun nndoc-request-list (&optional server)
221 (defun nndoc-request-newgroups (date &optional server)
224 (defun nndoc-request-list-newsgroups (&optional server)
227 (defalias 'nndoc-request-post 'nnmail-request-post)
228 (defalias 'nndoc-request-post-buffer 'nnmail-request-post-buffer)
231 ;;; Internal functions.
233 (defun nndoc-possibly-change-buffer (group source)
236 ;; The current buffer is this group's buffer.
237 ((and nndoc-current-buffer
238 (eq nndoc-current-buffer
239 (setq buf (cdr (assoc group nndoc-group-alist))))))
240 ;; We change buffers by taking an old from the group alist.
241 ;; `source' is either a string (a file name) or a buffer object.
243 (setq nndoc-current-buffer buf))
244 ;; It's a totally new group.
245 ((or (and (bufferp nndoc-address)
246 (buffer-name nndoc-address))
247 (and (stringp nndoc-address)
248 (file-exists-p nndoc-address)
249 (not (file-directory-p nndoc-address))))
250 (setq nndoc-group-alist
251 (cons (cons group (setq nndoc-current-buffer
253 (concat " *nndoc " group "*"))))
256 (set-buffer nndoc-current-buffer)
257 (buffer-disable-undo (current-buffer))
259 (if (stringp nndoc-address)
260 (insert-file-contents nndoc-address)
262 (set-buffer nndoc-address)
264 (insert-buffer-substring nndoc-address))
267 (defun nndoc-forward-article (n)
269 (re-search-forward nndoc-article-begin nil t)
270 (or (not nndoc-head-begin)
271 (re-search-forward nndoc-head-begin nil t))
272 (re-search-forward nndoc-head-end nil t))
276 (defun nndoc-number-of-articles ()
278 (set-buffer nndoc-current-buffer)
280 (goto-char (point-min))
282 (if (re-search-forward (or nndoc-first-article
283 nndoc-article-begin) nil t)
286 (while (and (re-search-forward nndoc-article-begin nil t)
287 (or (not nndoc-end-of-file)
288 (not (looking-at nndoc-end-of-file)))
289 (or (not nndoc-head-begin)
290 (re-search-forward nndoc-head-begin nil t))
291 (re-search-forward nndoc-head-end nil t))
292 (setq num (1+ num)))))
295 (defun nndoc-narrow-to-article (article)
297 (set-buffer nndoc-current-buffer)
299 (goto-char (point-min))
300 (while (and (re-search-forward nndoc-article-begin nil t)
301 (not (zerop (setq article (1- article))))))
302 (if (not (zerop article))
306 (or (and (re-search-forward nndoc-article-end nil t)
311 ;; Insert article ARTICLE in the current buffer.
312 (defun nndoc-insert-article (article)
313 (let ((ibuf (current-buffer)))
315 (set-buffer nndoc-current-buffer)
317 (goto-char (point-min))
318 (while (and (re-search-forward nndoc-article-begin nil t)
319 (not (zerop (setq article (1- article))))))
320 (if (not (zerop article))
324 (or (and (re-search-forward nndoc-article-end nil t)
327 (goto-char (point-min))
328 (or (re-search-forward nndoc-head-end nil t)
329 (goto-char (point-max)))
330 (append-to-buffer ibuf (point-min) (point))
331 (and nndoc-body-begin
332 (re-search-forward nndoc-body-begin nil t))
333 (append-to-buffer ibuf (point) (point-max))
338 ;;; nndoc.el ends here