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