0
|
1 ;;; nneething.el --- random file access for Gnus
|
98
|
2 ;; Copyright (C) 1995,96,97 Free Software Foundation, Inc.
|
0
|
3
|
|
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
|
|
5 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
|
|
6 ;; Keywords: news, mail
|
|
7
|
|
8 ;; This file is part of GNU Emacs.
|
|
9
|
|
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)
|
|
13 ;; any later version.
|
|
14
|
|
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.
|
|
19
|
|
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.
|
|
24
|
|
25 ;;; Commentary:
|
|
26
|
|
27 ;;; Code:
|
|
28
|
|
29 (require 'nnheader)
|
|
30 (require 'nnmail)
|
|
31 (require 'nnoo)
|
98
|
32 (require 'gnus-util)
|
|
33 (require 'cl)
|
0
|
34
|
|
35 (nnoo-declare nneething)
|
|
36
|
|
37 (defvoo nneething-map-file-directory "~/.nneething/"
|
112
|
38 "Where nneething stores the map files.")
|
0
|
39
|
|
40 (defvoo nneething-map-file ".nneething"
|
112
|
41 "Name of the map files.")
|
0
|
42
|
|
43 (defvoo nneething-exclude-files nil
|
112
|
44 "Regexp saying what files to exclude from the group.
|
0
|
45 If this variable is nil, no files will be excluded.")
|
|
46
|
|
47
|
|
48
|
108
|
49 ;;; Internal variables.
|
0
|
50
|
|
51 (defconst nneething-version "nneething 1.0"
|
|
52 "nneething version.")
|
|
53
|
|
54 (defvoo nneething-current-directory nil
|
|
55 "Current news group directory.")
|
|
56
|
|
57 (defvoo nneething-status-string "")
|
70
|
58 (defvoo nneething-group-alist nil)
|
0
|
59
|
|
60 (defvoo nneething-message-id-number 0)
|
|
61 (defvoo nneething-work-buffer " *nneething work*")
|
|
62
|
|
63 (defvoo nneething-group nil)
|
|
64 (defvoo nneething-map nil)
|
|
65 (defvoo nneething-read-only nil)
|
|
66 (defvoo nneething-active nil)
|
|
67
|
|
68
|
|
69
|
|
70 ;;; Interface functions.
|
|
71
|
|
72 (nnoo-define-basics nneething)
|
|
73
|
|
74 (deffoo nneething-retrieve-headers (articles &optional group server fetch-old)
|
|
75 (nneething-possibly-change-directory group)
|
|
76
|
|
77 (save-excursion
|
|
78 (set-buffer nntp-server-buffer)
|
|
79 (erase-buffer)
|
|
80 (let* ((number (length articles))
|
|
81 (count 0)
|
|
82 (large (and (numberp nnmail-large-newsgroup)
|
|
83 (> number nnmail-large-newsgroup)))
|
|
84 article file)
|
|
85
|
|
86 (if (stringp (car articles))
|
|
87 'headers
|
|
88
|
|
89 (while (setq article (pop articles))
|
|
90 (setq file (nneething-file-name article))
|
|
91
|
|
92 (when (and (file-exists-p file)
|
|
93 (or (file-directory-p file)
|
2
|
94 (not (zerop (nnheader-file-size file)))))
|
0
|
95 (insert (format "221 %d Article retrieved.\n" article))
|
|
96 (nneething-insert-head file)
|
|
97 (insert ".\n"))
|
|
98
|
|
99 (incf count)
|
|
100
|
|
101 (and large
|
|
102 (zerop (% count 20))
|
|
103 (message "nneething: Receiving headers... %d%%"
|
|
104 (/ (* count 100) number))))
|
|
105
|
|
106 (when large
|
|
107 (message "nneething: Receiving headers...done"))
|
|
108
|
|
109 (nnheader-fold-continuation-lines)
|
|
110 'headers))))
|
|
111
|
|
112 (deffoo nneething-request-article (id &optional group server buffer)
|
|
113 (nneething-possibly-change-directory group)
|
98
|
114 (let ((file (unless (stringp id)
|
|
115 (nneething-file-name id)))
|
0
|
116 (nntp-server-buffer (or buffer nntp-server-buffer)))
|
|
117 (and (stringp file) ; We did not request by Message-ID.
|
|
118 (file-exists-p file) ; The file exists.
|
|
119 (not (file-directory-p file)) ; It's not a dir.
|
|
120 (save-excursion
|
|
121 (nnmail-find-file file) ; Insert the file in the nntp buf.
|
98
|
122 (unless (nnheader-article-p) ; Either it's a real article...
|
|
123 (goto-char (point-min))
|
|
124 (nneething-make-head file (current-buffer)) ; ... or we fake some headers.
|
|
125 (insert "\n"))
|
0
|
126 t))))
|
|
127
|
112
|
128 (deffoo nneething-request-group (group &optional server dont-check)
|
|
129 (nneething-possibly-change-directory group server)
|
0
|
130 (unless dont-check
|
|
131 (nneething-create-mapping)
|
|
132 (if (> (car nneething-active) (cdr nneething-active))
|
|
133 (nnheader-insert "211 0 1 0 %s\n" group)
|
|
134 (nnheader-insert
|
108
|
135 "211 %d %d %d %s\n"
|
0
|
136 (- (1+ (cdr nneething-active)) (car nneething-active))
|
|
137 (car nneething-active) (cdr nneething-active)
|
|
138 group)))
|
|
139 t)
|
|
140
|
|
141 (deffoo nneething-request-list (&optional server dir)
|
|
142 (nnheader-report 'nneething "LIST is not implemented."))
|
|
143
|
|
144 (deffoo nneething-request-newgroups (date &optional server)
|
|
145 (nnheader-report 'nneething "NEWSGROUPS is not implemented."))
|
|
146
|
|
147 (deffoo nneething-request-type (group &optional article)
|
|
148 'unknown)
|
|
149
|
|
150 (deffoo nneething-close-group (group &optional server)
|
|
151 (setq nneething-current-directory nil)
|
|
152 t)
|
|
153
|
|
154
|
|
155 ;;; Internal functions.
|
|
156
|
70
|
157 (defun nneething-possibly-change-directory (group &optional dir)
|
|
158 (when group
|
|
159 (if (and nneething-group
|
|
160 (string= group nneething-group))
|
|
161 t
|
|
162 (let (entry)
|
|
163 (if (setq entry (assoc group nneething-group-alist))
|
|
164 (progn
|
|
165 (setq nneething-group group)
|
|
166 (setq nneething-directory (nth 1 entry))
|
|
167 (setq nneething-map (nth 2 entry))
|
|
168 (setq nneething-active (nth 3 entry)))
|
|
169 (setq nneething-group group)
|
|
170 (setq nneething-directory dir)
|
|
171 (setq nneething-map nil)
|
|
172 (setq nneething-active (cons 1 0))
|
|
173 (nneething-create-mapping)
|
|
174 (push (list group dir nneething-map nneething-active)
|
|
175 nneething-group-alist))))))
|
0
|
176
|
|
177 (defun nneething-map-file ()
|
108
|
178 ;; We make sure that the .nneething directory exists.
|
98
|
179 (gnus-make-directory nneething-map-file-directory)
|
0
|
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))
|
|
183
|
|
184 (defun nneething-create-mapping ()
|
|
185 ;; Read nneething-active and nneething-map.
|
110
|
186 (when (file-exists-p nneething-directory)
|
|
187 (let ((map-file (nneething-map-file))
|
|
188 (files (directory-files nneething-directory))
|
|
189 touched map-files)
|
|
190 (when (file-exists-p map-file)
|
|
191 (ignore-errors
|
|
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)))
|
|
198 (setq nneething-map
|
|
199 (mapcar (lambda (n)
|
|
200 (list (cdr n) (car n)
|
|
201 (nth 5 (file-attributes
|
|
202 (nneething-file-name (car n))))))
|
|
203 nneething-map)))
|
|
204 ;; Remove files matching the exclusion regexp.
|
|
205 (when nneething-exclude-files
|
|
206 (let ((f files)
|
|
207 prev)
|
|
208 (while f
|
|
209 (if (string-match nneething-exclude-files (car f))
|
|
210 (if prev (setcdr prev (cdr f))
|
|
211 (setq files (cdr files)))
|
|
212 (setq prev f))
|
|
213 (setq f (cdr f)))))
|
|
214 ;; Remove deleted files from the map.
|
|
215 (let ((map nneething-map)
|
0
|
216 prev)
|
110
|
217 (while 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))))
|
|
222 (caddar map)))
|
|
223 (progn
|
|
224 (push (cadar map) map-files)
|
|
225 (setq prev map))
|
|
226 (setq touched t)
|
|
227 (if prev
|
|
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.
|
|
232 (while files
|
|
233 (unless (member (car files) map-files)
|
|
234 ;; This file is not in the map, so we enter it.
|
0
|
235 (setq touched t)
|
110
|
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)))))
|
|
240 nneething-map))
|
|
241 (setq files (cdr files)))
|
|
242 (when (and touched
|
|
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)
|
|
249 (insert ")\n"))))))
|
0
|
250
|
|
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))))
|
|
256
|
|
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)))
|
108
|
260 (insert
|
0
|
261 "Subject: " (file-name-nondirectory file) "\n"
|
108
|
262 "Message-ID: <nneething-"
|
0
|
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"))
|
98
|
267 (or (when buffer
|
108
|
268 (save-excursion
|
98
|
269 (set-buffer buffer)
|
|
270 (when (re-search-forward "<[a-zA-Z0-9_]@[-a-zA-Z0-9_]>" 1000 t)
|
|
271 (concat "From: " (match-string 0) "\n"))))
|
0
|
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")
|
|
275 "")
|
108
|
276 (if buffer
|
0
|
277 (save-excursion
|
|
278 (set-buffer buffer)
|
108
|
279 (concat "Lines: " (int-to-string
|
98
|
280 (count-lines (point-min) (point-max)))
|
|
281 "\n"))
|
0
|
282 "")
|
|
283 )))
|
|
284
|
|
285 (defun nneething-from-line (uid &optional file)
|
|
286 "Return a From header based of UID."
|
108
|
287 (let* ((login (condition-case nil
|
0
|
288 (user-login-name uid)
|
108
|
289 (error
|
0
|
290 (cond ((= uid (user-uid)) (user-login-name))
|
|
291 ((zerop uid) "root")
|
|
292 (t (int-to-string uid))))))
|
108
|
293 (name (condition-case nil
|
0
|
294 (user-full-name uid)
|
108
|
295 (error
|
0
|
296 (cond ((= uid (user-uid)) (user-full-name))
|
|
297 ((zerop uid) "Ms. Root")))))
|
|
298 (host (if (string-match "\\`/[^/@]*@\\([^:/]+\\):" file)
|
|
299 (prog1
|
108
|
300 (substring file
|
98
|
301 (match-beginning 1)
|
0
|
302 (match-end 1))
|
98
|
303 (when (string-match "/\\(users\\|home\\)/\\([^/]+\\)/" file)
|
|
304 (setq login (substring file
|
|
305 (match-beginning 2)
|
|
306 (match-end 2))
|
|
307 name nil)))
|
0
|
308 (system-name))))
|
108
|
309 (concat "From: " login "@" host
|
0
|
310 (if name (concat " (" name ")") "") "\n")))
|
|
311
|
|
312 (defun nneething-get-head (file)
|
|
313 "Either find the head in FILE or make a head for FILE."
|
|
314 (save-excursion
|
|
315 (set-buffer (get-buffer-create nneething-work-buffer))
|
|
316 (setq case-fold-search nil)
|
|
317 (buffer-disable-undo (current-buffer))
|
|
318 (erase-buffer)
|
108
|
319 (cond
|
0
|
320 ((not (file-exists-p file))
|
108
|
321 ;; The file do not exist.
|
0
|
322 nil)
|
|
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)
|
108
|
327 (t
|
0
|
328 ;; We examine the file.
|
|
329 (nnheader-insert-head file)
|
|
330 (if (nnheader-article-p)
|
108
|
331 (delete-region
|
0
|
332 (progn
|
|
333 (goto-char (point-min))
|
|
334 (or (and (search-forward "\n\n" nil t)
|
|
335 (1- (point)))
|
|
336 (point-max)))
|
|
337 (point-max))
|
|
338 (goto-char (point-min))
|
|
339 (nneething-make-head file (current-buffer))
|
|
340 (delete-region (point) (point-max)))
|
|
341 t))))
|
|
342
|
|
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))
|
|
348 article)))
|
|
349
|
|
350 (provide 'nneething)
|
|
351
|
|
352 ;;; nneething.el ends here
|