24357
|
1 ;;; nneething.el --- arbitrary file access for Gnus
|
31716
|
2
|
|
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
|
|
4 ;; Free Software Foundation, Inc.
|
17493
|
5
|
24357
|
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
|
17493
|
7 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
|
|
8 ;; Keywords: news, mail
|
|
9
|
|
10 ;; This file is part of GNU Emacs.
|
|
11
|
|
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)
|
|
15 ;; any later version.
|
|
16
|
|
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.
|
|
21
|
|
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., 59 Temple Place - Suite 330,
|
|
25 ;; Boston, MA 02111-1307, USA.
|
|
26
|
|
27 ;;; Commentary:
|
|
28
|
|
29 ;;; Code:
|
|
30
|
19522
|
31 (eval-when-compile (require 'cl))
|
|
32
|
17493
|
33 (require 'nnheader)
|
|
34 (require 'nnmail)
|
|
35 (require 'nnoo)
|
|
36 (require 'gnus-util)
|
|
37
|
|
38 (nnoo-declare nneething)
|
|
39
|
|
40 (defvoo nneething-map-file-directory "~/.nneething/"
|
|
41 "Where nneething stores the map files.")
|
|
42
|
|
43 (defvoo nneething-map-file ".nneething"
|
|
44 "Name of the map files.")
|
|
45
|
|
46 (defvoo nneething-exclude-files nil
|
|
47 "Regexp saying what files to exclude from the group.
|
|
48 If this variable is nil, no files will be excluded.")
|
|
49
|
31716
|
50 (defvoo nneething-include-files nil
|
|
51 "Regexp saying what files to include in the group.
|
|
52 If this variable is non-nil, only files matching this regexp will be
|
|
53 included.")
|
|
54
|
17493
|
55
|
|
56
|
|
57 ;;; Internal variables.
|
|
58
|
|
59 (defconst nneething-version "nneething 1.0"
|
|
60 "nneething version.")
|
|
61
|
|
62 (defvoo nneething-current-directory nil
|
|
63 "Current news group directory.")
|
|
64
|
|
65 (defvoo nneething-status-string "")
|
|
66
|
|
67 (defvoo nneething-message-id-number 0)
|
|
68 (defvoo nneething-work-buffer " *nneething work*")
|
|
69
|
|
70 (defvoo nneething-group nil)
|
|
71 (defvoo nneething-map nil)
|
|
72 (defvoo nneething-read-only nil)
|
|
73 (defvoo nneething-active nil)
|
24357
|
74 (defvoo nneething-address nil)
|
17493
|
75
|
|
76
|
|
77
|
|
78 ;;; Interface functions.
|
|
79
|
|
80 (nnoo-define-basics nneething)
|
|
81
|
|
82 (deffoo nneething-retrieve-headers (articles &optional group server fetch-old)
|
|
83 (nneething-possibly-change-directory group)
|
|
84
|
|
85 (save-excursion
|
|
86 (set-buffer nntp-server-buffer)
|
|
87 (erase-buffer)
|
|
88 (let* ((number (length articles))
|
|
89 (count 0)
|
|
90 (large (and (numberp nnmail-large-newsgroup)
|
|
91 (> number nnmail-large-newsgroup)))
|
|
92 article file)
|
|
93
|
|
94 (if (stringp (car articles))
|
|
95 'headers
|
|
96
|
|
97 (while (setq article (pop articles))
|
|
98 (setq file (nneething-file-name article))
|
|
99
|
|
100 (when (and (file-exists-p file)
|
|
101 (or (file-directory-p file)
|
|
102 (not (zerop (nnheader-file-size file)))))
|
|
103 (insert (format "221 %d Article retrieved.\n" article))
|
|
104 (nneething-insert-head file)
|
|
105 (insert ".\n"))
|
|
106
|
|
107 (incf count)
|
|
108
|
|
109 (and large
|
|
110 (zerop (% count 20))
|
24357
|
111 (nnheader-message 5 "nneething: Receiving headers... %d%%"
|
31716
|
112 (/ (* count 100) number))))
|
17493
|
113
|
|
114 (when large
|
24357
|
115 (nnheader-message 5 "nneething: Receiving headers...done"))
|
17493
|
116
|
|
117 (nnheader-fold-continuation-lines)
|
|
118 'headers))))
|
|
119
|
|
120 (deffoo nneething-request-article (id &optional group server buffer)
|
|
121 (nneething-possibly-change-directory group)
|
|
122 (let ((file (unless (stringp id)
|
|
123 (nneething-file-name id)))
|
|
124 (nntp-server-buffer (or buffer nntp-server-buffer)))
|
|
125 (and (stringp file) ; We did not request by Message-ID.
|
|
126 (file-exists-p file) ; The file exists.
|
|
127 (not (file-directory-p file)) ; It's not a dir.
|
|
128 (save-excursion
|
|
129 (nnmail-find-file file) ; Insert the file in the nntp buf.
|
|
130 (unless (nnheader-article-p) ; Either it's a real article...
|
|
131 (goto-char (point-min))
|
31716
|
132 (nneething-make-head
|
|
133 file (current-buffer)) ; ... or we fake some headers.
|
17493
|
134 (insert "\n"))
|
|
135 t))))
|
|
136
|
|
137 (deffoo nneething-request-group (group &optional server dont-check)
|
|
138 (nneething-possibly-change-directory group server)
|
|
139 (unless dont-check
|
|
140 (nneething-create-mapping)
|
|
141 (if (> (car nneething-active) (cdr nneething-active))
|
|
142 (nnheader-insert "211 0 1 0 %s\n" group)
|
|
143 (nnheader-insert
|
|
144 "211 %d %d %d %s\n"
|
|
145 (- (1+ (cdr nneething-active)) (car nneething-active))
|
|
146 (car nneething-active) (cdr nneething-active)
|
|
147 group)))
|
|
148 t)
|
|
149
|
|
150 (deffoo nneething-request-list (&optional server dir)
|
|
151 (nnheader-report 'nneething "LIST is not implemented."))
|
|
152
|
|
153 (deffoo nneething-request-newgroups (date &optional server)
|
|
154 (nnheader-report 'nneething "NEWSGROUPS is not implemented."))
|
|
155
|
|
156 (deffoo nneething-request-type (group &optional article)
|
|
157 'unknown)
|
|
158
|
|
159 (deffoo nneething-close-group (group &optional server)
|
|
160 (setq nneething-current-directory nil)
|
|
161 t)
|
|
162
|
|
163 (deffoo nneething-open-server (server &optional defs)
|
|
164 (nnheader-init-server-buffer)
|
|
165 (if (nneething-server-opened server)
|
|
166 t
|
24357
|
167 (unless (assq 'nneething-address defs)
|
|
168 (setq defs (append defs (list (list 'nneething-address server)))))
|
17493
|
169 (nnoo-change-server 'nneething server defs)))
|
|
170
|
|
171
|
|
172 ;;; Internal functions.
|
|
173
|
|
174 (defun nneething-possibly-change-directory (group &optional server)
|
|
175 (when (and server
|
|
176 (not (nneething-server-opened server)))
|
|
177 (nneething-open-server server))
|
|
178 (when (and group
|
|
179 (not (equal nneething-group group)))
|
|
180 (setq nneething-group group)
|
|
181 (setq nneething-map nil)
|
|
182 (setq nneething-active (cons 1 0))
|
|
183 (nneething-create-mapping)))
|
|
184
|
|
185 (defun nneething-map-file ()
|
|
186 ;; We make sure that the .nneething directory exists.
|
|
187 (gnus-make-directory nneething-map-file-directory)
|
|
188 ;; We store it in a special directory under the user's home dir.
|
|
189 (concat (file-name-as-directory nneething-map-file-directory)
|
|
190 nneething-group nneething-map-file))
|
|
191
|
|
192 (defun nneething-create-mapping ()
|
|
193 ;; Read nneething-active and nneething-map.
|
24357
|
194 (when (file-exists-p nneething-address)
|
17493
|
195 (let ((map-file (nneething-map-file))
|
24357
|
196 (files (directory-files nneething-address))
|
17493
|
197 touched map-files)
|
|
198 (when (file-exists-p map-file)
|
|
199 (ignore-errors
|
|
200 (load map-file nil t t)))
|
|
201 (unless nneething-active
|
|
202 (setq nneething-active (cons 1 0)))
|
|
203 ;; Old nneething had a different map format.
|
|
204 (when (and (cdar nneething-map)
|
|
205 (atom (cdar nneething-map)))
|
|
206 (setq nneething-map
|
|
207 (mapcar (lambda (n)
|
|
208 (list (cdr n) (car n)
|
|
209 (nth 5 (file-attributes
|
|
210 (nneething-file-name (car n))))))
|
|
211 nneething-map)))
|
|
212 ;; Remove files matching the exclusion regexp.
|
|
213 (when nneething-exclude-files
|
|
214 (let ((f files)
|
|
215 prev)
|
|
216 (while f
|
|
217 (if (string-match nneething-exclude-files (car f))
|
|
218 (if prev (setcdr prev (cdr f))
|
|
219 (setq files (cdr files)))
|
|
220 (setq prev f))
|
|
221 (setq f (cdr f)))))
|
31716
|
222 ;; Remove files not matching the inclusion regexp.
|
|
223 (when nneething-include-files
|
|
224 (let ((f files)
|
|
225 prev)
|
|
226 (while f
|
|
227 (if (not (string-match nneething-include-files (car f)))
|
|
228 (if prev (setcdr prev (cdr f))
|
|
229 (setq files (cdr files)))
|
|
230 (setq prev f))
|
|
231 (setq f (cdr f)))))
|
17493
|
232 ;; Remove deleted files from the map.
|
|
233 (let ((map nneething-map)
|
|
234 prev)
|
|
235 (while map
|
31716
|
236 (if (and (member (cadr (car map)) files)
|
17493
|
237 ;; We also remove files that have changed mod times.
|
|
238 (equal (nth 5 (file-attributes
|
31716
|
239 (nneething-file-name (cadr (car map)))))
|
|
240 (cadr (cdar map))))
|
17493
|
241 (progn
|
31716
|
242 (push (cadr (car map)) map-files)
|
17493
|
243 (setq prev map))
|
|
244 (setq touched t)
|
|
245 (if prev
|
|
246 (setcdr prev (cdr map))
|
|
247 (setq nneething-map (cdr nneething-map))))
|
|
248 (setq map (cdr map))))
|
|
249 ;; Find all new files and enter them into the map.
|
|
250 (while files
|
|
251 (unless (member (car files) map-files)
|
|
252 ;; This file is not in the map, so we enter it.
|
|
253 (setq touched t)
|
|
254 (setcdr nneething-active (1+ (cdr nneething-active)))
|
|
255 (push (list (cdr nneething-active) (car files)
|
|
256 (nth 5 (file-attributes
|
|
257 (nneething-file-name (car files)))))
|
|
258 nneething-map))
|
|
259 (setq files (cdr files)))
|
|
260 (when (and touched
|
|
261 (not nneething-read-only))
|
31716
|
262 (with-temp-file map-file
|
17493
|
263 (insert "(setq nneething-map '")
|
|
264 (gnus-prin1 nneething-map)
|
|
265 (insert ")\n(setq nneething-active '")
|
|
266 (gnus-prin1 nneething-active)
|
|
267 (insert ")\n"))))))
|
|
268
|
|
269 (defun nneething-insert-head (file)
|
|
270 "Insert the head of FILE."
|
|
271 (when (nneething-get-head file)
|
|
272 (insert-buffer-substring nneething-work-buffer)
|
|
273 (goto-char (point-max))))
|
|
274
|
|
275 (defun nneething-make-head (file &optional buffer)
|
|
276 "Create a head by looking at the file attributes of FILE."
|
|
277 (let ((atts (file-attributes file)))
|
|
278 (insert
|
|
279 "Subject: " (file-name-nondirectory file) "\n"
|
|
280 "Message-ID: <nneething-"
|
|
281 (int-to-string (incf nneething-message-id-number))
|
|
282 "@" (system-name) ">\n"
|
|
283 (if (equal '(0 0) (nth 5 atts)) ""
|
|
284 (concat "Date: " (current-time-string (nth 5 atts)) "\n"))
|
|
285 (or (when buffer
|
|
286 (save-excursion
|
|
287 (set-buffer buffer)
|
|
288 (when (re-search-forward "<[a-zA-Z0-9_]@[-a-zA-Z0-9_]>" 1000 t)
|
|
289 (concat "From: " (match-string 0) "\n"))))
|
|
290 (nneething-from-line (nth 2 atts) file))
|
|
291 (if (> (string-to-int (int-to-string (nth 7 atts))) 0)
|
|
292 (concat "Chars: " (int-to-string (nth 7 atts)) "\n")
|
|
293 "")
|
|
294 (if buffer
|
|
295 (save-excursion
|
|
296 (set-buffer buffer)
|
|
297 (concat "Lines: " (int-to-string
|
|
298 (count-lines (point-min) (point-max)))
|
|
299 "\n"))
|
31716
|
300 ""))))
|
17493
|
301
|
|
302 (defun nneething-from-line (uid &optional file)
|
|
303 "Return a From header based of UID."
|
|
304 (let* ((login (condition-case nil
|
|
305 (user-login-name uid)
|
|
306 (error
|
|
307 (cond ((= uid (user-uid)) (user-login-name))
|
|
308 ((zerop uid) "root")
|
|
309 (t (int-to-string uid))))))
|
|
310 (name (condition-case nil
|
|
311 (user-full-name uid)
|
|
312 (error
|
|
313 (cond ((= uid (user-uid)) (user-full-name))
|
|
314 ((zerop uid) "Ms. Root")))))
|
|
315 (host (if (string-match "\\`/[^/@]*@\\([^:/]+\\):" file)
|
|
316 (prog1
|
|
317 (substring file
|
|
318 (match-beginning 1)
|
|
319 (match-end 1))
|
31716
|
320 (when (string-match
|
|
321 "/\\(users\\|home\\)/\\([^/]+\\)/" file)
|
17493
|
322 (setq login (substring file
|
|
323 (match-beginning 2)
|
|
324 (match-end 2))
|
|
325 name nil)))
|
|
326 (system-name))))
|
|
327 (concat "From: " login "@" host
|
|
328 (if name (concat " (" name ")") "") "\n")))
|
|
329
|
|
330 (defun nneething-get-head (file)
|
|
331 "Either find the head in FILE or make a head for FILE."
|
|
332 (save-excursion
|
|
333 (set-buffer (get-buffer-create nneething-work-buffer))
|
|
334 (setq case-fold-search nil)
|
31716
|
335 (buffer-disable-undo)
|
17493
|
336 (erase-buffer)
|
|
337 (cond
|
|
338 ((not (file-exists-p file))
|
|
339 ;; The file do not exist.
|
|
340 nil)
|
|
341 ((or (file-directory-p file)
|
|
342 (file-symlink-p file))
|
|
343 ;; It's a dir, so we fudge a head.
|
|
344 (nneething-make-head file) t)
|
|
345 (t
|
|
346 ;; We examine the file.
|
|
347 (nnheader-insert-head file)
|
|
348 (if (nnheader-article-p)
|
|
349 (delete-region
|
|
350 (progn
|
|
351 (goto-char (point-min))
|
|
352 (or (and (search-forward "\n\n" nil t)
|
|
353 (1- (point)))
|
|
354 (point-max)))
|
|
355 (point-max))
|
|
356 (goto-char (point-min))
|
|
357 (nneething-make-head file (current-buffer))
|
|
358 (delete-region (point) (point-max)))
|
|
359 t))))
|
|
360
|
|
361 (defun nneething-file-name (article)
|
|
362 "Return the file name of ARTICLE."
|
31716
|
363 (let ((dir (file-name-as-directory nneething-address))
|
|
364 fname)
|
|
365 (if (numberp article)
|
|
366 (if (setq fname (cadr (assq article nneething-map)))
|
|
367 (expand-file-name fname dir)
|
44075
|
368 (mm-make-temp-file (expand-file-name "nneething" dir)))
|
31716
|
369 (expand-file-name article dir))))
|
17493
|
370
|
|
371 (provide 'nneething)
|
|
372
|
|
373 ;;; nneething.el ends here
|