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