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