1 ;;; nneething.el --- arbitrary file access for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005 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., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
31 (eval-when-compile (require 'cl))
38 (nnoo-declare nneething)
40 (defvoo nneething-map-file-directory
41 (nnheader-concat gnus-directory ".nneething/")
42 "Where nneething stores the map files.")
44 (defvoo nneething-map-file ".nneething"
45 "Name of the map files.")
47 (defvoo nneething-exclude-files nil
48 "Regexp saying what files to exclude from the group.
49 If this variable is nil, no files will be excluded.")
51 (defvoo nneething-include-files nil
52 "Regexp saying what files to include in the group.
53 If this variable is non-nil, only files matching this regexp will be
58 ;;; Internal variables.
60 (defconst nneething-version "nneething 1.0"
63 (defvoo nneething-current-directory nil
64 "Current news group directory.")
66 (defvoo nneething-status-string "")
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)
76 (defvar nneething-mime-extensions
78 (".abs" . "audio/x-mpeg")
79 (".aif" . "audio/aiff")
80 (".aifc" . "audio/aiff")
81 (".aiff" . "audio/aiff")
82 (".ano" . "application/x-annotator")
83 (".au" . "audio/ulaw")
84 (".avi" . "video/x-msvideo")
85 (".bcpio" . "application/x-bcpio")
86 (".bin" . "application/octet-stream")
87 (".cdf" . "application/x-netcdr")
88 (".cpio" . "application/x-cpio")
89 (".csh" . "application/x-csh")
91 (".dvi" . "application/x-dvi")
92 (".diff" . "text/x-patch")
93 (".el" . "application/emacs-lisp")
94 (".eps" . "application/postscript")
95 (".etx" . "text/x-setext")
96 (".exe" . "application/octet-stream")
97 (".fax" . "image/x-fax")
98 (".gif" . "image/gif")
99 (".hdf" . "application/x-hdf")
100 (".hqx" . "application/mac-binhex40")
101 (".htm" . "text/html")
102 (".html" . "text/html")
103 (".icon" . "image/x-icon")
104 (".ief" . "image/ief")
105 (".jpg" . "image/jpeg")
106 (".macp" . "image/x-macpaint")
107 (".man" . "application/x-troff-man")
108 (".me" . "application/x-troff-me")
109 (".mif" . "application/mif")
110 (".mov" . "video/quicktime")
111 (".movie" . "video/x-sgi-movie")
112 (".mp2" . "audio/x-mpeg")
113 (".mp3" . "audio/x-mpeg")
114 (".mp2a" . "audio/x-mpeg2")
115 (".mpa" . "audio/x-mpeg")
116 (".mpa2" . "audio/x-mpeg2")
117 (".mpe" . "video/mpeg")
118 (".mpeg" . "video/mpeg")
119 (".mpega" . "audio/x-mpeg")
120 (".mpegv" . "video/mpeg")
121 (".mpg" . "video/mpeg")
122 (".mpv" . "video/mpeg")
123 (".ms" . "application/x-troff-ms")
124 (".nc" . "application/x-netcdf")
125 (".nc" . "application/x-netcdf")
126 (".oda" . "application/oda")
127 (".patch" . "text/x-patch")
128 (".pbm" . "image/x-portable-bitmap")
129 (".pdf" . "application/pdf")
130 (".pgm" . "image/portable-graymap")
131 (".pict" . "image/pict")
132 (".png" . "image/png")
133 (".pnm" . "image/x-portable-anymap")
134 (".ppm" . "image/portable-pixmap")
135 (".ps" . "application/postscript")
136 (".qt" . "video/quicktime")
137 (".ras" . "image/x-raster")
138 (".rgb" . "image/x-rgb")
139 (".rtf" . "application/rtf")
140 (".rtx" . "text/richtext")
141 (".sh" . "application/x-sh")
142 (".sit" . "application/x-stuffit")
143 (".siv" . "application/sieve")
144 (".snd" . "audio/basic")
145 (".src" . "application/x-wais-source")
146 (".tar" . "archive/tar")
147 (".tcl" . "application/x-tcl")
148 (".tex" . "application/x-tex")
149 (".texi" . "application/texinfo")
150 (".tga" . "image/x-targa")
151 (".tif" . "image/tiff")
152 (".tiff" . "image/tiff")
153 (".tr" . "application/x-troff")
154 (".troff" . "application/x-troff")
155 (".tsv" . "text/tab-separated-values")
156 (".txt" . "text/plain")
157 (".vbs" . "video/mpeg")
158 (".vox" . "audio/basic")
159 (".vrml" . "x-world/x-vrml")
160 (".wav" . "audio/x-wav")
161 (".xls" . "application/vnd.ms-excel")
162 (".wrl" . "x-world/x-vrml")
163 (".xbm" . "image/xbm")
164 (".xpm" . "image/xpm")
165 (".xwd" . "image/windowdump")
166 (".zip" . "application/zip")
167 (".ai" . "application/postscript")
168 (".jpe" . "image/jpeg")
169 (".jpeg" . "image/jpeg"))
170 "An alist of file extensions and corresponding MIME content-types.
171 This variable is used as the alternative of `mailcap-mime-extensions'.")
175 ;;; Interface functions.
177 (nnoo-define-basics nneething)
179 (deffoo nneething-retrieve-headers (articles &optional group server fetch-old)
180 (nneething-possibly-change-directory group)
183 (set-buffer nntp-server-buffer)
185 (let* ((number (length articles))
187 (large (and (numberp nnmail-large-newsgroup)
188 (> number nnmail-large-newsgroup)))
191 (if (stringp (car articles))
194 (while (setq article (pop articles))
195 (setq file (nneething-file-name article))
197 (when (and (file-exists-p file)
198 (or (file-directory-p file)
199 (not (zerop (nnheader-file-size file)))))
200 (insert (format "221 %d Article retrieved.\n" article))
201 (nneething-insert-head file)
208 (nnheader-message 5 "nneething: Receiving headers... %d%%"
209 (/ (* count 100) number))))
212 (nnheader-message 5 "nneething: Receiving headers...done"))
214 (nnheader-fold-continuation-lines)
217 (deffoo nneething-request-article (id &optional group server buffer)
218 (nneething-possibly-change-directory group)
219 (let ((file (unless (stringp id)
220 (nneething-file-name id)))
221 (nntp-server-buffer (or buffer nntp-server-buffer)))
222 (and (stringp file) ; We did not request by Message-ID.
223 (file-exists-p file) ; The file exists.
224 (not (file-directory-p file)) ; It's not a dir.
226 (let ((nnmail-file-coding-system 'binary))
227 (nnmail-find-file file)) ; Insert the file in the nntp buf.
228 (unless (nnheader-article-p) ; Either it's a real article...
230 (unless (file-directory-p file)
231 (or (cdr (assoc (concat "." (file-name-extension file))
232 (if (boundp 'mailcap-mime-extensions)
233 (symbol-value 'mailcap-mime-extensions)
234 nneething-mime-extensions)))
237 (mm-detect-mime-charset-region (point-min) (point-max)))
239 (unless (string-match "\\`text/" type)
240 (base64-encode-region (point-min) (point-max))
241 (setq encoding "base64"))
242 (goto-char (point-min))
243 (nneething-make-head file (current-buffer)
244 nil type charset encoding))
248 (deffoo nneething-request-group (group &optional server dont-check)
249 (nneething-possibly-change-directory group server)
251 (nneething-create-mapping)
252 (if (> (car nneething-active) (cdr nneething-active))
253 (nnheader-insert "211 0 1 0 %s\n" group)
256 (- (1+ (cdr nneething-active)) (car nneething-active))
257 (car nneething-active) (cdr nneething-active)
261 (deffoo nneething-request-list (&optional server dir)
262 (nnheader-report 'nneething "LIST is not implemented."))
264 (deffoo nneething-request-newgroups (date &optional server)
265 (nnheader-report 'nneething "NEWSGROUPS is not implemented."))
267 (deffoo nneething-request-type (group &optional article)
270 (deffoo nneething-close-group (group &optional server)
271 (setq nneething-current-directory nil)
274 (deffoo nneething-open-server (server &optional defs)
275 (nnheader-init-server-buffer)
276 (if (nneething-server-opened server)
278 (unless (assq 'nneething-address defs)
279 (setq defs (append defs (list (list 'nneething-address server)))))
280 (nnoo-change-server 'nneething server defs)))
283 ;;; Internal functions.
285 (defun nneething-possibly-change-directory (group &optional server)
287 (not (nneething-server-opened server)))
288 (nneething-open-server server))
290 (not (equal nneething-group group)))
291 (setq nneething-group group)
292 (setq nneething-map nil)
293 (setq nneething-active (cons 1 0))
294 (nneething-create-mapping)))
296 (defun nneething-map-file ()
297 ;; We make sure that the .nneething directory exists.
298 (gnus-make-directory nneething-map-file-directory)
299 ;; We store it in a special directory under the user's home dir.
300 (concat (file-name-as-directory nneething-map-file-directory)
301 nneething-group nneething-map-file))
303 (defun nneething-create-mapping ()
304 ;; Read nneething-active and nneething-map.
305 (when (file-exists-p nneething-address)
306 (let ((map-file (nneething-map-file))
307 (files (directory-files nneething-address))
309 (when (file-exists-p map-file)
311 (load map-file nil t t)))
312 (unless nneething-active
313 (setq nneething-active (cons 1 0)))
314 ;; Old nneething had a different map format.
315 (when (and (cdar nneething-map)
316 (atom (cdar nneething-map)))
319 (list (cdr n) (car n)
320 (nth 5 (file-attributes
321 (nneething-file-name (car n))))))
323 ;; Remove files matching the exclusion regexp.
324 (when nneething-exclude-files
328 (if (string-match nneething-exclude-files (car f))
329 (if prev (setcdr prev (cdr f))
330 (setq files (cdr files)))
333 ;; Remove files not matching the inclusion regexp.
334 (when nneething-include-files
338 (if (not (string-match nneething-include-files (car f)))
339 (if prev (setcdr prev (cdr f))
340 (setq files (cdr files)))
343 ;; Remove deleted files from the map.
344 (let ((map nneething-map)
347 (if (and (member (cadr (car map)) files)
348 ;; We also remove files that have changed mod times.
349 (equal (nth 5 (file-attributes
350 (nneething-file-name (cadr (car map)))))
353 (push (cadr (car map)) map-files)
357 (setcdr prev (cdr map))
358 (setq nneething-map (cdr nneething-map))))
359 (setq map (cdr map))))
360 ;; Find all new files and enter them into the map.
362 (unless (member (car files) map-files)
363 ;; This file is not in the map, so we enter it.
365 (setcdr nneething-active (1+ (cdr nneething-active)))
366 (push (list (cdr nneething-active) (car files)
367 (nth 5 (file-attributes
368 (nneething-file-name (car files)))))
370 (setq files (cdr files)))
372 (not nneething-read-only))
373 (with-temp-file map-file
374 (insert "(setq nneething-map '")
375 (gnus-prin1 nneething-map)
376 (insert ")\n(setq nneething-active '")
377 (gnus-prin1 nneething-active)
380 (defun nneething-insert-head (file)
381 "Insert the head of FILE."
382 (when (nneething-get-head file)
383 (insert-buffer-substring nneething-work-buffer)
384 (goto-char (point-max))))
386 (defun nneething-encode-file-name (file &optional coding-system)
387 "Encode the name of the FILE in CODING-SYSTEM."
389 (setq file (mm-encode-coding-string
390 file (or coding-system nnmail-pathname-coding-system)))
391 (while (string-match "[^-0-9a-zA-Z_:/.]" file pos)
392 (setq buf (cons (format "%%%02x" (aref file (match-beginning 0)))
393 (cons (substring file pos (match-beginning 0)) buf))
395 (apply (function concat)
396 (nreverse (cons (substring file pos) buf)))))
398 (defun nneething-decode-file-name (file &optional coding-system)
399 "Decode the name of the FILE is encoded in CODING-SYSTEM."
401 (while (string-match "%\\([0-9a-fA-F][0-9a-fA-F]\\)" file pos)
402 (setq buf (cons (string (string-to-number (match-string 1 file) 16))
403 (cons (substring file pos (match-beginning 0)) buf))
405 (decode-coding-string
406 (apply (function concat)
407 (nreverse (cons (substring file pos) buf)))
408 (or coding-system nnmail-pathname-coding-system))))
410 (defun nneething-get-file-name (id)
411 "Extract the file name from the message ID string."
412 (when (string-match "\\`<nneething-\\([^@]+\\)@.*>\\'" id)
413 (nneething-decode-file-name (match-string 1 id))))
415 (defun nneething-make-head (file &optional buffer extra-msg
416 mime-type mime-charset mime-encoding)
417 "Create a head by looking at the file attributes of FILE."
418 (let ((atts (file-attributes file)))
420 "Subject: " (file-name-nondirectory file) (or extra-msg "") "\n"
421 "Message-ID: <nneething-" (nneething-encode-file-name file)
422 "@" (system-name) ">\n"
423 (if (equal '(0 0) (nth 5 atts)) ""
424 (concat "Date: " (current-time-string (nth 5 atts)) "\n"))
428 (when (re-search-forward "<[a-zA-Z0-9_]@[-a-zA-Z0-9_]>" 1000 t)
429 (concat "From: " (match-string 0) "\n"))))
430 (nneething-from-line (nth 2 atts) file))
431 (if (> (string-to-number (int-to-string (nth 7 atts))) 0)
432 (concat "Chars: " (int-to-string (nth 7 atts)) "\n")
437 (concat "Lines: " (int-to-string
438 (count-lines (point-min) (point-max)))
442 (concat "Content-Type: " mime-type
445 (if (stringp mime-charset)
447 (symbol-name mime-charset)))
450 (concat "\nContent-Transfer-Encoding: " mime-encoding)
452 "\nMIME-Version: 1.0\n")
455 (defun nneething-from-line (uid &optional file)
456 "Return a From header based of UID."
457 (let* ((login (condition-case nil
458 (user-login-name uid)
460 (cond ((= uid (user-uid)) (user-login-name))
462 (t (int-to-string uid))))))
463 (name (condition-case nil
466 (cond ((= uid (user-uid)) (user-full-name))
467 ((zerop uid) "Ms. Root")))))
468 (host (if (string-match "\\`/[^/@]*@\\([^:/]+\\):" file)
474 "/\\(users\\|home\\)/\\([^/]+\\)/" file)
475 (setq login (substring file
480 (concat "From: " login "@" host
481 (if name (concat " (" name ")") "") "\n")))
483 (defun nneething-get-head (file)
484 "Either find the head in FILE or make a head for FILE."
486 (set-buffer (get-buffer-create nneething-work-buffer))
487 (setq case-fold-search nil)
488 (buffer-disable-undo)
491 ((not (file-exists-p file))
492 ;; The file do not exist.
494 ((or (file-directory-p file)
495 (file-symlink-p file))
496 ;; It's a dir, so we fudge a head.
497 (nneething-make-head file) t)
499 ;; We examine the file.
502 (nnheader-insert-head file)
503 (if (nnheader-article-p)
506 (goto-char (point-min))
507 (or (and (search-forward "\n\n" nil t)
511 (goto-char (point-min))
512 (nneething-make-head file (current-buffer))
513 (delete-region (point) (point-max))))
515 (nneething-make-head file (current-buffer) " (unreadable)")))
518 (defun nneething-file-name (article)
519 "Return the file name of ARTICLE."
520 (let ((dir (file-name-as-directory nneething-address))
522 (if (numberp article)
523 (if (setq fname (cadr (assq article nneething-map)))
524 (expand-file-name fname dir)
525 (make-temp-name (expand-file-name "nneething" dir)))
526 (expand-file-name article dir))))
530 ;;; nneething.el ends here