Add 2011 to FSF/AIST copyright years.
[bpt/emacs.git] / lisp / gnus / nneething.el
CommitLineData
6748645f 1;;; nneething.el --- arbitrary file access for Gnus
16409b0b 2
e84b4b86 3;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
5df4f04c 4;; 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
eec82323 5
6748645f 6;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
23f87bed 7;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
eec82323
LMI
8;; Keywords: news, mail
9
10;; This file is part of GNU Emacs.
11
5e809f55 12;; GNU Emacs is free software: you can redistribute it and/or modify
eec82323 13;; it under the terms of the GNU General Public License as published by
5e809f55
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
eec82323
LMI
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
5e809f55 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
eec82323
LMI
24
25;;; Commentary:
26
27;;; Code:
28
0df953b6
RS
29(eval-when-compile (require 'cl))
30
eec82323
LMI
31(require 'nnheader)
32(require 'nnmail)
33(require 'nnoo)
34(require 'gnus-util)
eec82323
LMI
35
36(nnoo-declare nneething)
37
fa9a04e1
MB
38(defvoo nneething-map-file-directory
39 (nnheader-concat gnus-directory ".nneething/")
eec82323
LMI
40 "Where nneething stores the map files.")
41
42(defvoo nneething-map-file ".nneething"
43 "Name of the map files.")
44
45(defvoo nneething-exclude-files nil
46 "Regexp saying what files to exclude from the group.
47If this variable is nil, no files will be excluded.")
48
16409b0b
GM
49(defvoo nneething-include-files nil
50 "Regexp saying what files to include in the group.
51If this variable is non-nil, only files matching this regexp will be
52included.")
53
eec82323
LMI
54\f
55
56;;; Internal variables.
57
58(defconst nneething-version "nneething 1.0"
59 "nneething version.")
60
61(defvoo nneething-current-directory nil
62 "Current news group directory.")
63
64(defvoo nneething-status-string "")
65
eec82323
LMI
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)
6748645f 72(defvoo nneething-address nil)
eec82323
LMI
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))
6748645f 109 (nnheader-message 5 "nneething: Receiving headers... %d%%"
16409b0b 110 (/ (* count 100) number))))
eec82323
LMI
111
112 (when large
6748645f 113 (nnheader-message 5 "nneething: Receiving headers...done"))
eec82323
LMI
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)))
23f87bed 123 (and (stringp file) ; We did not request by Message-ID.
eec82323
LMI
124 (file-exists-p file) ; The file exists.
125 (not (file-directory-p file)) ; It's not a dir.
126 (save-excursion
23f87bed
MB
127 (let ((nnmail-file-coding-system 'binary))
128 (nnmail-find-file file)) ; Insert the file in the nntp buf.
eec82323 129 (unless (nnheader-article-p) ; Either it's a real article...
23f87bed
MB
130 (let ((type
131 (unless (file-directory-p file)
132 (or (cdr (assoc (concat "." (file-name-extension file))
133 mailcap-mime-extensions))
134 "text/plain")))
135 (charset
136 (mm-detect-mime-charset-region (point-min) (point-max)))
137 (encoding))
138 (unless (string-match "\\`text/" type)
139 (base64-encode-region (point-min) (point-max))
140 (setq encoding "base64"))
141 (goto-char (point-min))
142 (nneething-make-head file (current-buffer)
143 nil type charset encoding))
eec82323
LMI
144 (insert "\n"))
145 t))))
146
147(deffoo nneething-request-group (group &optional server dont-check)
148 (nneething-possibly-change-directory group server)
149 (unless dont-check
150 (nneething-create-mapping)
151 (if (> (car nneething-active) (cdr nneething-active))
152 (nnheader-insert "211 0 1 0 %s\n" group)
153 (nnheader-insert
154 "211 %d %d %d %s\n"
155 (- (1+ (cdr nneething-active)) (car nneething-active))
156 (car nneething-active) (cdr nneething-active)
157 group)))
158 t)
159
160(deffoo nneething-request-list (&optional server dir)
161 (nnheader-report 'nneething "LIST is not implemented."))
162
163(deffoo nneething-request-newgroups (date &optional server)
164 (nnheader-report 'nneething "NEWSGROUPS is not implemented."))
165
166(deffoo nneething-request-type (group &optional article)
167 'unknown)
168
169(deffoo nneething-close-group (group &optional server)
170 (setq nneething-current-directory nil)
171 t)
172
173(deffoo nneething-open-server (server &optional defs)
174 (nnheader-init-server-buffer)
175 (if (nneething-server-opened server)
176 t
6748645f
LMI
177 (unless (assq 'nneething-address defs)
178 (setq defs (append defs (list (list 'nneething-address server)))))
eec82323
LMI
179 (nnoo-change-server 'nneething server defs)))
180
181\f
182;;; Internal functions.
183
184(defun nneething-possibly-change-directory (group &optional server)
185 (when (and server
186 (not (nneething-server-opened server)))
187 (nneething-open-server server))
188 (when (and group
189 (not (equal nneething-group group)))
190 (setq nneething-group group)
191 (setq nneething-map nil)
192 (setq nneething-active (cons 1 0))
193 (nneething-create-mapping)))
194
195(defun nneething-map-file ()
196 ;; We make sure that the .nneething directory exists.
197 (gnus-make-directory nneething-map-file-directory)
198 ;; We store it in a special directory under the user's home dir.
199 (concat (file-name-as-directory nneething-map-file-directory)
200 nneething-group nneething-map-file))
201
202(defun nneething-create-mapping ()
203 ;; Read nneething-active and nneething-map.
6748645f 204 (when (file-exists-p nneething-address)
eec82323 205 (let ((map-file (nneething-map-file))
6748645f 206 (files (directory-files nneething-address))
eec82323
LMI
207 touched map-files)
208 (when (file-exists-p map-file)
209 (ignore-errors
210 (load map-file nil t t)))
211 (unless nneething-active
212 (setq nneething-active (cons 1 0)))
213 ;; Old nneething had a different map format.
214 (when (and (cdar nneething-map)
215 (atom (cdar nneething-map)))
216 (setq nneething-map
217 (mapcar (lambda (n)
218 (list (cdr n) (car n)
219 (nth 5 (file-attributes
220 (nneething-file-name (car n))))))
221 nneething-map)))
222 ;; Remove files matching the exclusion regexp.
223 (when nneething-exclude-files
224 (let ((f files)
225 prev)
226 (while f
227 (if (string-match nneething-exclude-files (car f))
228 (if prev (setcdr prev (cdr f))
229 (setq files (cdr files)))
230 (setq prev f))
231 (setq f (cdr f)))))
16409b0b
GM
232 ;; Remove files not matching the inclusion regexp.
233 (when nneething-include-files
234 (let ((f files)
235 prev)
236 (while f
237 (if (not (string-match nneething-include-files (car f)))
238 (if prev (setcdr prev (cdr f))
239 (setq files (cdr files)))
240 (setq prev f))
241 (setq f (cdr f)))))
eec82323
LMI
242 ;; Remove deleted files from the map.
243 (let ((map nneething-map)
244 prev)
245 (while map
16409b0b 246 (if (and (member (cadr (car map)) files)
23f87bed 247 ;; We also remove files that have changed mod times.
eec82323 248 (equal (nth 5 (file-attributes
16409b0b
GM
249 (nneething-file-name (cadr (car map)))))
250 (cadr (cdar map))))
eec82323 251 (progn
16409b0b 252 (push (cadr (car map)) map-files)
eec82323
LMI
253 (setq prev map))
254 (setq touched t)
255 (if prev
256 (setcdr prev (cdr map))
257 (setq nneething-map (cdr nneething-map))))
258 (setq map (cdr map))))
259 ;; Find all new files and enter them into the map.
260 (while files
261 (unless (member (car files) map-files)
262 ;; This file is not in the map, so we enter it.
263 (setq touched t)
264 (setcdr nneething-active (1+ (cdr nneething-active)))
265 (push (list (cdr nneething-active) (car files)
266 (nth 5 (file-attributes
267 (nneething-file-name (car files)))))
268 nneething-map))
269 (setq files (cdr files)))
270 (when (and touched
271 (not nneething-read-only))
16409b0b 272 (with-temp-file map-file
eec82323
LMI
273 (insert "(setq nneething-map '")
274 (gnus-prin1 nneething-map)
275 (insert ")\n(setq nneething-active '")
276 (gnus-prin1 nneething-active)
277 (insert ")\n"))))))
278
279(defun nneething-insert-head (file)
280 "Insert the head of FILE."
281 (when (nneething-get-head file)
282 (insert-buffer-substring nneething-work-buffer)
283 (goto-char (point-max))))
284
23f87bed
MB
285(defun nneething-encode-file-name (file &optional coding-system)
286 "Encode the name of the FILE in CODING-SYSTEM."
287 (let ((pos 0) buf)
288 (setq file (mm-encode-coding-string
289 file (or coding-system nnmail-pathname-coding-system)))
290 (while (string-match "[^-0-9a-zA-Z_:/.]" file pos)
291 (setq buf (cons (format "%%%02x" (aref file (match-beginning 0)))
292 (cons (substring file pos (match-beginning 0)) buf))
293 pos (match-end 0)))
294 (apply (function concat)
295 (nreverse (cons (substring file pos) buf)))))
296
297(defun nneething-decode-file-name (file &optional coding-system)
298 "Decode the name of the FILE is encoded in CODING-SYSTEM."
299 (let ((pos 0) buf)
300 (while (string-match "%\\([0-9a-fA-F][0-9a-fA-F]\\)" file pos)
301 (setq buf (cons (string (string-to-number (match-string 1 file) 16))
302 (cons (substring file pos (match-beginning 0)) buf))
303 pos (match-end 0)))
5538c331 304 (mm-decode-coding-string
23f87bed
MB
305 (apply (function concat)
306 (nreverse (cons (substring file pos) buf)))
307 (or coding-system nnmail-pathname-coding-system))))
308
309(defun nneething-get-file-name (id)
310 "Extract the file name from the message ID string."
311 (when (string-match "\\`<nneething-\\([^@]+\\)@.*>\\'" id)
312 (nneething-decode-file-name (match-string 1 id))))
313
314(defun nneething-make-head (file &optional buffer extra-msg
315 mime-type mime-charset mime-encoding)
eec82323
LMI
316 "Create a head by looking at the file attributes of FILE."
317 (let ((atts (file-attributes file)))
318 (insert
23f87bed
MB
319 "Subject: " (file-name-nondirectory file) (or extra-msg "") "\n"
320 "Message-ID: <nneething-" (nneething-encode-file-name file)
eec82323
LMI
321 "@" (system-name) ">\n"
322 (if (equal '(0 0) (nth 5 atts)) ""
323 (concat "Date: " (current-time-string (nth 5 atts)) "\n"))
324 (or (when buffer
325 (save-excursion
326 (set-buffer buffer)
327 (when (re-search-forward "<[a-zA-Z0-9_]@[-a-zA-Z0-9_]>" 1000 t)
328 (concat "From: " (match-string 0) "\n"))))
329 (nneething-from-line (nth 2 atts) file))
e9bd5782 330 (if (> (string-to-number (int-to-string (nth 7 atts))) 0)
eec82323
LMI
331 (concat "Chars: " (int-to-string (nth 7 atts)) "\n")
332 "")
333 (if buffer
334 (save-excursion
335 (set-buffer buffer)
336 (concat "Lines: " (int-to-string
337 (count-lines (point-min) (point-max)))
338 "\n"))
23f87bed
MB
339 "")
340 (if mime-type
341 (concat "Content-Type: " mime-type
342 (if mime-charset
343 (concat "; charset="
344 (if (stringp mime-charset)
345 mime-charset
346 (symbol-name mime-charset)))
347 "")
348 (if mime-encoding
349 (concat "\nContent-Transfer-Encoding: " mime-encoding)
350 "")
351 "\nMIME-Version: 1.0\n")
16409b0b 352 ""))))
eec82323
LMI
353
354(defun nneething-from-line (uid &optional file)
355 "Return a From header based of UID."
356 (let* ((login (condition-case nil
357 (user-login-name uid)
358 (error
359 (cond ((= uid (user-uid)) (user-login-name))
360 ((zerop uid) "root")
361 (t (int-to-string uid))))))
362 (name (condition-case nil
363 (user-full-name uid)
364 (error
365 (cond ((= uid (user-uid)) (user-full-name))
366 ((zerop uid) "Ms. Root")))))
367 (host (if (string-match "\\`/[^/@]*@\\([^:/]+\\):" file)
368 (prog1
369 (substring file
370 (match-beginning 1)
371 (match-end 1))
16409b0b
GM
372 (when (string-match
373 "/\\(users\\|home\\)/\\([^/]+\\)/" file)
eec82323
LMI
374 (setq login (substring file
375 (match-beginning 2)
376 (match-end 2))
377 name nil)))
378 (system-name))))
379 (concat "From: " login "@" host
380 (if name (concat " (" name ")") "") "\n")))
381
382(defun nneething-get-head (file)
383 "Either find the head in FILE or make a head for FILE."
384 (save-excursion
385 (set-buffer (get-buffer-create nneething-work-buffer))
386 (setq case-fold-search nil)
16409b0b 387 (buffer-disable-undo)
eec82323
LMI
388 (erase-buffer)
389 (cond
390 ((not (file-exists-p file))
391 ;; The file do not exist.
392 nil)
393 ((or (file-directory-p file)
394 (file-symlink-p file))
395 ;; It's a dir, so we fudge a head.
396 (nneething-make-head file) t)
397 (t
398 ;; We examine the file.
23f87bed
MB
399 (condition-case ()
400 (progn
401 (nnheader-insert-head file)
402 (if (nnheader-article-p)
403 (delete-region
404 (progn
405 (goto-char (point-min))
406 (or (and (search-forward "\n\n" nil t)
407 (1- (point)))
408 (point-max)))
409 (point-max))
410 (goto-char (point-min))
411 (nneething-make-head file (current-buffer))
412 (delete-region (point) (point-max))))
413 (file-error
414 (nneething-make-head file (current-buffer) " (unreadable)")))
eec82323
LMI
415 t))))
416
417(defun nneething-file-name (article)
418 "Return the file name of ARTICLE."
16409b0b 419 (let ((dir (file-name-as-directory nneething-address))
23f87bed 420 fname)
16409b0b
GM
421 (if (numberp article)
422 (if (setq fname (cadr (assq article nneething-map)))
423 (expand-file-name fname dir)
01c52d31 424 (make-temp-name (expand-file-name "nneething" dir)))
16409b0b 425 (expand-file-name article dir))))
eec82323
LMI
426
427(provide 'nneething)
428
cbee283d 429;; arch-tag: 1277f386-88f2-4459-bb24-f3f45962a6c5
eec82323 430;;; nneething.el ends here