1 ;;; nneething.el --- arbitrary file access for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001
4 ;; Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
8 ;; Keywords: news, mail
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
31 (eval-when-compile (require 'cl))
38 (nnoo-declare nneething)
40 (defvoo nneething-map-file-directory "~/.nneething/"
41 "Where nneething stores the map files.")
43 (defvoo nneething-map-file ".nneething"
44 "Name of the map files.")
46 (defvoo nneething-exclude-files nil
47 "Regexp saying what files to exclude from the group.
48 If this variable is nil, no files will be excluded.")
50 (defvoo nneething-include-files nil
51 "Regexp saying what files to include in the group.
52 If this variable is non-nil, only files matching this regexp will be
57 ;;; Internal variables.
59 (defconst nneething-version "nneething 1.0"
62 (defvoo nneething-current-directory nil
63 "Current news group directory.")
65 (defvoo nneething-status-string "")
67 (defvoo nneething-message-id-number 0)
68 (defvoo nneething-work-buffer " *nneething work*")
70 (defvoo nneething-group nil)
71 (defvoo nneething-map nil)
72 (defvoo nneething-read-only nil)
73 (defvoo nneething-active nil)
74 (defvoo nneething-address nil)
78 ;;; Interface functions.
80 (nnoo-define-basics nneething)
82 (deffoo nneething-retrieve-headers (articles &optional group server fetch-old)
83 (nneething-possibly-change-directory group)
86 (set-buffer nntp-server-buffer)
88 (let* ((number (length articles))
90 (large (and (numberp nnmail-large-newsgroup)
91 (> number nnmail-large-newsgroup)))
94 (if (stringp (car articles))
97 (while (setq article (pop articles))
98 (setq file (nneething-file-name article))
100 (when (and (file-exists-p file)
101 (or (file-directory-p file)
102 (not (zerop (nnheader-file-size file)))))
103 (insert (format "221 %d Article retrieved.\n" article))
104 (nneething-insert-head file)
111 (nnheader-message 5 "nneething: Receiving headers... %d%%"
112 (/ (* count 100) number))))
115 (nnheader-message 5 "nneething: Receiving headers...done"))
117 (nnheader-fold-continuation-lines)
120 (deffoo nneething-request-article (id &optional group server buffer)
121 (nneething-possibly-change-directory group)
122 (let ((file (unless (stringp id)
123 (nneething-file-name id)))
124 (nntp-server-buffer (or buffer nntp-server-buffer)))
125 (and (stringp file) ; We did not request by Message-ID.
126 (file-exists-p file) ; The file exists.
127 (not (file-directory-p file)) ; It's not a dir.
129 (nnmail-find-file file) ; Insert the file in the nntp buf.
130 (unless (nnheader-article-p) ; Either it's a real article...
132 (unless (file-directory-p file)
133 (or (cdr (assoc (concat "." (file-name-extension file))
134 mailcap-mime-extensions))
137 (mm-detect-mime-charset-region (point-min) (point-max)))
139 (unless (string-match "\\`text/" type)
140 (base64-encode-region (point-min) (point-max))
141 (setq encoding "base64"))
142 (goto-char (point-min))
143 (nneething-make-head file (current-buffer)
144 nil type charset encoding))
148 (deffoo nneething-request-group (group &optional server dont-check)
149 (nneething-possibly-change-directory group server)
151 (nneething-create-mapping)
152 (if (> (car nneething-active) (cdr nneething-active))
153 (nnheader-insert "211 0 1 0 %s\n" group)
156 (- (1+ (cdr nneething-active)) (car nneething-active))
157 (car nneething-active) (cdr nneething-active)
161 (deffoo nneething-request-list (&optional server dir)
162 (nnheader-report 'nneething "LIST is not implemented."))
164 (deffoo nneething-request-newgroups (date &optional server)
165 (nnheader-report 'nneething "NEWSGROUPS is not implemented."))
167 (deffoo nneething-request-type (group &optional article)
170 (deffoo nneething-close-group (group &optional server)
171 (setq nneething-current-directory nil)
174 (deffoo nneething-open-server (server &optional defs)
175 (nnheader-init-server-buffer)
176 (if (nneething-server-opened server)
178 (unless (assq 'nneething-address defs)
179 (setq defs (append defs (list (list 'nneething-address server)))))
180 (nnoo-change-server 'nneething server defs)))
183 ;;; Internal functions.
185 (defun nneething-possibly-change-directory (group &optional server)
187 (not (nneething-server-opened server)))
188 (nneething-open-server server))
190 (not (equal nneething-group group)))
191 (setq nneething-group group)
192 (setq nneething-map nil)
193 (setq nneething-active (cons 1 0))
194 (nneething-create-mapping)))
196 (defun nneething-map-file ()
197 ;; We make sure that the .nneething directory exists.
198 (gnus-make-directory nneething-map-file-directory)
199 ;; We store it in a special directory under the user's home dir.
200 (concat (file-name-as-directory nneething-map-file-directory)
201 nneething-group nneething-map-file))
203 (defun nneething-create-mapping ()
204 ;; Read nneething-active and nneething-map.
205 (when (file-exists-p nneething-address)
206 (let ((map-file (nneething-map-file))
207 (files (directory-files nneething-address))
209 (when (file-exists-p map-file)
211 (load map-file nil t t)))
212 (unless nneething-active
213 (setq nneething-active (cons 1 0)))
214 ;; Old nneething had a different map format.
215 (when (and (cdar nneething-map)
216 (atom (cdar nneething-map)))
219 (list (cdr n) (car n)
220 (nth 5 (file-attributes
221 (nneething-file-name (car n))))))
223 ;; Remove files matching the exclusion regexp.
224 (when nneething-exclude-files
228 (if (string-match nneething-exclude-files (car f))
229 (if prev (setcdr prev (cdr f))
230 (setq files (cdr files)))
233 ;; Remove files not matching the inclusion regexp.
234 (when nneething-include-files
238 (if (not (string-match nneething-include-files (car f)))
239 (if prev (setcdr prev (cdr f))
240 (setq files (cdr files)))
243 ;; Remove deleted files from the map.
244 (let ((map nneething-map)
247 (if (and (member (cadr (car map)) files)
248 ;; We also remove files that have changed mod times.
249 (equal (nth 5 (file-attributes
250 (nneething-file-name (cadr (car map)))))
253 (push (cadr (car map)) map-files)
257 (setcdr prev (cdr map))
258 (setq nneething-map (cdr nneething-map))))
259 (setq map (cdr map))))
260 ;; Find all new files and enter them into the map.
262 (unless (member (car files) map-files)
263 ;; This file is not in the map, so we enter it.
265 (setcdr nneething-active (1+ (cdr nneething-active)))
266 (push (list (cdr nneething-active) (car files)
267 (nth 5 (file-attributes
268 (nneething-file-name (car files)))))
270 (setq files (cdr files)))
272 (not nneething-read-only))
273 (with-temp-file map-file
274 (insert "(setq nneething-map '")
275 (gnus-prin1 nneething-map)
276 (insert ")\n(setq nneething-active '")
277 (gnus-prin1 nneething-active)
280 (defun nneething-insert-head (file)
281 "Insert the head of FILE."
282 (when (nneething-get-head file)
283 (insert-buffer-substring nneething-work-buffer)
284 (goto-char (point-max))))
286 (defun nneething-make-head (file &optional buffer extra-msg
287 mime-type mime-charset mime-encoding)
288 "Create a head by looking at the file attributes of FILE."
289 (let ((atts (file-attributes file)))
291 "Subject: " (file-name-nondirectory file) (or extra-msg "") "\n"
292 "Message-ID: <nneething-"
293 (int-to-string (incf nneething-message-id-number))
294 "@" (system-name) ">\n"
295 (if (equal '(0 0) (nth 5 atts)) ""
296 (concat "Date: " (current-time-string (nth 5 atts)) "\n"))
300 (when (re-search-forward "<[a-zA-Z0-9_]@[-a-zA-Z0-9_]>" 1000 t)
301 (concat "From: " (match-string 0) "\n"))))
302 (nneething-from-line (nth 2 atts) file))
303 (if (> (string-to-int (int-to-string (nth 7 atts))) 0)
304 (concat "Chars: " (int-to-string (nth 7 atts)) "\n")
309 (concat "Lines: " (int-to-string
310 (count-lines (point-min) (point-max)))
314 (concat "Content-Type: " mime-type
317 (if (stringp mime-charset)
319 (symbol-name mime-charset)))
322 (concat "\nContent-Transfer-Encoding: " mime-encoding)
324 "\nMIME-Version: 1.0\n")
327 (defun nneething-from-line (uid &optional file)
328 "Return a From header based of UID."
329 (let* ((login (condition-case nil
330 (user-login-name uid)
332 (cond ((= uid (user-uid)) (user-login-name))
334 (t (int-to-string uid))))))
335 (name (condition-case nil
338 (cond ((= uid (user-uid)) (user-full-name))
339 ((zerop uid) "Ms. Root")))))
340 (host (if (string-match "\\`/[^/@]*@\\([^:/]+\\):" file)
346 "/\\(users\\|home\\)/\\([^/]+\\)/" file)
347 (setq login (substring file
352 (concat "From: " login "@" host
353 (if name (concat " (" name ")") "") "\n")))
355 (defun nneething-get-head (file)
356 "Either find the head in FILE or make a head for FILE."
358 (set-buffer (get-buffer-create nneething-work-buffer))
359 (setq case-fold-search nil)
360 (buffer-disable-undo)
363 ((not (file-exists-p file))
364 ;; The file do not exist.
366 ((or (file-directory-p file)
367 (file-symlink-p file))
368 ;; It's a dir, so we fudge a head.
369 (nneething-make-head file) t)
371 ;; We examine the file.
374 (nnheader-insert-head file)
375 (if (nnheader-article-p)
378 (goto-char (point-min))
379 (or (and (search-forward "\n\n" nil t)
383 (goto-char (point-min))
384 (nneething-make-head file (current-buffer))
385 (delete-region (point) (point-max))))
387 (nneething-make-head file (current-buffer) " (unreadable)")))
390 (defun nneething-file-name (article)
391 "Return the file name of ARTICLE."
392 (let ((dir (file-name-as-directory nneething-address))
394 (if (numberp article)
395 (if (setq fname (cadr (assq article nneething-map)))
396 (expand-file-name fname dir)
397 (make-temp-name (expand-file-name "nneething" dir)))
398 (expand-file-name article dir))))
402 ;;; nneething.el ends here