*** empty log message ***
[gnus] / lisp / nneething.el
1 ;;; nneething.el --- arbitrary file access for Gnus
2 ;; Copyright (C) 1995,96,97,98 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
31 (require 'nnheader)
32 (require 'nnmail)
33 (require 'nnoo)
34 (require 'gnus-util)
35
36 (nnoo-declare nneething)
37
38 (defvoo nneething-map-file-directory "~/.nneething/"
39   "Where nneething stores the map files.")
40
41 (defvoo nneething-map-file ".nneething"
42   "Name of the map files.")
43
44 (defvoo nneething-exclude-files nil
45   "Regexp saying what files to exclude from the group.
46 If this variable is nil, no files will be excluded.")
47
48 (defvoo nneething-include-files nil
49   "Regexp saying what files to include in the group.
50 If this variable is non-nil, only files matching this regexp will be
51 included.")
52
53 \f
54
55 ;;; Internal variables.
56
57 (defconst nneething-version "nneething 1.0"
58   "nneething version.")
59
60 (defvoo nneething-current-directory nil
61   "Current news group directory.")
62
63 (defvoo nneething-status-string "")
64
65 (defvoo nneething-message-id-number 0)
66 (defvoo nneething-work-buffer " *nneething work*")
67
68 (defvoo nneething-group nil)
69 (defvoo nneething-map nil)
70 (defvoo nneething-read-only nil)
71 (defvoo nneething-active nil)
72 (defvoo nneething-address nil)
73
74 \f
75
76 ;;; Interface functions.
77
78 (nnoo-define-basics nneething)
79
80 (deffoo 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 (nnheader-file-size 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                (nnheader-message 5 "nneething: Receiving headers... %d%%"
110                         (/ (* count 100) number))))
111
112         (when large
113           (nnheader-message 5 "nneething: Receiving headers...done"))
114
115         (nnheader-fold-continuation-lines)
116         'headers))))
117
118 (deffoo nneething-request-article (id &optional group server buffer)
119   (nneething-possibly-change-directory group)
120   (let ((file (unless (stringp id)
121                 (nneething-file-name id)))
122         (nntp-server-buffer (or buffer nntp-server-buffer)))
123     (and (stringp file)                 ; We did not request by Message-ID.
124          (file-exists-p file)           ; The file exists.
125          (not (file-directory-p file))  ; It's not a dir.
126          (save-excursion
127            (nnmail-find-file file)      ; Insert the file in the nntp buf.
128            (unless (nnheader-article-p) ; Either it's a real article...
129              (goto-char (point-min))
130              (nneething-make-head 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 "/\\(users\\|home\\)/\\([^/]+\\)/" file)
319                        (setq login (substring file
320                                               (match-beginning 2)
321                                               (match-end 2))
322                              name nil)))
323                  (system-name))))
324     (concat "From: " login "@" host
325             (if name (concat " (" name ")") "") "\n")))
326
327 (defun nneething-get-head (file)
328   "Either find the head in FILE or make a head for FILE."
329   (save-excursion
330     (set-buffer (get-buffer-create nneething-work-buffer))
331     (setq case-fold-search nil)
332     (buffer-disable-undo)
333     (erase-buffer)
334     (cond
335      ((not (file-exists-p file))
336       ;; The file do not exist.
337       nil)
338      ((or (file-directory-p file)
339           (file-symlink-p file))
340       ;; It's a dir, so we fudge a head.
341       (nneething-make-head file) t)
342      (t
343       ;; We examine the file.
344       (nnheader-insert-head file)
345       (if (nnheader-article-p)
346           (delete-region
347            (progn
348              (goto-char (point-min))
349              (or (and (search-forward "\n\n" nil t)
350                       (1- (point)))
351                  (point-max)))
352            (point-max))
353         (goto-char (point-min))
354         (nneething-make-head file (current-buffer))
355         (delete-region (point) (point-max)))
356       t))))
357
358 (defun nneething-file-name (article)
359   "Return the file name of ARTICLE."
360   (let ((dir (file-name-as-directory nneething-address))
361         fname)
362     (if (numberp article)
363         (if (setq fname (cadr (assq article nneething-map)))
364             (concat dir fname)
365           (make-temp-name (concat dir "nneething")))
366       (concat dir article))))
367
368 (provide 'nneething)
369
370 ;;; nneething.el ends here