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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
27 ;; For an overview of what the interface functions do, please see the
34 (eval-when-compile (require 'cl))
36 (defvar nneething-map-file-directory "~/.nneething/"
37 "*Map files directory.")
39 (defvar nneething-exclude-files nil
40 "*Regexp saying what files to exclude from the group.
41 If this variable is nil, no files will be excluded.")
43 (defvar nneething-map-file ".nneething"
44 "*Name of map files.")
48 (defconst nneething-version "nneething 1.0"
51 (defvar nneething-current-directory nil
52 "Current news group directory.")
54 (defvar nneething-status-string "")
55 (defvar nneething-group-alist nil)
59 (defvar nneething-directory nil)
60 (defvar nneething-group nil)
61 (defvar nneething-map nil)
62 (defvar nneething-read-only nil)
63 (defvar nneething-active nil)
64 (defvar nneething-server-variables
65 `((nneething-directory ,nneething-directory)
66 (nneething-current-directory nil)
67 (nneething-status-string "")
68 (nneething-group-alist)))
72 ;;; Interface functions.
74 (defun nneething-retrieve-headers (sequence &optional newsgroup server fetch-old)
75 (nneething-possibly-change-directory newsgroup)
78 (set-buffer nntp-server-buffer)
80 (let* ((number (length sequence))
82 (large (and (numberp nnmail-large-newsgroup)
83 (> number nnmail-large-newsgroup)))
86 (if (stringp (car sequence))
90 (setq article (car sequence))
91 (setq file (nneething-file-name article))
93 (if (and (file-exists-p file)
94 (or (file-directory-p file)
95 (not (zerop (nth 7 (file-attributes file))))))
97 (insert (format "221 %d Article retrieved.\n" article))
98 (nneething-insert-head file)
101 (setq sequence (cdr sequence)
106 (message "nneething: Receiving headers... %d%%"
107 (/ (* count 100) number))))
109 (and large (message "nneething: Receiving headers...done"))
111 ;; Fold continuation lines.
112 (goto-char (point-min))
113 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
114 (replace-match " " t t))
117 (defun nneething-open-server (server &optional defs)
118 (setq nneething-status-string "")
119 (nnheader-init-server-buffer))
121 (defun nneething-close-server (&optional server)
124 (defun nneething-server-opened (&optional server)
127 (defun nneething-status-message (&optional server)
128 nneething-status-string)
130 (defun nneething-request-article (id &optional newsgroup server buffer)
131 (nneething-possibly-change-directory newsgroup)
132 (let ((file (if (stringp id) nil (nneething-file-name id)))
133 (nntp-server-buffer (or buffer nntp-server-buffer)))
134 (and (stringp file) ; We did not request by Message-ID.
135 (file-exists-p file) ; The file exists.
136 (not (file-directory-p file)) ; It's not a dir.
138 (nnmail-find-file file) ; Insert the file in the nntp buf.
139 (or (nnheader-article-p) ; Either it's a real article...
141 (goto-char (point-min))
142 (nneething-make-head file) ; ... or we fake some headers.
146 (defun nneething-request-group (group &optional dir dont-check)
147 (nneething-possibly-change-directory group dir)
148 (or dont-check (nneething-create-mapping))
150 (set-buffer nntp-server-buffer)
152 (if (> (car nneething-active) (cdr nneething-active))
153 (insert (format "211 0 1 0 %s\n" group))
154 (insert (format "211 %d %d %d %s\n"
155 (- (1+ (cdr nneething-active)) (car nneething-active))
156 (car nneething-active) (cdr nneething-active)
160 (defun nneething-request-list (&optional server dir)
162 (set-buffer nntp-server-buffer)
166 (defun nneething-request-newgroups (date &optional server)
168 (set-buffer nntp-server-buffer)
172 (defun nneething-request-post (&optional server)
173 (mail-send-and-exit nil))
175 (defun nneething-close-group (group &optional server)
179 ;;; Internal functions.
181 (defun nneething-possibly-change-directory (group &optional dir)
184 (if (and nneething-group
185 (string= group nneething-group))
188 (if (setq entry (assoc group nneething-group-alist))
190 (setq nneething-group group)
191 (setq nneething-directory (nth 1 entry))
192 (setq nneething-map (nth 2 entry))
193 (setq nneething-active (nth 3 entry)))
194 (setq nneething-group group)
195 (setq nneething-directory dir)
196 (setq nneething-map nil)
197 (setq nneething-active (cons 1 0))
198 (nneething-create-mapping)
199 (setq nneething-group-alist
200 (cons (list group dir nneething-map nneething-active)
201 nneething-group-alist)))))))
203 (defun nneething-map-file ()
204 ;; We make sure that the .neething directory exists.
205 (or (file-exists-p nneething-map-file-directory)
206 (make-directory nneething-map-file-directory 'parents))
207 ;; We store it in a special directory under the user's home dir.
208 (concat (file-name-as-directory nneething-map-file-directory)
209 nneething-group nneething-map-file))
211 (defun nneething-create-mapping ()
212 ;; Read nneething-active and nneething-map.
213 (let ((map-file (nneething-map-file))
214 (files (directory-files nneething-directory))
216 (if (file-exists-p map-file)
218 (load map-file nil t t)
220 (or nneething-active (setq nneething-active (cons 1 0)))
221 ;; Old nneething had a different map format.
222 (when (and (cdr (car nneething-map))
223 (atom (cdar nneething-map)))
226 (list (cdr n) (car n)
227 (nth 5 (file-attributes
228 (nneething-file-name (car n))))))
230 ;; Remove files matching the exclusion regexp.
231 (when nneething-exclude-files
235 (if (string-match nneething-exclude-files (car f))
236 (if prev (setcdr prev (cdr f))
237 (setq files (cdr files)))
240 ;; Remove deleted files from the map.
241 (let ((map nneething-map)
244 (if (and (member (cadar map) files)
245 ;; We also remove files that have changed mod times.
246 (equal (nth 5 (file-attributes
247 (nneething-file-name (cadar map))))
250 (push (cadar map) map-files)
254 (setcdr prev (cdr map))
255 (setq nneething-map (cdr nneething-map))))
256 (setq map (cdr map))))
257 ;; Find all new files and enter them into the map.
259 (unless (member (car files) map-files)
260 ;; This file is not in the map, so we enter it.
262 (setcdr nneething-active (1+ (cdr nneething-active)))
263 (push (list (cdr nneething-active) (car files)
264 (nth 5 (file-attributes
265 (nneething-file-name (car files)))))
267 (setq files (cdr files)))
269 (not nneething-read-only))
271 (nnheader-set-temp-buffer " *nneething map*")
272 (insert "(setq nneething-map '" (prin1-to-string nneething-map) ")\n"
273 "(setq nneething-active '" (prin1-to-string nneething-active)
275 (write-region (point-min) (point-max) map-file nil 'nomesg)
276 (kill-buffer (current-buffer))))))
278 (defvar nneething-message-id-number 0)
279 (defvar nneething-work-buffer " *nneething work*")
281 (defun nneething-insert-head (file)
282 (and (nneething-get-head file)
283 (insert-buffer-substring nneething-work-buffer)))
285 (defun nneething-make-head (file)
286 (let ((atts (file-attributes file)))
287 (insert "Subject: " (file-name-nondirectory file) "\n"
288 "Message-ID: <nneething-"
290 (setq nneething-message-id-number
291 (1+ nneething-message-id-number)))
292 "@" (system-name) ">\n"
293 "Date: " (current-time-string (nth 5 atts)) "\n"
294 (nneething-from-line (nth 2 atts))
295 "Chars: " (int-to-string (nth 7 atts)) "\n")))
297 (defun nneething-from-line (uid)
298 (let ((login (condition-case nil
299 (user-login-name uid)
301 (cond ((= uid (user-uid)) (user-login-name))
303 (t (int-to-string uid))))))
304 (name (condition-case nil
307 (cond ((= uid (user-uid)) (user-full-name))
308 ((zerop uid) "Ms. Root"))))))
309 (concat "From: " login "@" (system-name)
310 (if name (concat " (" name ")") "") "\n")))
312 (defun nneething-get-head (file)
314 (set-buffer (get-buffer-create nneething-work-buffer))
315 (setq case-fold-search nil)
316 (buffer-disable-undo (current-buffer))
319 ((not (file-exists-p file))
320 ;; The file do not exist.
322 ((or (file-directory-p file)
323 (file-symlink-p file))
324 ;; It's a dir, so we fudge a head.
325 (nneething-make-head file) t)
327 ;; We examine the file.
328 (nnheader-insert-head file)
329 (if (nnheader-article-p)
332 (goto-char (point-min))
333 (or (and (search-forward "\n\n" nil t)
338 (nneething-make-head file))
341 (defun nneething-file-name (article)
342 (concat (file-name-as-directory nneething-directory)
343 (if (numberp article)
344 (cadr (assq article nneething-map))
349 ;;; nneething.el ends here