1 ;;; nneething.el --- random file access for Gnus
2 ;; Copyright (C) 1995,96 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.
27 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
28 ;; For an overview of what the interface functions do, please see the
35 (eval-when-compile (require 'cl))
37 (defvar nneething-map-file-directory "~/.nneething/"
38 "*Where nneething stores the map files.")
40 (defvar nneething-map-file ".nneething"
41 "*Name of the map files.")
43 (defvar nneething-exclude-files nil
44 "*Regexp saying what files to exclude from the group.
45 If this variable is nil, no files will be excluded.")
49 ;;; Internal variables.
51 (defconst nneething-version "nneething 1.0"
54 (defvar nneething-current-directory nil
55 "Current news group directory.")
57 (defvar nneething-status-string "")
58 (defvar nneething-group-alist nil)
60 (defvar nneething-message-id-number 0)
61 (defvar nneething-work-buffer " *nneething work*")
65 (defvar nneething-directory nil)
66 (defvar nneething-group nil)
67 (defvar nneething-map nil)
68 (defvar nneething-read-only nil)
69 (defvar nneething-active nil)
70 (defvar nneething-server-variables
71 `((nneething-directory ,nneething-directory)
72 (nneething-current-directory nil)
73 (nneething-status-string "")
74 (nneething-group-alist)))
78 ;;; Interface functions.
80 (defun 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 (nth 7 (file-attributes file))))))
101 (insert (format "221 %d Article retrieved.\n" article))
102 (nneething-insert-head file)
109 (message "nneething: Receiving headers... %d%%"
110 (/ (* count 100) number))))
113 (message "nneething: Receiving headers...done"))
115 (nnheader-fold-continuation-lines)
118 (defun nneething-open-server (server &optional defs)
119 (nnheader-report 'nneething "")
120 (nnheader-init-server-buffer))
122 (defun nneething-close-server (&optional server)
123 (setq nneething-current-directory nil)
126 (defun nneething-server-opened (&optional server)
127 nneething-current-directory)
129 (defun nneething-status-message (&optional server)
130 nneething-status-string)
132 (defun nneething-request-article (id &optional group server buffer)
133 (nneething-possibly-change-directory group)
134 (let ((file (unless (stringp id) (nneething-file-name id)))
135 (nntp-server-buffer (or buffer nntp-server-buffer)))
136 (and (stringp file) ; We did not request by Message-ID.
137 (file-exists-p file) ; The file exists.
138 (not (file-directory-p file)) ; It's not a dir.
140 (nnmail-find-file file) ; Insert the file in the nntp buf.
141 (or (nnheader-article-p) ; Either it's a real article...
143 (goto-char (point-min))
144 (nneething-make-head file) ; ... or we fake some headers.
148 (defun nneething-request-group (group &optional dir dont-check)
149 (nneething-possibly-change-directory group dir)
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 (defun nneething-request-list (&optional server dir)
162 (nnheader-report 'nneething "LIST is not implemented."))
164 (defun nneething-request-newgroups (date &optional server)
165 (nnheader-report 'nneething "NEWSGROUPS is not implemented."))
167 (defun nneething-request-type (group &optional article)
170 (defun nneething-close-group (group &optional server)
171 (setq nneething-current-directory nil)
175 ;;; Internal functions.
177 (defun nneething-possibly-change-directory (group &optional dir)
179 (if (and nneething-group
180 (string= group nneething-group))
183 (if (setq entry (assoc group nneething-group-alist))
185 (setq nneething-group group)
186 (setq nneething-directory (nth 1 entry))
187 (setq nneething-map (nth 2 entry))
188 (setq nneething-active (nth 3 entry)))
189 (setq nneething-group group)
190 (setq nneething-directory dir)
191 (setq nneething-map nil)
192 (setq nneething-active (cons 1 0))
193 (nneething-create-mapping)
194 (push (list group dir nneething-map nneething-active)
195 nneething-group-alist))))))
197 (defun nneething-map-file ()
198 ;; We make sure that the .nneething directory exists.
199 (unless (file-exists-p nneething-map-file-directory)
200 (make-directory nneething-map-file-directory 'parents))
201 ;; We store it in a special directory under the user's home dir.
202 (concat (file-name-as-directory nneething-map-file-directory)
203 nneething-group nneething-map-file))
205 (defun nneething-create-mapping ()
206 ;; Read nneething-active and nneething-map.
207 (let ((map-file (nneething-map-file))
208 (files (directory-files nneething-directory))
210 (if (file-exists-p map-file)
212 (load map-file nil t t)
214 (or nneething-active (setq nneething-active (cons 1 0)))
215 ;; Old nneething had a different map format.
216 (when (and (cdr (car nneething-map))
217 (atom (cdar nneething-map)))
220 (list (cdr n) (car n)
221 (nth 5 (file-attributes
222 (nneething-file-name (car n))))))
224 ;; Remove files matching the exclusion regexp.
225 (when nneething-exclude-files
229 (if (string-match nneething-exclude-files (car f))
230 (if prev (setcdr prev (cdr f))
231 (setq files (cdr files)))
234 ;; Remove deleted files from the map.
235 (let ((map nneething-map)
238 (if (and (member (cadar map) files)
239 ;; We also remove files that have changed mod times.
240 (equal (nth 5 (file-attributes
241 (nneething-file-name (cadar map))))
244 (push (cadar map) map-files)
248 (setcdr prev (cdr map))
249 (setq nneething-map (cdr nneething-map))))
250 (setq map (cdr map))))
251 ;; Find all new files and enter them into the map.
253 (unless (member (car files) map-files)
254 ;; This file is not in the map, so we enter it.
256 (setcdr nneething-active (1+ (cdr nneething-active)))
257 (push (list (cdr nneething-active) (car files)
258 (nth 5 (file-attributes
259 (nneething-file-name (car files)))))
261 (setq files (cdr files)))
263 (not nneething-read-only))
265 (nnheader-set-temp-buffer " *nneething map*")
266 (insert "(setq nneething-map '" (prin1-to-string nneething-map) ")\n"
267 "(setq nneething-active '" (prin1-to-string nneething-active)
269 (write-region (point-min) (point-max) map-file nil 'nomesg)
270 (kill-buffer (current-buffer))))))
272 (defun nneething-insert-head (file)
273 "Insert the head of FILE."
274 (when (nneething-get-head file)
275 (insert-buffer-substring nneething-work-buffer)))
277 (defun nneething-make-head (file)
278 "Create a head by looking at the file attributes of FILE."
279 (let ((atts (file-attributes file)))
281 "Subject: " (file-name-nondirectory file) "\n"
282 "Message-ID: <nneething-"
283 (int-to-string (incf nneething-message-id-number))
284 "@" (system-name) ">\n"
285 "Date: " (current-time-string (nth 5 atts)) "\n"
286 (nneething-from-line (nth 2 atts))
287 "Chars: " (int-to-string (nth 7 atts)) "\n")))
289 (defun nneething-from-line (uid)
290 "Return a From header based of UID."
291 (let ((login (condition-case nil
292 (user-login-name uid)
294 (cond ((= uid (user-uid)) (user-login-name))
296 (t (int-to-string uid))))))
297 (name (condition-case nil
300 (cond ((= uid (user-uid)) (user-full-name))
301 ((zerop uid) "Ms. Root"))))))
302 (concat "From: " login "@" (system-name)
303 (if name (concat " (" name ")") "") "\n")))
305 (defun nneething-get-head (file)
306 "Either find the head in FILE or make a head for FILE."
308 (set-buffer (get-buffer-create nneething-work-buffer))
309 (setq case-fold-search nil)
310 (buffer-disable-undo (current-buffer))
313 ((not (file-exists-p file))
314 ;; The file do not exist.
316 ((or (file-directory-p file)
317 (file-symlink-p file))
318 ;; It's a dir, so we fudge a head.
319 (nneething-make-head file) t)
321 ;; We examine the file.
322 (nnheader-insert-head file)
323 (if (nnheader-article-p)
326 (goto-char (point-min))
327 (or (and (search-forward "\n\n" nil t)
332 (nneething-make-head file))
335 (defun nneething-file-name (article)
336 "Return the file name of ARTICLE."
337 (concat (file-name-as-directory nneething-directory)
338 (if (numberp article)
339 (cadr (assq article nneething-map))
344 ;;; nneething.el ends here