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