1 ;;; nneething.el --- random file access for Gnus
2 ;; Copyright (C) 1995,96,97 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.
35 (nnoo-declare nneething
)
37 (defvoo nneething-map-file-directory
"~/.nneething/"
38 "Where nneething stores the map files.")
40 (defvoo nneething-map-file
".nneething"
41 "Name of the map files.")
43 (defvoo 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 (defvoo nneething-current-directory nil
55 "Current news group directory.")
57 (defvoo nneething-status-string
"")
59 (defvoo nneething-message-id-number
0)
60 (defvoo nneething-work-buffer
" *nneething work*")
62 (defvoo nneething-group nil
)
63 (defvoo nneething-map nil
)
64 (defvoo nneething-read-only nil
)
65 (defvoo nneething-active nil
)
69 ;;; Interface functions.
71 (nnoo-define-basics nneething
)
73 (deffoo nneething-retrieve-headers
(articles &optional group server fetch-old
)
74 (nneething-possibly-change-directory group
)
77 (set-buffer nntp-server-buffer
)
79 (let* ((number (length articles
))
81 (large (and (numberp nnmail-large-newsgroup
)
82 (> number nnmail-large-newsgroup
)))
85 (if (stringp (car articles
))
88 (while (setq article
(pop articles
))
89 (setq file
(nneething-file-name article
))
91 (when (and (file-exists-p file
)
92 (or (file-directory-p file
)
93 (not (zerop (nnheader-file-size file
)))))
94 (insert (format "221 %d Article retrieved.\n" article
))
95 (nneething-insert-head file
)
102 (message "nneething: Receiving headers... %d%%"
103 (/ (* count
100) number
))))
106 (message "nneething: Receiving headers...done"))
108 (nnheader-fold-continuation-lines)
111 (deffoo nneething-request-article
(id &optional group server buffer
)
112 (nneething-possibly-change-directory group
)
113 (let ((file (unless (stringp id
)
114 (nneething-file-name id
)))
115 (nntp-server-buffer (or buffer nntp-server-buffer
)))
116 (and (stringp file
) ; We did not request by Message-ID.
117 (file-exists-p file
) ; The file exists.
118 (not (file-directory-p file
)) ; It's not a dir.
120 (nnmail-find-file file
) ; Insert the file in the nntp buf.
121 (unless (nnheader-article-p) ; Either it's a real article...
122 (goto-char (point-min))
123 (nneething-make-head file
(current-buffer)) ; ... or we fake some headers.
127 (deffoo nneething-request-group
(group &optional server dont-check
)
128 (nneething-possibly-change-directory group server
)
130 (nneething-create-mapping)
131 (if (> (car nneething-active
) (cdr nneething-active
))
132 (nnheader-insert "211 0 1 0 %s\n" group
)
135 (- (1+ (cdr nneething-active
)) (car nneething-active
))
136 (car nneething-active
) (cdr nneething-active
)
140 (deffoo nneething-request-list
(&optional server dir
)
141 (nnheader-report 'nneething
"LIST is not implemented."))
143 (deffoo nneething-request-newgroups
(date &optional server
)
144 (nnheader-report 'nneething
"NEWSGROUPS is not implemented."))
146 (deffoo nneething-request-type
(group &optional article
)
149 (deffoo nneething-close-group
(group &optional server
)
150 (setq nneething-current-directory nil
)
153 (deffoo nneething-open-server
(server &optional defs
)
154 (nnheader-init-server-buffer)
155 (if (nneething-server-opened server
)
157 (unless (assq 'nneething-directory defs
)
158 (setq defs
(append defs
(list (list 'nneething-directory server
)))))
159 (nnoo-change-server 'nneething server defs
)))
162 ;;; Internal functions.
164 (defun nneething-possibly-change-directory (group &optional server
)
166 (not (nneething-server-opened server
)))
167 (nneething-open-server server
))
169 (not (equal nneething-group group
)))
170 (setq nneething-group group
)
171 (setq nneething-map nil
)
172 (setq nneething-active
(cons 1 0))
173 (nneething-create-mapping)))
175 (defun nneething-map-file ()
176 ;; We make sure that the .nneething directory exists.
177 (gnus-make-directory nneething-map-file-directory
)
178 ;; We store it in a special directory under the user's home dir.
179 (concat (file-name-as-directory nneething-map-file-directory
)
180 nneething-group nneething-map-file
))
182 (defun nneething-create-mapping ()
183 ;; Read nneething-active and nneething-map.
184 (when (file-exists-p nneething-directory
)
185 (let ((map-file (nneething-map-file))
186 (files (directory-files nneething-directory
))
188 (when (file-exists-p map-file
)
190 (load map-file nil t t
)))
191 (unless nneething-active
192 (setq nneething-active
(cons 1 0)))
193 ;; Old nneething had a different map format.
194 (when (and (cdar nneething-map
)
195 (atom (cdar nneething-map
)))
198 (list (cdr n
) (car n
)
199 (nth 5 (file-attributes
200 (nneething-file-name (car n
))))))
202 ;; Remove files matching the exclusion regexp.
203 (when nneething-exclude-files
207 (if (string-match nneething-exclude-files
(car f
))
208 (if prev
(setcdr prev
(cdr f
))
209 (setq files
(cdr files
)))
212 ;; Remove deleted files from the map.
213 (let ((map nneething-map
)
216 (if (and (member (cadar map
) files
)
217 ;; We also remove files that have changed mod times.
218 (equal (nth 5 (file-attributes
219 (nneething-file-name (cadar map
))))
222 (push (cadar map
) map-files
)
226 (setcdr prev
(cdr map
))
227 (setq nneething-map
(cdr nneething-map
))))
228 (setq map
(cdr map
))))
229 ;; Find all new files and enter them into the map.
231 (unless (member (car files
) map-files
)
232 ;; This file is not in the map, so we enter it.
234 (setcdr nneething-active
(1+ (cdr nneething-active
)))
235 (push (list (cdr nneething-active
) (car files
)
236 (nth 5 (file-attributes
237 (nneething-file-name (car files
)))))
239 (setq files
(cdr files
)))
241 (not nneething-read-only
))
242 (nnheader-temp-write map-file
243 (insert "(setq nneething-map '")
244 (gnus-prin1 nneething-map
)
245 (insert ")\n(setq nneething-active '")
246 (gnus-prin1 nneething-active
)
249 (defun nneething-insert-head (file)
250 "Insert the head of FILE."
251 (when (nneething-get-head file
)
252 (insert-buffer-substring nneething-work-buffer
)
253 (goto-char (point-max))))
255 (defun nneething-make-head (file &optional buffer
)
256 "Create a head by looking at the file attributes of FILE."
257 (let ((atts (file-attributes file
)))
259 "Subject: " (file-name-nondirectory file
) "\n"
260 "Message-ID: <nneething-"
261 (int-to-string (incf nneething-message-id-number
))
262 "@" (system-name) ">\n"
263 (if (equal '(0 0) (nth 5 atts
)) ""
264 (concat "Date: " (current-time-string (nth 5 atts
)) "\n"))
268 (when (re-search-forward "<[a-zA-Z0-9_]@[-a-zA-Z0-9_]>" 1000 t
)
269 (concat "From: " (match-string 0) "\n"))))
270 (nneething-from-line (nth 2 atts
) file
))
271 (if (> (string-to-int (int-to-string (nth 7 atts
))) 0)
272 (concat "Chars: " (int-to-string (nth 7 atts
)) "\n")
277 (concat "Lines: " (int-to-string
278 (count-lines (point-min) (point-max)))
283 (defun nneething-from-line (uid &optional file
)
284 "Return a From header based of UID."
285 (let* ((login (condition-case nil
286 (user-login-name uid
)
288 (cond ((= uid
(user-uid)) (user-login-name))
290 (t (int-to-string uid
))))))
291 (name (condition-case nil
294 (cond ((= uid
(user-uid)) (user-full-name))
295 ((zerop uid
) "Ms. Root")))))
296 (host (if (string-match "\\`/[^/@]*@\\([^:/]+\\):" file
)
301 (when (string-match "/\\(users\\|home\\)/\\([^/]+\\)/" file
)
302 (setq login
(substring file
307 (concat "From: " login
"@" host
308 (if name
(concat " (" name
")") "") "\n")))
310 (defun nneething-get-head (file)
311 "Either find the head in FILE or make a head for FILE."
313 (set-buffer (get-buffer-create nneething-work-buffer
))
314 (setq case-fold-search nil
)
315 (buffer-disable-undo (current-buffer))
318 ((not (file-exists-p file
))
319 ;; The file do not exist.
321 ((or (file-directory-p file
)
322 (file-symlink-p file
))
323 ;; It's a dir, so we fudge a head.
324 (nneething-make-head file
) t
)
326 ;; We examine the file.
327 (nnheader-insert-head file
)
328 (if (nnheader-article-p)
331 (goto-char (point-min))
332 (or (and (search-forward "\n\n" nil t
)
336 (goto-char (point-min))
337 (nneething-make-head file
(current-buffer))
338 (delete-region (point) (point-max)))
341 (defun nneething-file-name (article)
342 "Return the file name of ARTICLE."
343 (concat (file-name-as-directory nneething-directory
)
344 (if (numberp article
)
345 (cadr (assq article nneething-map
))
350 ;;; nneething.el ends here