1 ;;; nneething.el --- arbitrary file access for Gnus
2 ;; Copyright (C) 1995,96,97,98 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;; Keywords: news, mail
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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
29 (eval-when-compile (require 'cl))
36 (nnoo-declare nneething)
38 (defvoo nneething-map-file-directory "~/.nneething/"
39 "Where nneething stores the map files.")
41 (defvoo nneething-map-file ".nneething"
42 "Name of the map files.")
44 (defvoo nneething-exclude-files nil
45 "Regexp saying what files to exclude from the group.
46 If this variable is nil, no files will be excluded.")
48 (defvoo nneething-include-files nil
49 "Regexp saying what files to include in the group.
50 If this variable is non-nil, only files matching this regexp will be
55 ;;; Internal variables.
57 (defconst nneething-version "nneething 1.0"
60 (defvoo nneething-current-directory nil
61 "Current news group directory.")
63 (defvoo nneething-status-string "")
65 (defvoo nneething-message-id-number 0)
66 (defvoo nneething-work-buffer " *nneething work*")
68 (defvoo nneething-group nil)
69 (defvoo nneething-map nil)
70 (defvoo nneething-read-only nil)
71 (defvoo nneething-active nil)
72 (defvoo nneething-address nil)
76 ;;; Interface functions.
78 (nnoo-define-basics nneething)
80 (deffoo nneething-retrieve-headers (articles &optional group server fetch-old)
81 (nneething-possibly-change-directory group)
84 (set-buffer nntp-server-buffer)
86 (let* ((number (length articles))
88 (large (and (numberp nnmail-large-newsgroup)
89 (> number nnmail-large-newsgroup)))
92 (if (stringp (car articles))
95 (while (setq article (pop articles))
96 (setq file (nneething-file-name article))
98 (when (and (file-exists-p file)
99 (or (file-directory-p file)
100 (not (zerop (nnheader-file-size file)))))
101 (insert (format "221 %d Article retrieved.\n" article))
102 (nneething-insert-head file)
109 (nnheader-message 5 "nneething: Receiving headers... %d%%"
110 (/ (* count 100) number))))
113 (nnheader-message 5 "nneething: Receiving headers...done"))
115 (nnheader-fold-continuation-lines)
118 (deffoo nneething-request-article (id &optional group server buffer)
119 (nneething-possibly-change-directory group)
120 (let ((file (unless (stringp id)
121 (nneething-file-name id)))
122 (nntp-server-buffer (or buffer nntp-server-buffer)))
123 (and (stringp file) ; We did not request by Message-ID.
124 (file-exists-p file) ; The file exists.
125 (not (file-directory-p file)) ; It's not a dir.
127 (nnmail-find-file file) ; Insert the file in the nntp buf.
128 (unless (nnheader-article-p) ; Either it's a real article...
129 (goto-char (point-min))
130 (nneething-make-head file (current-buffer)) ; ... or we fake some headers.
134 (deffoo nneething-request-group (group &optional server dont-check)
135 (nneething-possibly-change-directory group server)
137 (nneething-create-mapping)
138 (if (> (car nneething-active) (cdr nneething-active))
139 (nnheader-insert "211 0 1 0 %s\n" group)
142 (- (1+ (cdr nneething-active)) (car nneething-active))
143 (car nneething-active) (cdr nneething-active)
147 (deffoo nneething-request-list (&optional server dir)
148 (nnheader-report 'nneething "LIST is not implemented."))
150 (deffoo nneething-request-newgroups (date &optional server)
151 (nnheader-report 'nneething "NEWSGROUPS is not implemented."))
153 (deffoo nneething-request-type (group &optional article)
156 (deffoo nneething-close-group (group &optional server)
157 (setq nneething-current-directory nil)
160 (deffoo nneething-open-server (server &optional defs)
161 (nnheader-init-server-buffer)
162 (if (nneething-server-opened server)
164 (unless (assq 'nneething-address defs)
165 (setq defs (append defs (list (list 'nneething-address server)))))
166 (nnoo-change-server 'nneething server defs)))
169 ;;; Internal functions.
171 (defun nneething-possibly-change-directory (group &optional server)
173 (not (nneething-server-opened server)))
174 (nneething-open-server server))
176 (not (equal nneething-group group)))
177 (setq nneething-group group)
178 (setq nneething-map nil)
179 (setq nneething-active (cons 1 0))
180 (nneething-create-mapping)))
182 (defun nneething-map-file ()
183 ;; We make sure that the .nneething directory exists.
184 (gnus-make-directory nneething-map-file-directory)
185 ;; We store it in a special directory under the user's home dir.
186 (concat (file-name-as-directory nneething-map-file-directory)
187 nneething-group nneething-map-file))
189 (defun nneething-create-mapping ()
190 ;; Read nneething-active and nneething-map.
191 (when (file-exists-p nneething-address)
192 (let ((map-file (nneething-map-file))
193 (files (directory-files nneething-address))
195 (when (file-exists-p map-file)
197 (load map-file nil t t)))
198 (unless nneething-active
199 (setq nneething-active (cons 1 0)))
200 ;; Old nneething had a different map format.
201 (when (and (cdar nneething-map)
202 (atom (cdar nneething-map)))
205 (list (cdr n) (car n)
206 (nth 5 (file-attributes
207 (nneething-file-name (car n))))))
209 ;; Remove files matching the exclusion regexp.
210 (when nneething-exclude-files
214 (if (string-match nneething-exclude-files (car f))
215 (if prev (setcdr prev (cdr f))
216 (setq files (cdr files)))
219 ;; Remove files not matching the inclusion regexp.
220 (when nneething-include-files
224 (if (not (string-match nneething-include-files (car f)))
225 (if prev (setcdr prev (cdr f))
226 (setq files (cdr files)))
229 ;; Remove deleted files from the map.
230 (let ((map nneething-map)
233 (if (and (member (cadar map) files)
234 ;; We also remove files that have changed mod times.
235 (equal (nth 5 (file-attributes
236 (nneething-file-name (cadar map))))
239 (push (cadar map) map-files)
243 (setcdr prev (cdr map))
244 (setq nneething-map (cdr nneething-map))))
245 (setq map (cdr map))))
246 ;; Find all new files and enter them into the map.
248 (unless (member (car files) map-files)
249 ;; This file is not in the map, so we enter it.
251 (setcdr nneething-active (1+ (cdr nneething-active)))
252 (push (list (cdr nneething-active) (car files)
253 (nth 5 (file-attributes
254 (nneething-file-name (car files)))))
256 (setq files (cdr files)))
258 (not nneething-read-only))
259 (with-temp-file map-file
260 (insert "(setq nneething-map '")
261 (gnus-prin1 nneething-map)
262 (insert ")\n(setq nneething-active '")
263 (gnus-prin1 nneething-active)
266 (defun nneething-insert-head (file)
267 "Insert the head of FILE."
268 (when (nneething-get-head file)
269 (insert-buffer-substring nneething-work-buffer)
270 (goto-char (point-max))))
272 (defun nneething-make-head (file &optional buffer)
273 "Create a head by looking at the file attributes of FILE."
274 (let ((atts (file-attributes file)))
276 "Subject: " (file-name-nondirectory file) "\n"
277 "Message-ID: <nneething-"
278 (int-to-string (incf nneething-message-id-number))
279 "@" (system-name) ">\n"
280 (if (equal '(0 0) (nth 5 atts)) ""
281 (concat "Date: " (current-time-string (nth 5 atts)) "\n"))
285 (when (re-search-forward "<[a-zA-Z0-9_]@[-a-zA-Z0-9_]>" 1000 t)
286 (concat "From: " (match-string 0) "\n"))))
287 (nneething-from-line (nth 2 atts) file))
288 (if (> (string-to-int (int-to-string (nth 7 atts))) 0)
289 (concat "Chars: " (int-to-string (nth 7 atts)) "\n")
294 (concat "Lines: " (int-to-string
295 (count-lines (point-min) (point-max)))
300 (defun nneething-from-line (uid &optional file)
301 "Return a From header based of UID."
302 (let* ((login (condition-case nil
303 (user-login-name uid)
305 (cond ((= uid (user-uid)) (user-login-name))
307 (t (int-to-string uid))))))
308 (name (condition-case nil
311 (cond ((= uid (user-uid)) (user-full-name))
312 ((zerop uid) "Ms. Root")))))
313 (host (if (string-match "\\`/[^/@]*@\\([^:/]+\\):" file)
318 (when (string-match "/\\(users\\|home\\)/\\([^/]+\\)/" file)
319 (setq login (substring file
324 (concat "From: " login "@" host
325 (if name (concat " (" name ")") "") "\n")))
327 (defun nneething-get-head (file)
328 "Either find the head in FILE or make a head for FILE."
330 (set-buffer (get-buffer-create nneething-work-buffer))
331 (setq case-fold-search nil)
332 (buffer-disable-undo)
335 ((not (file-exists-p file))
336 ;; The file do not exist.
338 ((or (file-directory-p file)
339 (file-symlink-p file))
340 ;; It's a dir, so we fudge a head.
341 (nneething-make-head file) t)
343 ;; We examine the file.
344 (nnheader-insert-head file)
345 (if (nnheader-article-p)
348 (goto-char (point-min))
349 (or (and (search-forward "\n\n" nil t)
353 (goto-char (point-min))
354 (nneething-make-head file (current-buffer))
355 (delete-region (point) (point-max)))
358 (defun nneething-file-name (article)
359 "Return the file name of ARTICLE."
360 (let ((dir (file-name-as-directory nneething-address))
362 (if (numberp article)
363 (if (setq fname (cadr (assq article nneething-map)))
365 (make-temp-name (concat dir "nneething")))
366 (concat dir article))))
370 ;;; nneething.el ends here