3caee7b7f78cb7d2b1d3d0fba07588e45250b844
[elisp/gnus.git-] / lisp / nneething.el
1 ;;; nneething.el --- arbitrary file access for Gnus
2 ;; Copyright (C) 1995,96,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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 (eval-when-compile (require 'cl))
30 (require 'nnheader)
31 (require 'nnmail)
32 (require 'nnoo)
33 (require 'gnus-util)
34
35 (nnoo-declare nneething)
36
37 (defvoo nneething-map-file-directory "~/.nneething/"
38   "Where nneething stores the map files.")
39
40 (defvoo nneething-map-file ".nneething"
41   "Name of the map files.")
42
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.")
46
47 (defvoo nneething-include-files nil
48   "Regexp saying what files to include in the group.
49 If this variable is non-nil, only files matching this regexp will be
50 included.")
51
52 \f
53
54 ;;; Internal variables.
55
56 (defconst nneething-version "nneething 1.0"
57   "nneething version.")
58
59 (defvoo nneething-current-directory nil
60   "Current news group directory.")
61
62 (defvoo nneething-status-string "")
63
64 (defvoo nneething-message-id-number 0)
65 (defvoo nneething-work-buffer " *nneething work*")
66
67 (defvoo nneething-group nil)
68 (defvoo nneething-map nil)
69 (defvoo nneething-read-only nil)
70 (defvoo nneething-active nil)
71 (defvoo nneething-address 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                (nnheader-message 5 "nneething: Receiving headers... %d%%"
109                         (/ (* count 100) number))))
110
111         (when large
112           (nnheader-message 5 "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
130               file (current-buffer))    ; ... or we fake some headers.
131              (insert "\n"))
132            t))))
133
134 (deffoo nneething-request-group (group &optional server dont-check)
135   (nneething-possibly-change-directory group server)
136   (unless dont-check
137     (nneething-create-mapping)
138     (if (> (car nneething-active) (cdr nneething-active))
139         (nnheader-insert "211 0 1 0 %s\n" group)
140       (nnheader-insert
141        "211 %d %d %d %s\n"
142        (- (1+ (cdr nneething-active)) (car nneething-active))
143        (car nneething-active) (cdr nneething-active)
144        group)))
145   t)
146
147 (deffoo nneething-request-list (&optional server dir)
148   (nnheader-report 'nneething "LIST is not implemented."))
149
150 (deffoo nneething-request-newgroups (date &optional server)
151   (nnheader-report 'nneething "NEWSGROUPS is not implemented."))
152
153 (deffoo nneething-request-type (group &optional article)
154   'unknown)
155
156 (deffoo nneething-close-group (group &optional server)
157   (setq nneething-current-directory nil)
158   t)
159
160 (deffoo nneething-open-server (server &optional defs)
161   (nnheader-init-server-buffer)
162   (if (nneething-server-opened server)
163       t
164     (unless (assq 'nneething-address defs)
165       (setq defs (append defs (list (list 'nneething-address server)))))
166     (nnoo-change-server 'nneething server defs)))
167
168 \f
169 ;;; Internal functions.
170
171 (defun nneething-possibly-change-directory (group &optional server)
172   (when (and server
173              (not (nneething-server-opened server)))
174     (nneething-open-server server))
175   (when (and group
176              (not (equal nneething-group group)))
177     (setq nneething-group group)
178     (setq nneething-map nil)
179     (setq nneething-active (cons 1 0))
180     (nneething-create-mapping)))
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   (when (file-exists-p nneething-address)
192     (let ((map-file (nneething-map-file))
193           (files (directory-files nneething-address))
194           touched map-files)
195       (when (file-exists-p map-file)
196         (ignore-errors
197           (load map-file nil t t)))
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 files not matching the inclusion regexp.
220       (when nneething-include-files
221         (let ((f files)
222               prev)
223           (while f
224             (if (not (string-match nneething-include-files (car f)))
225                 (if prev (setcdr prev (cdr f))
226                   (setq files (cdr files)))
227               (setq prev f))
228             (setq f (cdr f)))))
229       ;; Remove deleted files from the map.
230       (let ((map nneething-map)
231             prev)
232         (while map
233           (if (and (member (cadar map) files)
234                    ;; We also remove files that have changed mod times.
235                    (equal (nth 5 (file-attributes
236                                   (nneething-file-name (cadar map))))
237                           (caddar map)))
238               (progn
239                 (push (cadar map) map-files)
240                 (setq prev map))
241             (setq touched t)
242             (if prev
243                 (setcdr prev (cdr map))
244               (setq nneething-map (cdr nneething-map))))
245           (setq map (cdr map))))
246       ;; Find all new files and enter them into the map.
247       (while files
248         (unless (member (car files) map-files)
249           ;; This file is not in the map, so we enter it.
250           (setq touched t)
251           (setcdr nneething-active (1+ (cdr nneething-active)))
252           (push (list (cdr nneething-active) (car files)
253                       (nth 5 (file-attributes
254                               (nneething-file-name (car files)))))
255                 nneething-map))
256         (setq files (cdr files)))
257       (when (and touched
258                  (not nneething-read-only))
259         (with-temp-file map-file
260           (insert "(setq nneething-map '")
261           (gnus-prin1 nneething-map)
262           (insert ")\n(setq nneething-active '")
263           (gnus-prin1 nneething-active)
264           (insert ")\n"))))))
265
266 (defun nneething-insert-head (file)
267   "Insert the head of FILE."
268   (when (nneething-get-head file)
269     (insert-buffer-substring nneething-work-buffer)
270     (goto-char (point-max))))
271
272 (defun nneething-make-head (file &optional buffer)
273   "Create a head by looking at the file attributes of FILE."
274   (let ((atts (file-attributes file)))
275     (insert
276      "Subject: " (file-name-nondirectory file) "\n"
277      "Message-ID: <nneething-"
278      (int-to-string (incf nneething-message-id-number))
279      "@" (system-name) ">\n"
280      (if (equal '(0 0) (nth 5 atts)) ""
281        (concat "Date: " (current-time-string (nth 5 atts)) "\n"))
282      (or (when buffer
283            (save-excursion
284              (set-buffer buffer)
285              (when (re-search-forward "<[a-zA-Z0-9_]@[-a-zA-Z0-9_]>" 1000 t)
286                (concat "From: " (match-string 0) "\n"))))
287          (nneething-from-line (nth 2 atts) file))
288      (if (> (string-to-int (int-to-string (nth 7 atts))) 0)
289          (concat "Chars: " (int-to-string (nth 7 atts)) "\n")
290        "")
291      (if buffer
292          (save-excursion
293            (set-buffer buffer)
294            (concat "Lines: " (int-to-string
295                               (count-lines (point-min) (point-max)))
296                    "\n"))
297        "")
298      )))
299
300 (defun nneething-from-line (uid &optional file)
301   "Return a From header based of UID."
302   (let* ((login (condition-case nil
303                     (user-login-name uid)
304                   (error
305                    (cond ((= uid (user-uid)) (user-login-name))
306                          ((zerop uid) "root")
307                          (t (int-to-string uid))))))
308          (name (condition-case nil
309                    (user-full-name uid)
310                  (error
311                   (cond ((= uid (user-uid)) (user-full-name))
312                         ((zerop uid) "Ms. Root")))))
313          (host (if  (string-match "\\`/[^/@]*@\\([^:/]+\\):" file)
314                    (prog1
315                        (substring file
316                                   (match-beginning 1)
317                                   (match-end 1))
318                      (when (string-match
319                             "/\\(users\\|home\\)/\\([^/]+\\)/" file)
320                        (setq login (substring file
321                                               (match-beginning 2)
322                                               (match-end 2))
323                              name nil)))
324                  (system-name))))
325     (concat "From: " login "@" host
326             (if name (concat " (" name ")") "") "\n")))
327
328 (defun nneething-get-head (file)
329   "Either find the head in FILE or make a head for FILE."
330   (save-excursion
331     (set-buffer (get-buffer-create nneething-work-buffer))
332     (setq case-fold-search nil)
333     (buffer-disable-undo)
334     (erase-buffer)
335     (cond
336      ((not (file-exists-p file))
337       ;; The file do not exist.
338       nil)
339      ((or (file-directory-p file)
340           (file-symlink-p file))
341       ;; It's a dir, so we fudge a head.
342       (nneething-make-head file) t)
343      (t
344       ;; We examine the file.
345       (nnheader-insert-head file)
346       (if (nnheader-article-p)
347           (delete-region
348            (progn
349              (goto-char (point-min))
350              (or (and (search-forward "\n\n" nil t)
351                       (1- (point)))
352                  (point-max)))
353            (point-max))
354         (goto-char (point-min))
355         (nneething-make-head file (current-buffer))
356         (delete-region (point) (point-max)))
357       t))))
358
359 (defun nneething-file-name (article)
360   "Return the file name of ARTICLE."
361   (let ((dir (file-name-as-directory nneething-address))
362         fname)
363     (if (numberp article)
364         (if (setq fname (cadr (assq article nneething-map)))
365             (concat dir fname)
366           (make-temp-name (concat dir "nneething")))
367       (concat dir article))))
368
369 (provide 'nneething)
370
371 ;;; nneething.el ends here