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