1 ;;; nneething.el --- arbitrary file access for Gnus
2 ;; Copyright (C) 1995,96,97,98,99 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))
131 file (current-buffer)) ; ... or we fake some headers.
135 (deffoo nneething-request-group (group &optional server dont-check)
136 (nneething-possibly-change-directory group server)
138 (nneething-create-mapping)
139 (if (> (car nneething-active) (cdr nneething-active))
140 (nnheader-insert "211 0 1 0 %s\n" group)
143 (- (1+ (cdr nneething-active)) (car nneething-active))
144 (car nneething-active) (cdr nneething-active)
148 (deffoo nneething-request-list (&optional server dir)
149 (nnheader-report 'nneething "LIST is not implemented."))
151 (deffoo nneething-request-newgroups (date &optional server)
152 (nnheader-report 'nneething "NEWSGROUPS is not implemented."))
154 (deffoo nneething-request-type (group &optional article)
157 (deffoo nneething-close-group (group &optional server)
158 (setq nneething-current-directory nil)
161 (deffoo nneething-open-server (server &optional defs)
162 (nnheader-init-server-buffer)
163 (if (nneething-server-opened server)
165 (unless (assq 'nneething-address defs)
166 (setq defs (append defs (list (list 'nneething-address server)))))
167 (nnoo-change-server 'nneething server defs)))
170 ;;; Internal functions.
172 (defun nneething-possibly-change-directory (group &optional server)
174 (not (nneething-server-opened server)))
175 (nneething-open-server server))
177 (not (equal nneething-group group)))
178 (setq nneething-group group)
179 (setq nneething-map nil)
180 (setq nneething-active (cons 1 0))
181 (nneething-create-mapping)))
183 (defun nneething-map-file ()
184 ;; We make sure that the .nneething directory exists.
185 (gnus-make-directory nneething-map-file-directory)
186 ;; We store it in a special directory under the user's home dir.
187 (concat (file-name-as-directory nneething-map-file-directory)
188 nneething-group nneething-map-file))
190 (defun nneething-create-mapping ()
191 ;; Read nneething-active and nneething-map.
192 (when (file-exists-p nneething-address)
193 (let ((map-file (nneething-map-file))
194 (files (directory-files nneething-address))
196 (when (file-exists-p map-file)
198 (load map-file nil t t)))
199 (unless nneething-active
200 (setq nneething-active (cons 1 0)))
201 ;; Old nneething had a different map format.
202 (when (and (cdar nneething-map)
203 (atom (cdar nneething-map)))
206 (list (cdr n) (car n)
207 (nth 5 (file-attributes
208 (nneething-file-name (car n))))))
210 ;; Remove files matching the exclusion regexp.
211 (when nneething-exclude-files
215 (if (string-match nneething-exclude-files (car f))
216 (if prev (setcdr prev (cdr f))
217 (setq files (cdr files)))
220 ;; Remove files not matching the inclusion regexp.
221 (when nneething-include-files
225 (if (not (string-match nneething-include-files (car f)))
226 (if prev (setcdr prev (cdr f))
227 (setq files (cdr files)))
230 ;; Remove deleted files from the map.
231 (let ((map nneething-map)
234 (if (and (member (cadar map) files)
235 ;; We also remove files that have changed mod times.
236 (equal (nth 5 (file-attributes
237 (nneething-file-name (cadar map))))
240 (push (cadar map) map-files)
244 (setcdr prev (cdr map))
245 (setq nneething-map (cdr nneething-map))))
246 (setq map (cdr map))))
247 ;; Find all new files and enter them into the map.
249 (unless (member (car files) map-files)
250 ;; This file is not in the map, so we enter it.
252 (setcdr nneething-active (1+ (cdr nneething-active)))
253 (push (list (cdr nneething-active) (car files)
254 (nth 5 (file-attributes
255 (nneething-file-name (car files)))))
257 (setq files (cdr files)))
259 (not nneething-read-only))
260 (with-temp-file map-file
261 (insert "(setq nneething-map '")
262 (gnus-prin1 nneething-map)
263 (insert ")\n(setq nneething-active '")
264 (gnus-prin1 nneething-active)
267 (defun nneething-insert-head (file)
268 "Insert the head of FILE."
269 (when (nneething-get-head file)
270 (insert-buffer-substring nneething-work-buffer)
271 (goto-char (point-max))))
273 (defun nneething-make-head (file &optional buffer)
274 "Create a head by looking at the file attributes of FILE."
275 (let ((atts (file-attributes file)))
277 "Subject: " (file-name-nondirectory file) "\n"
278 "Message-ID: <nneething-"
279 (int-to-string (incf nneething-message-id-number))
280 "@" (system-name) ">\n"
281 (if (equal '(0 0) (nth 5 atts)) ""
282 (concat "Date: " (current-time-string (nth 5 atts)) "\n"))
286 (when (re-search-forward "<[a-zA-Z0-9_]@[-a-zA-Z0-9_]>" 1000 t)
287 (concat "From: " (match-string 0) "\n"))))
288 (nneething-from-line (nth 2 atts) file))
289 (if (> (string-to-int (int-to-string (nth 7 atts))) 0)
290 (concat "Chars: " (int-to-string (nth 7 atts)) "\n")
295 (concat "Lines: " (int-to-string
296 (count-lines (point-min) (point-max)))
301 (defun nneething-from-line (uid &optional file)
302 "Return a From header based of UID."
303 (let* ((login (condition-case nil
304 (user-login-name uid)
306 (cond ((= uid (user-uid)) (user-login-name))
308 (t (int-to-string uid))))))
309 (name (condition-case nil
312 (cond ((= uid (user-uid)) (user-full-name))
313 ((zerop uid) "Ms. Root")))))
314 (host (if (string-match "\\`/[^/@]*@\\([^:/]+\\):" file)
320 "/\\(users\\|home\\)/\\([^/]+\\)/" file)
321 (setq login (substring file
326 (concat "From: " login "@" host
327 (if name (concat " (" name ")") "") "\n")))
329 (defun nneething-get-head (file)
330 "Either find the head in FILE or make a head for FILE."
332 (set-buffer (get-buffer-create nneething-work-buffer))
333 (setq case-fold-search nil)
334 (buffer-disable-undo)
337 ((not (file-exists-p file))
338 ;; The file do not exist.
340 ((or (file-directory-p file)
341 (file-symlink-p file))
342 ;; It's a dir, so we fudge a head.
343 (nneething-make-head file) t)
345 ;; We examine the file.
346 (nnheader-insert-head file)
347 (if (nnheader-article-p)
350 (goto-char (point-min))
351 (or (and (search-forward "\n\n" nil t)
355 (goto-char (point-min))
356 (nneething-make-head file (current-buffer))
357 (delete-region (point) (point-max)))
360 (defun nneething-file-name (article)
361 "Return the file name of ARTICLE."
362 (let ((dir (file-name-as-directory nneething-address))
364 (if (numberp article)
365 (if (setq fname (cadr (assq article nneething-map)))
367 (make-temp-name (concat dir "nneething")))
368 (concat dir article))))
372 ;;; nneething.el ends here