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)
42 "^------------------------------[\n \t]+"
43 "^------------------------------[\n \t]+"
45 "^------------------------------*[\n \t]*\n[^ ]+: "))
46 "Regular expressions for articles of the various types.")
50 (defvar nndoc-article-begin nil)
51 (defvar nndoc-article-end nil)
52 (defvar nndoc-head-begin nil)
53 (defvar nndoc-head-end nil)
54 (defvar nndoc-first-article nil)
56 (defvar nndoc-current-server nil)
57 (defvar nndoc-server-alist nil)
58 (defvar nndoc-server-variables
60 (list 'nndoc-article-type nndoc-article-type)
61 '(nndoc-article-begin nil)
62 '(nndoc-article-end nil)
63 '(nndoc-head-begin nil)
65 '(nndoc-first-article nil)
66 '(nndoc-current-buffer nil)
67 '(nndoc-group-alist nil)
68 '(nndoc-address nil)))
70 (defconst nndoc-version "nndoc 0.1"
73 (defvar nndoc-current-buffer nil
74 "Current nndoc news buffer.")
76 (defvar nndoc-address nil)
80 (defvar nndoc-status-string "")
82 (defvar nndoc-group-alist nil)
84 ;;; Interface functions
86 (defun nndoc-retrieve-headers (sequence &optional newsgroup server)
88 (set-buffer nntp-server-buffer)
91 article p beg end lines)
92 (nndoc-possibly-change-buffer newsgroup server)
93 (if (stringp (car sequence))
95 (set-buffer nndoc-current-buffer)
96 (goto-char (point-min))
97 (re-search-forward nndoc-article-begin nil t)
98 (or (not nndoc-head-begin)
99 (re-search-forward nndoc-head-begin nil t))
100 (re-search-forward nndoc-head-end nil t)
102 (setq article (car sequence))
103 (set-buffer nndoc-current-buffer)
104 (if (not (nndoc-forward-article (- article prev)))
107 (setq beg (or (re-search-backward nndoc-article-begin nil t)
110 (setq lines (count-lines
113 (and (re-search-forward nndoc-article-end nil t)
114 (goto-char (match-beginning 0)))
115 (goto-char (point-max)))))
118 (set-buffer nntp-server-buffer)
119 (insert (format "221 %d Article retrieved.\n" article))
120 (insert-buffer-substring nndoc-current-buffer beg end)
121 (goto-char (point-max))
122 (insert (format "Lines: %d\n" lines))
126 sequence (cdr sequence)))
128 ;; Fold continuation lines.
129 (goto-char (point-min))
130 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
131 (replace-match " " t t))
134 (defun nndoc-open-server (server &optional defs)
135 (nnheader-init-server-buffer)
136 (if (equal server nndoc-current-server)
138 (if nndoc-current-server
139 (setq nndoc-server-alist
140 (cons (list nndoc-current-server
141 (nnheader-save-variables nndoc-server-variables))
142 nndoc-server-alist)))
143 (let ((state (assoc server nndoc-server-alist)))
146 (nnheader-restore-variables (nth 1 state))
147 (setq nndoc-server-alist (delq state nndoc-server-alist)))
148 (nnheader-set-init-variables nndoc-server-variables defs)))
149 (setq nndoc-current-server server)
150 (let ((defs (cdr (assq nndoc-article-type nndoc-type-to-regexp))))
151 (setq nndoc-article-begin (nth 0 defs))
152 (setq nndoc-article-end (nth 1 defs))
153 (setq nndoc-head-begin (nth 2 defs))
154 (setq nndoc-head-end (nth 3 defs))
155 (setq nndoc-first-article (nth 4 defs)))
158 (defun nndoc-close-server (&optional server)
161 (defun nndoc-server-opened (&optional server)
162 (and (equal server nndoc-current-server)
164 (buffer-name nntp-server-buffer)))
166 (defun nndoc-status-message (&optional server)
169 (defun nndoc-request-article (article &optional newsgroup server buffer)
170 (nndoc-possibly-change-buffer newsgroup server)
172 (let ((buffer (or buffer nntp-server-buffer)))
175 (if (stringp article)
177 (nndoc-narrow-to-article article)
178 (insert-buffer-substring nndoc-current-buffer)
181 (defun nndoc-request-group (group &optional server dont-check)
184 (if (not (nndoc-possibly-change-buffer group server))
186 (setq nndoc-status-string "No such file or buffer")
191 (set-buffer nntp-server-buffer)
193 (let ((number (nndoc-number-of-articles)))
196 (nndoc-close-group group)
198 (insert (format "211 %d %d %d %s\n" number 1 number group))
201 (defun nndoc-close-group (group &optional server)
202 (nndoc-possibly-change-buffer group server)
203 (kill-buffer nndoc-current-buffer)
204 (setq nndoc-group-alist (delq (assoc group nndoc-group-alist)
206 (setq nndoc-current-buffer nil)
209 (defun nndoc-request-list (&optional server)
212 (defun nndoc-request-newgroups (date &optional server)
215 (defun nndoc-request-list-newsgroups (&optional server)
218 (defalias 'nndoc-request-post 'nnmail-request-post)
219 (defalias 'nndoc-request-post-buffer 'nnmail-request-post-buffer)
222 ;;; Internal functions.
224 (defun nndoc-possibly-change-buffer (group source)
227 ;; The current buffer is this group's buffer.
228 ((and nndoc-current-buffer
229 (eq nndoc-current-buffer
230 (setq buf (cdr (assoc group nndoc-group-alist))))))
231 ;; We change buffers by taking an old from the group alist.
232 ;; `source' is either a string (a file name) or a buffer object.
234 (setq nndoc-current-buffer buf))
235 ;; It's a totally new group.
236 ((or (and (bufferp nndoc-address)
237 (buffer-name nndoc-address))
238 (and (stringp nndoc-address)
239 (file-exists-p nndoc-address)
240 (not (file-directory-p nndoc-address))))
241 (setq nndoc-group-alist
242 (cons (cons group (setq nndoc-current-buffer
244 (concat " *nndoc " group "*"))))
247 (set-buffer nndoc-current-buffer)
248 (buffer-disable-undo (current-buffer))
250 (if (stringp nndoc-address)
251 (insert-file-contents nndoc-address)
253 (set-buffer nndoc-address)
255 (insert-buffer-substring nndoc-address))
258 (defun nndoc-forward-article (n)
260 (re-search-forward nndoc-article-begin nil t)
261 (or (not nndoc-head-begin)
262 (re-search-forward nndoc-head-begin nil t))
263 (re-search-forward nndoc-head-end nil t))
267 (defun nndoc-number-of-articles ()
269 (set-buffer nndoc-current-buffer)
271 (goto-char (point-min))
273 (while (and (re-search-forward nndoc-article-begin nil t)
274 (or (not nndoc-head-begin)
275 (re-search-forward nndoc-head-begin nil t))
276 (re-search-forward nndoc-head-end nil t))
280 (defun nndoc-narrow-to-article (article)
282 (set-buffer nndoc-current-buffer)
284 (goto-char (point-min))
285 (while (and (re-search-forward nndoc-article-begin nil t)
286 (not (zerop (setq article (1- article))))))
287 (if (not (zerop article))
291 (or (and (re-search-forward nndoc-article-end nil t)
298 ;;; nndoc.el ends here