(nneething-create-mapping): Don't use cadar & al.
[gnus] / 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
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
131               file (current-buffer))    ; ... or we fake some headers.
132              (insert "\n"))
133            t))))
134
135 (deffoo nneething-request-group (group &optional server dont-check)
136   (nneething-possibly-change-directory group server)
137   (unless dont-check
138     (nneething-create-mapping)
139     (if (> (car nneething-active) (cdr nneething-active))
140         (nnheader-insert "211 0 1 0 %s\n" group)
141       (nnheader-insert
142        "211 %d %d %d %s\n"
143        (- (1+ (cdr nneething-active)) (car nneething-active))
144        (car nneething-active) (cdr nneething-active)
145        group)))
146   t)
147
148 (deffoo nneething-request-list (&optional server dir)
149   (nnheader-report 'nneething "LIST is not implemented."))
150
151 (deffoo nneething-request-newgroups (date &optional server)
152   (nnheader-report 'nneething "NEWSGROUPS is not implemented."))
153
154 (deffoo nneething-request-type (group &optional article)
155   'unknown)
156
157 (deffoo nneething-close-group (group &optional server)
158   (setq nneething-current-directory nil)
159   t)
160
161 (deffoo nneething-open-server (server &optional defs)
162   (nnheader-init-server-buffer)
163   (if (nneething-server-opened server)
164       t
165     (unless (assq 'nneething-address defs)
166       (setq defs (append defs (list (list 'nneething-address server)))))
167     (nnoo-change-server 'nneething server defs)))
168
169 \f
170 ;;; Internal functions.
171
172 (defun nneething-possibly-change-directory (group &optional server)
173   (when (and server
174              (not (nneething-server-opened server)))
175     (nneething-open-server server))
176   (when (and group
177              (not (equal nneething-group group)))
178     (setq nneething-group group)
179     (setq nneething-map nil)
180     (setq nneething-active (cons 1 0))
181     (nneething-create-mapping)))
182
183 (defun nneething-map-file ()
184   ;; We make sure that the .nneething directory exists.
185   (gnus-make-directory nneething-map-file-directory)
186   ;; We store it in a special directory under the user's home dir.
187   (concat (file-name-as-directory nneething-map-file-directory)
188           nneething-group nneething-map-file))
189
190 (defun nneething-create-mapping ()
191   ;; Read nneething-active and nneething-map.
192   (when (file-exists-p nneething-address)
193     (let ((map-file (nneething-map-file))
194           (files (directory-files nneething-address))
195           touched map-files)
196       (when (file-exists-p map-file)
197         (ignore-errors
198           (load map-file nil t t)))
199       (unless nneething-active
200         (setq nneething-active (cons 1 0)))
201       ;; Old nneething had a different map format.
202       (when (and (cdar nneething-map)
203                  (atom (cdar nneething-map)))
204         (setq nneething-map
205               (mapcar (lambda (n)
206                         (list (cdr n) (car n)
207                               (nth 5 (file-attributes
208                                       (nneething-file-name (car n))))))
209                       nneething-map)))
210       ;; Remove files matching the exclusion regexp.
211       (when nneething-exclude-files
212         (let ((f files)
213               prev)
214           (while f
215             (if (string-match nneething-exclude-files (car f))
216                 (if prev (setcdr prev (cdr f))
217                   (setq files (cdr files)))
218               (setq prev f))
219             (setq f (cdr f)))))
220       ;; Remove files not matching the inclusion regexp.
221       (when nneething-include-files
222         (let ((f files)
223               prev)
224           (while f
225             (if (not (string-match nneething-include-files (car f)))
226                 (if prev (setcdr prev (cdr f))
227                   (setq files (cdr files)))
228               (setq prev f))
229             (setq f (cdr f)))))
230       ;; Remove deleted files from the map.
231       (let ((map nneething-map)
232             prev)
233         (while map
234           (if (and (member (cadr (car map)) files)
235                    ;; We also remove files that have changed mod times.
236                    (equal (nth 5 (file-attributes
237                                   (nneething-file-name (cadr (car map)))))
238                           (cadr (cdar map))))
239               (progn
240                 (push (cadr (car map)) map-files)
241                 (setq prev map))
242             (setq touched t)
243             (if prev
244                 (setcdr prev (cdr map))
245               (setq nneething-map (cdr nneething-map))))
246           (setq map (cdr map))))
247       ;; Find all new files and enter them into the map.
248       (while files
249         (unless (member (car files) map-files)
250           ;; This file is not in the map, so we enter it.
251           (setq touched t)
252           (setcdr nneething-active (1+ (cdr nneething-active)))
253           (push (list (cdr nneething-active) (car files)
254                       (nth 5 (file-attributes
255                               (nneething-file-name (car files)))))
256                 nneething-map))
257         (setq files (cdr files)))
258       (when (and touched
259                  (not nneething-read-only))
260         (with-temp-file map-file
261           (insert "(setq nneething-map '")
262           (gnus-prin1 nneething-map)
263           (insert ")\n(setq nneething-active '")
264           (gnus-prin1 nneething-active)
265           (insert ")\n"))))))
266
267 (defun nneething-insert-head (file)
268   "Insert the head of FILE."
269   (when (nneething-get-head file)
270     (insert-buffer-substring nneething-work-buffer)
271     (goto-char (point-max))))
272
273 (defun nneething-make-head (file &optional buffer)
274   "Create a head by looking at the file attributes of FILE."
275   (let ((atts (file-attributes file)))
276     (insert
277      "Subject: " (file-name-nondirectory file) "\n"
278      "Message-ID: <nneething-"
279      (int-to-string (incf nneething-message-id-number))
280      "@" (system-name) ">\n"
281      (if (equal '(0 0) (nth 5 atts)) ""
282        (concat "Date: " (current-time-string (nth 5 atts)) "\n"))
283      (or (when buffer
284            (save-excursion
285              (set-buffer buffer)
286              (when (re-search-forward "<[a-zA-Z0-9_]@[-a-zA-Z0-9_]>" 1000 t)
287                (concat "From: " (match-string 0) "\n"))))
288          (nneething-from-line (nth 2 atts) file))
289      (if (> (string-to-int (int-to-string (nth 7 atts))) 0)
290          (concat "Chars: " (int-to-string (nth 7 atts)) "\n")
291        "")
292      (if buffer
293          (save-excursion
294            (set-buffer buffer)
295            (concat "Lines: " (int-to-string
296                               (count-lines (point-min) (point-max)))
297                    "\n"))
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             (expand-file-name fname dir)
366           (make-temp-name (expand-file-name "nneething" dir)))
367       (expand-file-name article dir))))
368
369 (provide 'nneething)
370
371 ;;; nneething.el ends here