1 ;;; nneething.el --- random file access for Gnus
2 ;; Copyright (C) 1995 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
35 (defvar nneething-map-file-directory
"~/.nneething/"
36 "*Map files directory.")
38 (defvar nneething-exclude-files
"~$"
39 "*Regexp saying what files to exclude from the group.")
41 (defvar nneething-map-file
".nneething"
42 "*Name of map files.")
46 (defconst nneething-version
"nneething 1.0"
49 (defvar nneething-current-directory nil
50 "Current news group directory.")
52 (defvar nneething-status-string
"")
53 (defvar nneething-group-alist nil
)
57 (defvar nneething-directory nil
)
58 (defvar nneething-group nil
)
59 (defvar nneething-map nil
)
60 (defvar nneething-read-only nil
)
61 (defvar nneething-active nil
)
62 (defvar nneething-server-variables
64 (list 'nneething-directory nneething-directory
)
65 '(nneething-current-directory nil
)
66 '(nneething-status-string "")
67 '(nneething-group-alist)))
71 ;;; Interface functions.
73 (defun nneething-retrieve-headers (sequence &optional newsgroup server
)
74 (nneething-possibly-change-directory newsgroup
)
77 (set-buffer nntp-server-buffer
)
79 (let* ((number (length sequence
))
81 (large (and (numberp nnmail-large-newsgroup
)
82 (> number nnmail-large-newsgroup
)))
85 (if (stringp (car sequence
))
89 (setq article
(car sequence
))
90 (setq file
(nneething-file-name article
))
92 (if (and (file-exists-p file
)
93 (not (zerop (nth 7 (file-attributes file
)))))
95 (insert (format "221 %d Article retrieved.\n" article
))
96 (nneething-insert-head file
)
99 (setq sequence
(cdr sequence
)
104 (message "nneething: Receiving headers... %d%%"
105 (/ (* count
100) number
))))
107 (and large
(message "nneething: Receiving headers...done"))
109 ;; Fold continuation lines.
110 (goto-char (point-min))
111 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t
)
112 (replace-match " " t t
))
115 (defun nneething-open-server (server &optional defs
)
116 (setq nneething-status-string
"")
117 (nnheader-init-server-buffer))
119 (defun nneething-close-server (&optional server
)
122 (defun nneething-server-opened (&optional server
)
125 (defun nneething-status-message (&optional server
)
126 nneething-status-string
)
128 (defun nneething-request-article (id &optional newsgroup server buffer
)
129 (nneething-possibly-change-directory newsgroup
)
130 (let ((file (if (stringp id
) nil
(nneething-file-name id
)))
131 (nntp-server-buffer (or buffer nntp-server-buffer
)))
132 (and (stringp file
) ; We did not request by Message-ID.
133 (file-exists-p file
) ; The file exists.
134 (not (file-directory-p file
)) ; It's not a dir.
136 (nnmail-find-file file
) ; Insert the file in the nntp buf.
137 (or (nnheader-article-p) ; Either it's a real article...
139 (goto-char (point-min))
140 (nneething-make-head file
) ; ... or we fake some headers.
144 (defun nneething-request-group (group &optional dir dont-check
)
145 (nneething-possibly-change-directory group dir
)
146 (or dont-check
(nneething-create-mapping))
148 (set-buffer nntp-server-buffer
)
150 (if (> (car nneething-active
) (cdr nneething-active
))
151 (insert (format "211 0 1 0 %s\n" group
))
152 (insert (format "211 %d %d %d %s\n"
153 (- (1+ (cdr nneething-active
)) (car nneething-active
))
154 (car nneething-active
) (cdr nneething-active
)
158 (defun nneething-request-list (&optional server dir
)
160 (set-buffer nntp-server-buffer
)
164 (defun nneething-request-newgroups (date &optional server
)
166 (set-buffer nntp-server-buffer
)
170 (defun nneething-request-post (&optional server
)
171 (mail-send-and-exit nil
))
173 (defalias 'nneething-request-post-buffer
'nnmail-request-post-buffer
)
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 (make-directory nneething-map-file-directory
'parents
)
206 ;; We store it in a special directory under the user's home dir.
207 (concat (file-name-as-directory nneething-map-file-directory
)
208 nneething-group nneething-map-file
))
210 (defun nneething-create-mapping ()
211 ;; Read nneething-active and nneething-map
212 (let ((map-file (nneething-map-file))
213 (files (directory-files nneething-directory
))
215 (if (file-exists-p map-file
)
217 (load map-file nil t t
)
219 (or nneething-active
(setq nneething-active
(cons 1 0)))
220 ;; Remove files matching that regexp.
224 (if (string-match nneething-exclude-files
(car f
))
225 (if prev
(setcdr prev
(cdr f
))
226 (setq files
(cdr files
)))
229 ;; Remove files that have disappeared from the map.
230 (let ((map nneething-map
)
233 (if (member (car (car map
)) files
)
237 (setcdr prev
(cdr map
))
238 (setq nneething-map
(cdr nneething-map
))))
239 (setq map
(cdr map
))))
240 ;; Find all new files and enter them into the map.
242 (or (assoc (car files
) nneething-map
) ; If already in the map, ignore.
245 (setcdr nneething-active
(1+ (cdr nneething-active
)))
247 (cons (cons (car files
) (cdr nneething-active
)) nneething-map
))))
248 (setq files
(cdr files
)))
249 (if (or (not touched
) nneething-read-only
)
252 (set-buffer (get-buffer-create " *nneething map*"))
253 (buffer-disable-undo (current-buffer))
255 (insert "(setq nneething-map '" (prin1-to-string nneething-map
) ")\n"
256 "(setq nneething-active '" (prin1-to-string nneething-active
)
258 (write-region (point-min) (point-max) map-file nil
'nomesg
)
259 (kill-buffer (current-buffer))))))
261 (defvar nneething-message-id-number
0)
262 (defvar nneething-work-buffer
" *nneething work*")
264 (defun nneething-insert-head (file)
265 (and (nneething-get-head file
)
266 (insert-buffer-substring nneething-work-buffer
)))
268 (defun nneething-make-head (file)
269 (let ((atts (file-attributes file
)))
270 (insert "Subject: " (file-name-nondirectory file
) "\n"
271 "Message-ID: <nneething-"
273 (setq nneething-message-id-number
274 (1+ nneething-message-id-number
)))
275 "@" (system-name) ">\n"
276 "Date: " (current-time-string (nth 5 atts
)) "\n"
277 (nneething-from-line (nth 2 atts
))
278 "Chars: " (int-to-string (nth 7 atts
)) "\n")))
280 (defun nneething-from-line (uid)
281 (let ((login (condition-case nil
282 (user-login-name uid
)
284 (cond ((= uid
(user-uid)) (user-login-name))
286 (t (int-to-string uid
))))))
287 (name (condition-case nil
290 (cond ((= uid
(user-uid)) (user-full-name))
291 ((zerop uid
) "Ms. Root"))))))
292 (concat "From: " login
"@" (system-name)
293 (if name
(concat " (" name
")") "") "\n")))
295 (defun nneething-get-head (file)
297 (set-buffer (get-buffer-create nneething-work-buffer
))
298 (setq case-fold-search nil
)
299 (buffer-disable-undo (current-buffer))
302 ((not (file-exists-p file
))
303 ;; The file do not exist.
305 ((or (file-directory-p file
)
306 (file-symlink-p file
))
307 ;; It's a dir, so we fudge a head.
308 (nneething-make-head file
) t
)
310 ;; We examine the file.
311 (nnheader-insert-head file
)
312 (if (nnheader-article-p)
315 (goto-char (point-min))
316 (or (and (search-forward "\n\n" nil t
)
321 (nneething-make-head file
))
324 (defun nneething-number-to-file (number)
325 (car (rassq number nneething-map
)))
327 (defun nneething-file-name (article)
328 (concat (file-name-as-directory nneething-directory
)
329 (if (numberp article
) (nneething-number-to-file article
)
334 ;;; nneething.el ends here