1 ;;; nneething.el --- random file access for Gnus
2 ;; Copyright (C) 1995,96,97 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
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.")
50 ;;; Internal variables.
52 (defconst nneething-version "nneething 1.0"
55 (defvoo nneething-current-directory nil
56 "Current news group directory.")
58 (defvoo nneething-status-string "")
60 (defvoo nneething-message-id-number 0)
61 (defvoo nneething-work-buffer " *nneething work*")
63 (defvoo nneething-group nil)
64 (defvoo nneething-map nil)
65 (defvoo nneething-read-only nil)
66 (defvoo nneething-active nil)
67 (defvoo nneething-directory nil)
71 ;;; Interface functions.
73 (nnoo-define-basics nneething)
75 (deffoo nneething-retrieve-headers (articles &optional group server fetch-old)
76 (nneething-possibly-change-directory group)
79 (set-buffer nntp-server-buffer)
81 (let* ((number (length articles))
83 (large (and (numberp nnmail-large-newsgroup)
84 (> number nnmail-large-newsgroup)))
87 (if (stringp (car articles))
90 (while (setq article (pop articles))
91 (setq file (nneething-file-name article))
93 (when (and (file-exists-p file)
94 (or (file-directory-p file)
95 (not (zerop (nnheader-file-size file)))))
96 (insert (format "221 %d Article retrieved.\n" article))
97 (nneething-insert-head file)
104 (message "nneething: Receiving headers... %d%%"
105 (/ (* count 100) number))))
108 (message "nneething: Receiving headers...done"))
110 (nnheader-fold-continuation-lines)
113 (deffoo nneething-request-article (id &optional group server buffer)
114 (nneething-possibly-change-directory group)
115 (let ((file (unless (stringp id)
116 (nneething-file-name id)))
117 (nntp-server-buffer (or buffer nntp-server-buffer)))
118 (and (stringp file) ; We did not request by Message-ID.
119 (file-exists-p file) ; The file exists.
120 (not (file-directory-p file)) ; It's not a dir.
122 (nnmail-find-file file) ; Insert the file in the nntp buf.
123 (unless (nnheader-article-p) ; Either it's a real article...
124 (goto-char (point-min))
125 (nneething-make-head file (current-buffer)) ; ... or we fake some headers.
129 (deffoo nneething-request-group (group &optional server dont-check)
130 (nneething-possibly-change-directory group server)
132 (nneething-create-mapping)
133 (if (> (car nneething-active) (cdr nneething-active))
134 (nnheader-insert "211 0 1 0 %s\n" group)
137 (- (1+ (cdr nneething-active)) (car nneething-active))
138 (car nneething-active) (cdr nneething-active)
142 (deffoo nneething-request-list (&optional server dir)
143 (nnheader-report 'nneething "LIST is not implemented."))
145 (deffoo nneething-request-newgroups (date &optional server)
146 (nnheader-report 'nneething "NEWSGROUPS is not implemented."))
148 (deffoo nneething-request-type (group &optional article)
151 (deffoo nneething-close-group (group &optional server)
152 (setq nneething-current-directory nil)
155 (deffoo nneething-open-server (server &optional defs)
156 (nnheader-init-server-buffer)
157 (if (nneething-server-opened server)
159 (unless (assq 'nneething-directory defs)
160 (setq defs (append defs (list (list 'nneething-directory server)))))
161 (nnoo-change-server 'nneething server defs)))
164 ;;; Internal functions.
166 (defun nneething-possibly-change-directory (group &optional server)
168 (not (nneething-server-opened server)))
169 (nneething-open-server server))
171 (not (equal nneething-group group)))
172 (setq nneething-group group)
173 (setq nneething-map nil)
174 (setq nneething-active (cons 1 0))
175 (nneething-create-mapping)))
177 (defun nneething-map-file ()
178 ;; We make sure that the .nneething directory exists.
179 (gnus-make-directory nneething-map-file-directory)
180 ;; We store it in a special directory under the user's home dir.
181 (concat (file-name-as-directory nneething-map-file-directory)
182 nneething-group nneething-map-file))
184 (defun nneething-create-mapping ()
185 ;; Read nneething-active and nneething-map.
186 (when (file-exists-p nneething-directory)
187 (let ((map-file (nneething-map-file))
188 (files (directory-files nneething-directory))
190 (when (file-exists-p map-file)
192 (load map-file nil t t)))
193 (unless nneething-active
194 (setq nneething-active (cons 1 0)))
195 ;; Old nneething had a different map format.
196 (when (and (cdar nneething-map)
197 (atom (cdar nneething-map)))
200 (list (cdr n) (car n)
201 (nth 5 (file-attributes
202 (nneething-file-name (car n))))))
204 ;; Remove files matching the exclusion regexp.
205 (when nneething-exclude-files
209 (if (string-match nneething-exclude-files (car f))
210 (if prev (setcdr prev (cdr f))
211 (setq files (cdr files)))
214 ;; Remove deleted files from the map.
215 (let ((map nneething-map)
218 (if (and (member (cadar map) files)
219 ;; We also remove files that have changed mod times.
220 (equal (nth 5 (file-attributes
221 (nneething-file-name (cadar map))))
224 (push (cadar map) map-files)
228 (setcdr prev (cdr map))
229 (setq nneething-map (cdr nneething-map))))
230 (setq map (cdr map))))
231 ;; Find all new files and enter them into the map.
233 (unless (member (car files) map-files)
234 ;; This file is not in the map, so we enter it.
236 (setcdr nneething-active (1+ (cdr nneething-active)))
237 (push (list (cdr nneething-active) (car files)
238 (nth 5 (file-attributes
239 (nneething-file-name (car files)))))
241 (setq files (cdr files)))
243 (not nneething-read-only))
244 (nnheader-temp-write map-file
245 (insert "(setq nneething-map '")
246 (gnus-prin1 nneething-map)
247 (insert ")\n(setq nneething-active '")
248 (gnus-prin1 nneething-active)
251 (defun nneething-insert-head (file)
252 "Insert the head of FILE."
253 (when (nneething-get-head file)
254 (insert-buffer-substring nneething-work-buffer)
255 (goto-char (point-max))))
257 (defun nneething-make-head (file &optional buffer)
258 "Create a head by looking at the file attributes of FILE."
259 (let ((atts (file-attributes file)))
261 "Subject: " (file-name-nondirectory file) "\n"
262 "Message-ID: <nneething-"
263 (int-to-string (incf nneething-message-id-number))
264 "@" (system-name) ">\n"
265 (if (equal '(0 0) (nth 5 atts)) ""
266 (concat "Date: " (current-time-string (nth 5 atts)) "\n"))
270 (when (re-search-forward "<[a-zA-Z0-9_]@[-a-zA-Z0-9_]>" 1000 t)
271 (concat "From: " (match-string 0) "\n"))))
272 (nneething-from-line (nth 2 atts) file))
273 (if (> (string-to-int (int-to-string (nth 7 atts))) 0)
274 (concat "Chars: " (int-to-string (nth 7 atts)) "\n")
279 (concat "Lines: " (int-to-string
280 (count-lines (point-min) (point-max)))
285 (defun nneething-from-line (uid &optional file)
286 "Return a From header based of UID."
287 (let* ((login (condition-case nil
288 (user-login-name uid)
290 (cond ((= uid (user-uid)) (user-login-name))
292 (t (int-to-string uid))))))
293 (name (condition-case nil
296 (cond ((= uid (user-uid)) (user-full-name))
297 ((zerop uid) "Ms. Root")))))
298 (host (if (string-match "\\`/[^/@]*@\\([^:/]+\\):" file)
303 (when (string-match "/\\(users\\|home\\)/\\([^/]+\\)/" file)
304 (setq login (substring file
309 (concat "From: " login "@" host
310 (if name (concat " (" name ")") "") "\n")))
312 (defun nneething-get-head (file)
313 "Either find the head in FILE or make a head for FILE."
315 (set-buffer (get-buffer-create nneething-work-buffer))
316 (setq case-fold-search nil)
317 (buffer-disable-undo (current-buffer))
320 ((not (file-exists-p file))
321 ;; The file do not exist.
323 ((or (file-directory-p file)
324 (file-symlink-p file))
325 ;; It's a dir, so we fudge a head.
326 (nneething-make-head file) t)
328 ;; We examine the file.
329 (nnheader-insert-head file)
330 (if (nnheader-article-p)
333 (goto-char (point-min))
334 (or (and (search-forward "\n\n" nil t)
338 (goto-char (point-min))
339 (nneething-make-head file (current-buffer))
340 (delete-region (point) (point-max)))
343 (defun nneething-file-name (article)
344 "Return the file name of ARTICLE."
345 (concat (file-name-as-directory nneething-directory)
346 (if (numberp article)
347 (cadr (assq article nneething-map))
352 ;;; nneething.el ends here