1 ;;; nneething.el --- arbitrary file access for Gnus
2 ;; Copyright (C) 1995,96,97,98 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.")
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-address nil
)
71 (autoload 'gnus-encode-coding-string
"gnus-ems")
73 ;;; Interface functions.
75 (nnoo-define-basics nneething
)
77 (deffoo nneething-retrieve-headers
(articles &optional group server fetch-old
)
78 (nneething-possibly-change-directory group
)
81 (set-buffer nntp-server-buffer
)
83 (let* ((number (length articles
))
85 (large (and (numberp nnmail-large-newsgroup
)
86 (> number nnmail-large-newsgroup
)))
89 (if (stringp (car articles
))
92 (while (setq article
(pop articles
))
93 (setq file
(nneething-file-name article
))
95 (when (and (file-exists-p file
)
96 (or (file-directory-p file
)
97 (not (zerop (nnheader-file-size file
)))))
98 (insert (format "221 %d Article retrieved.\n" article
))
99 (nneething-insert-head file
)
106 (nnheader-message 5 "nneething: Receiving headers... %d%%"
107 (/ (* count
100) number
))))
110 (nnheader-message 5 "nneething: Receiving headers...done"))
112 (nnheader-fold-continuation-lines)
115 (deffoo nneething-request-article
(id &optional group server buffer
)
116 (nneething-possibly-change-directory group
)
117 (let ((file (unless (stringp id
)
118 (nneething-file-name id
)))
119 (nntp-server-buffer (or buffer nntp-server-buffer
)))
120 (and (stringp file
) ; We did not request by Message-ID.
121 (file-exists-p file
) ; The file exists.
122 (not (file-directory-p file
)) ; It's not a dir.
124 (nnmail-find-file file
) ; Insert the file in the nntp buf.
125 (unless (nnheader-article-p) ; Either it's a real article...
126 (goto-char (point-min))
127 (nneething-make-head file
(current-buffer)) ; ... or we fake some headers.
131 (deffoo nneething-request-group
(group &optional server dont-check
)
132 (nneething-possibly-change-directory group server
)
134 (nneething-create-mapping)
135 (if (> (car nneething-active
) (cdr nneething-active
))
136 (nnheader-insert "211 0 1 0 %s\n" group
)
139 (- (1+ (cdr nneething-active
)) (car nneething-active
))
140 (car nneething-active
) (cdr nneething-active
)
144 (deffoo nneething-request-list
(&optional server dir
)
145 (nnheader-report 'nneething
"LIST is not implemented."))
147 (deffoo nneething-request-newgroups
(date &optional server
)
148 (nnheader-report 'nneething
"NEWSGROUPS is not implemented."))
150 (deffoo nneething-request-type
(group &optional article
)
153 (deffoo nneething-close-group
(group &optional server
)
154 (setq nneething-current-directory nil
)
157 (deffoo nneething-open-server
(server &optional defs
)
158 (nnheader-init-server-buffer)
159 (if (nneething-server-opened server
)
161 (unless (assq 'nneething-address defs
)
162 (setq defs
(append defs
(list (list 'nneething-address server
)))))
163 (nnoo-change-server 'nneething server defs
)))
166 ;;; Internal functions.
168 (defun nneething-possibly-change-directory (group &optional server
)
170 (not (nneething-server-opened server
)))
171 (nneething-open-server server
))
173 (not (equal nneething-group group
)))
174 (setq nneething-group group
)
175 (setq nneething-map nil
)
176 (setq nneething-active
(cons 1 0))
177 (nneething-create-mapping)))
179 (defun nneething-map-file ()
180 ;; We make sure that the .nneething directory exists.
181 (gnus-make-directory nneething-map-file-directory
)
182 ;; We store it in a special directory under the user's home dir.
183 (concat (file-name-as-directory nneething-map-file-directory
)
184 nneething-group nneething-map-file
))
186 (defun nneething-create-mapping ()
187 ;; Read nneething-active and nneething-map.
188 (when (file-exists-p nneething-address
)
189 (let ((map-file (nneething-map-file))
190 (files (directory-files nneething-address
))
192 (when (file-exists-p map-file
)
194 (load map-file nil t t
)))
195 (unless nneething-active
196 (setq nneething-active
(cons 1 0)))
197 ;; Old nneething had a different map format.
198 (when (and (cdar nneething-map
)
199 (atom (cdar nneething-map
)))
202 (list (cdr n
) (car n
)
203 (nth 5 (file-attributes
204 (nneething-file-name (car n
))))))
206 ;; Remove files matching the exclusion regexp.
207 (when nneething-exclude-files
211 (if (string-match nneething-exclude-files
(car f
))
212 (if prev
(setcdr prev
(cdr f
))
213 (setq files
(cdr files
)))
216 ;; Remove deleted files from the map.
217 (let ((map nneething-map
)
220 (if (and (member (cadar map
) files
)
221 ;; We also remove files that have changed mod times.
222 (equal (nth 5 (file-attributes
223 (nneething-file-name (cadar map
))))
226 (push (cadar map
) map-files
)
230 (setcdr prev
(cdr map
))
231 (setq nneething-map
(cdr nneething-map
))))
232 (setq map
(cdr map
))))
233 ;; Find all new files and enter them into the map.
235 (unless (member (car files
) map-files
)
236 ;; This file is not in the map, so we enter it.
238 (setcdr nneething-active
(1+ (cdr nneething-active
)))
239 (push (list (cdr nneething-active
) (car files
)
240 (nth 5 (file-attributes
241 (nneething-file-name (car files
)))))
243 (setq files
(cdr files
)))
245 (not nneething-read-only
))
246 (nnheader-temp-write map-file
247 (insert "(setq nneething-map '")
248 (gnus-prin1 nneething-map
)
249 (insert ")\n(setq nneething-active '")
250 (gnus-prin1 nneething-active
)
253 (defun nneething-insert-head (file)
254 "Insert the head of FILE."
255 (when (nneething-get-head file
)
256 (insert-buffer-substring nneething-work-buffer
)
257 (goto-char (point-max))))
259 (defun nneething-make-head (file &optional buffer
)
260 "Create a head by looking at the file attributes of FILE."
261 (let ((atts (file-attributes file
)))
263 "Subject: " (file-name-nondirectory file
) "\n"
264 "Message-ID: <nneething-"
265 (int-to-string (incf nneething-message-id-number
))
266 "@" (system-name) ">\n"
267 (if (equal '(0 0) (nth 5 atts
)) ""
268 (concat "Date: " (current-time-string (nth 5 atts
)) "\n"))
272 (when (re-search-forward "<[a-zA-Z0-9_]@[-a-zA-Z0-9_]>" 1000 t
)
273 (concat "From: " (match-string 0) "\n"))))
274 (nneething-from-line (nth 2 atts
) file
))
275 (if (> (string-to-int (int-to-string (nth 7 atts
))) 0)
276 (concat "Chars: " (int-to-string (nth 7 atts
)) "\n")
281 (concat "Lines: " (int-to-string
282 (count-lines (point-min) (point-max)))
287 (defun nneething-from-line (uid &optional file
)
288 "Return a From header based of UID."
289 (let* ((login (condition-case nil
290 (user-login-name uid
)
292 (cond ((= uid
(user-uid)) (user-login-name))
294 (t (int-to-string uid
))))))
295 (name (condition-case nil
298 (cond ((= uid
(user-uid)) (user-full-name))
299 ((zerop uid
) "Ms. Root")))))
300 (host (if (string-match "\\`/[^/@]*@\\([^:/]+\\):" file
)
305 (when (string-match "/\\(users\\|home\\)/\\([^/]+\\)/" file
)
306 (setq login
(substring file
311 (concat "From: " login
"@" host
312 (if name
(concat " (" name
")") "") "\n")))
314 (defun nneething-get-head (file)
315 "Either find the head in FILE or make a head for FILE."
317 (set-buffer (get-buffer-create nneething-work-buffer
))
318 (setq case-fold-search nil
)
319 (buffer-disable-undo (current-buffer))
322 ((not (file-exists-p file
))
323 ;; The file do not exist.
325 ((or (file-directory-p file
)
326 (file-symlink-p file
))
327 ;; It's a dir, so we fudge a head.
328 (nneething-make-head file
) t
)
330 ;; We examine the file.
331 (nnheader-insert-head file
)
332 (if (nnheader-article-p)
335 (goto-char (point-min))
336 (or (and (search-forward "\n\n" nil t
)
340 (goto-char (point-min))
341 (nneething-make-head file
(current-buffer))
342 (delete-region (point) (point-max)))
345 (defun nneething-file-name (article)
346 "Return the file name of ARTICLE."
347 (concat (file-name-as-directory nneething-address
)
348 (if (numberp article
)
349 (cadr (assq article nneething-map
))
354 ;;; nneething.el ends here