13401
|
1 ;;; nnml.el --- mail spool access for Gnus
|
|
2 ;; Copyright (C) 1995 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
|
|
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
23
|
|
24 ;;; Commentary:
|
|
25
|
|
26 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
|
|
27 ;; For an overview of what the interface functions do, please see the
|
|
28 ;; Gnus sources.
|
|
29
|
|
30 ;;; Code:
|
|
31
|
|
32 (require 'nnheader)
|
|
33 (require 'nnmail)
|
|
34
|
|
35 (defvar nnml-directory "~/Mail/"
|
|
36 "Mail spool directory.")
|
|
37
|
|
38 (defvar nnml-active-file (concat nnml-directory "active")
|
|
39 "Mail active file.")
|
|
40
|
|
41 (defvar nnml-newsgroups-file (concat nnml-directory "newsgroups")
|
|
42 "Mail newsgroups description file.")
|
|
43
|
|
44 (defvar nnml-get-new-mail t
|
|
45 "If non-nil, nnml will check the incoming mail file and split the mail.")
|
|
46
|
|
47 (defvar nnml-nov-is-evil nil
|
|
48 "If non-nil, Gnus will never generate and use nov databases for mail groups.
|
|
49 Using nov databases will speed up header fetching considerably.
|
|
50 This variable shouldn't be flipped much. If you have, for some reason,
|
|
51 set this to t, and want to set it to nil again, you should always run
|
|
52 the `nnml-generate-nov-databases' command. The function will go
|
|
53 through all nnml directories and generate nov databases for them
|
|
54 all. This may very well take some time.")
|
|
55
|
|
56 (defvar nnml-prepare-save-mail-hook nil
|
|
57 "Hook run narrowed to an article before saving.")
|
|
58
|
|
59
|
|
60
|
|
61 (defconst nnml-version "nnml 1.0"
|
|
62 "nnml version.")
|
|
63
|
|
64 (defvar nnml-nov-file-name ".overview")
|
|
65
|
|
66 (defvar nnml-current-directory nil)
|
|
67 (defvar nnml-status-string "")
|
|
68 (defvar nnml-nov-buffer-alist nil)
|
|
69 (defvar nnml-group-alist nil)
|
|
70 (defvar nnml-active-timestamp nil)
|
|
71
|
|
72
|
|
73
|
|
74 ;; Server variables.
|
|
75
|
|
76 (defvar nnml-current-server nil)
|
|
77 (defvar nnml-server-alist nil)
|
|
78 (defvar nnml-server-variables
|
|
79 (list
|
|
80 (list 'nnml-directory nnml-directory)
|
|
81 (list 'nnml-active-file nnml-active-file)
|
|
82 (list 'nnml-newsgroups-file nnml-newsgroups-file)
|
|
83 (list 'nnml-get-new-mail nnml-get-new-mail)
|
|
84 (list 'nnml-nov-is-evil nnml-nov-is-evil)
|
|
85 (list 'nnml-nov-file-name nnml-nov-file-name)
|
|
86 '(nnml-current-directory nil)
|
|
87 '(nnml-status-string "")
|
|
88 '(nnml-nov-buffer-alist nil)
|
|
89 '(nnml-group-alist nil)
|
|
90 '(nnml-active-timestamp nil)))
|
|
91
|
|
92
|
|
93
|
|
94 ;;; Interface functions.
|
|
95
|
|
96 (defun nnml-retrieve-headers (sequence &optional newsgroup server)
|
|
97 (save-excursion
|
|
98 (set-buffer nntp-server-buffer)
|
|
99 (erase-buffer)
|
|
100 (let ((file nil)
|
|
101 (number (length sequence))
|
|
102 (count 0)
|
|
103 beg article)
|
|
104 (if (stringp (car sequence))
|
|
105 'headers
|
|
106 (nnml-possibly-change-directory newsgroup)
|
|
107 (if (nnml-retrieve-headers-with-nov sequence)
|
|
108 'nov
|
|
109 (while sequence
|
|
110 (setq article (car sequence))
|
|
111 (setq file
|
|
112 (concat nnml-current-directory (int-to-string article)))
|
|
113 (if (and (file-exists-p file)
|
|
114 (not (file-directory-p file)))
|
|
115 (progn
|
|
116 (insert (format "221 %d Article retrieved.\n" article))
|
|
117 (setq beg (point))
|
|
118 (nnheader-insert-head file)
|
|
119 (goto-char beg)
|
|
120 (if (search-forward "\n\n" nil t)
|
|
121 (forward-char -1)
|
|
122 (goto-char (point-max))
|
|
123 (insert "\n\n"))
|
|
124 (insert ".\n")
|
|
125 (delete-region (point) (point-max))))
|
|
126 (setq sequence (cdr sequence))
|
|
127 (setq count (1+ count))
|
|
128 (and (numberp nnmail-large-newsgroup)
|
|
129 (> number nnmail-large-newsgroup)
|
|
130 (zerop (% count 20))
|
|
131 gnus-verbose-backends
|
|
132 (message "nnml: Receiving headers... %d%%"
|
|
133 (/ (* count 100) number))))
|
|
134
|
|
135 (and (numberp nnmail-large-newsgroup)
|
|
136 (> number nnmail-large-newsgroup)
|
|
137 gnus-verbose-backends
|
|
138 (message "nnml: Receiving headers...done"))
|
|
139
|
|
140 ;; Fold continuation lines.
|
|
141 (goto-char (point-min))
|
|
142 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
|
|
143 (replace-match " " t t))
|
|
144 'headers)))))
|
|
145
|
|
146 (defun nnml-open-server (server &optional defs)
|
|
147 (nnheader-init-server-buffer)
|
|
148 (if (equal server nnml-current-server)
|
|
149 t
|
|
150 (if nnml-current-server
|
|
151 (setq nnml-server-alist
|
|
152 (cons (list nnml-current-server
|
|
153 (nnheader-save-variables nnml-server-variables))
|
|
154 nnml-server-alist)))
|
|
155 (let ((state (assoc server nnml-server-alist)))
|
|
156 (if state
|
|
157 (progn
|
|
158 (nnheader-restore-variables (nth 1 state))
|
|
159 (setq nnml-server-alist (delq state nnml-server-alist)))
|
|
160 (nnheader-set-init-variables nnml-server-variables defs)))
|
|
161 (setq nnml-current-server server)))
|
|
162
|
|
163 (defun nnml-close-server (&optional server)
|
|
164 t)
|
|
165
|
|
166 (defun nnml-server-opened (&optional server)
|
|
167 (and (equal server nnml-current-server)
|
|
168 nntp-server-buffer
|
|
169 (buffer-name nntp-server-buffer)))
|
|
170
|
|
171 (defun nnml-status-message (&optional server)
|
|
172 nnml-status-string)
|
|
173
|
|
174 (defun nnml-request-article (id &optional newsgroup server buffer)
|
|
175 (nnml-possibly-change-directory newsgroup)
|
|
176 (let ((file (if (stringp id)
|
|
177 nil
|
|
178 (concat nnml-current-directory (int-to-string id))))
|
|
179 (nntp-server-buffer (or buffer nntp-server-buffer)))
|
|
180 (if (and (stringp file)
|
|
181 (file-exists-p file)
|
|
182 (not (file-directory-p file)))
|
|
183 (save-excursion
|
|
184 (nnmail-find-file file)))))
|
|
185
|
|
186 (defun nnml-request-group (group &optional server dont-check)
|
|
187 (if (not (nnml-possibly-change-directory group))
|
|
188 (progn
|
|
189 (setq nnml-status-string "Invalid group (no such directory)")
|
|
190 nil)
|
|
191 (if dont-check
|
|
192 t
|
|
193 (nnml-get-new-mail group)
|
|
194 (nnmail-activate 'nnml)
|
|
195 (let ((active (nth 1 (assoc group nnml-group-alist))))
|
|
196 (save-excursion
|
|
197 (set-buffer nntp-server-buffer)
|
|
198 (erase-buffer)
|
|
199 (if (not active)
|
|
200 ()
|
|
201 (insert (format "211 %d %d %d %s\n"
|
|
202 (max (1+ (- (cdr active) (car active))) 0)
|
|
203 (car active) (cdr active) group))
|
|
204 t))))))
|
|
205
|
|
206 (defun nnml-close-group (group &optional server)
|
|
207 t)
|
|
208
|
|
209 (defun nnml-request-close ()
|
|
210 (setq nnml-current-server nil)
|
|
211 (setq nnml-server-alist nil)
|
|
212 t)
|
|
213
|
|
214 (defun nnml-request-create-group (group &optional server)
|
|
215 (nnmail-activate 'nnml)
|
|
216 (or (assoc group nnml-group-alist)
|
|
217 (let (active)
|
|
218 (setq nnml-group-alist (cons (list group (setq active (cons 1 0)))
|
|
219 nnml-group-alist))
|
|
220 (nnml-possibly-create-directory group)
|
|
221 (nnml-possibly-change-directory group)
|
|
222 (let ((articles (mapcar
|
|
223 (lambda (file)
|
|
224 (string-to-int file))
|
|
225 (directory-files
|
|
226 nnml-current-directory nil "^[0-9]+$"))))
|
|
227 (and articles
|
|
228 (progn
|
|
229 (setcar active (apply 'min articles))
|
|
230 (setcdr active (apply 'max articles)))))
|
|
231 (nnmail-save-active nnml-group-alist nnml-active-file)))
|
|
232 t)
|
|
233
|
|
234 (defun nnml-request-list (&optional server)
|
|
235 (if server (nnml-get-new-mail))
|
|
236 (save-excursion
|
|
237 (nnmail-find-file nnml-active-file)
|
|
238 (setq nnml-group-alist (nnmail-get-active))))
|
|
239
|
|
240 (defun nnml-request-newgroups (date &optional server)
|
|
241 (nnml-request-list server))
|
|
242
|
|
243 (defun nnml-request-list-newsgroups (&optional server)
|
|
244 (save-excursion
|
|
245 (nnmail-find-file nnml-newsgroups-file)))
|
|
246
|
|
247 (defun nnml-request-post (&optional server)
|
|
248 (mail-send-and-exit nil))
|
|
249
|
|
250 (defalias 'nnml-request-post-buffer 'nnmail-request-post-buffer)
|
|
251
|
|
252 (defun nnml-request-expire-articles (articles newsgroup &optional server force)
|
|
253 (nnml-possibly-change-directory newsgroup)
|
|
254 (let* ((days (or (and nnmail-expiry-wait-function
|
|
255 (funcall nnmail-expiry-wait-function newsgroup))
|
|
256 nnmail-expiry-wait))
|
|
257 (active-articles
|
|
258 (mapcar
|
|
259 (function
|
|
260 (lambda (name)
|
|
261 (string-to-int name)))
|
|
262 (directory-files nnml-current-directory nil "^[0-9]+$" t)))
|
|
263 (max-article (and active-articles (apply 'max active-articles)))
|
|
264 (is-old t)
|
|
265 article rest mod-time)
|
|
266 (nnmail-activate 'nnml)
|
|
267
|
|
268 (while (and articles is-old)
|
|
269 (setq article (concat nnml-current-directory
|
|
270 (int-to-string (car articles))))
|
|
271 (if (setq mod-time (nth 5 (file-attributes article)))
|
|
272 (if (and (or (not nnmail-keep-last-article)
|
|
273 (not max-article)
|
|
274 (not (= (car articles) max-article)))
|
|
275 (or force
|
|
276 (and (not (equal mod-time '(0 0)))
|
|
277 (setq is-old
|
|
278 (> (nnmail-days-between
|
|
279 (current-time-string)
|
|
280 (current-time-string mod-time))
|
|
281 days)))))
|
|
282 (progn
|
|
283 (and gnus-verbose-backends
|
|
284 (message "Deleting article %s..." article))
|
|
285 (condition-case ()
|
|
286 (delete-file article)
|
|
287 (file-error
|
|
288 (setq rest (cons (car articles) rest))))
|
|
289 (setq active-articles (delq (car articles) active-articles))
|
|
290 (nnml-nov-delete-article newsgroup (car articles)))
|
|
291 (setq rest (cons (car articles) rest))))
|
|
292 (setq articles (cdr articles)))
|
|
293 (let ((active (nth 1 (assoc newsgroup nnml-group-alist))))
|
|
294 (and active
|
|
295 (setcar active (or (and active-articles
|
|
296 (apply 'min active-articles))
|
|
297 0)))
|
|
298 (nnmail-save-active nnml-group-alist nnml-active-file))
|
|
299 (nnml-save-nov)
|
|
300 (message "")
|
|
301 (nconc rest articles)))
|
|
302
|
|
303 (defun nnml-request-move-article
|
|
304 (article group server accept-form &optional last)
|
|
305 (let ((buf (get-buffer-create " *nnml move*"))
|
|
306 result)
|
|
307 (and
|
|
308 (nnml-request-article article group server)
|
|
309 (save-excursion
|
|
310 (set-buffer buf)
|
|
311 (insert-buffer-substring nntp-server-buffer)
|
|
312 (setq result (eval accept-form))
|
|
313 (kill-buffer (current-buffer))
|
|
314 result)
|
|
315 (progn
|
|
316 (condition-case ()
|
|
317 (delete-file (concat nnml-current-directory
|
|
318 (int-to-string article)))
|
|
319 (file-error nil))
|
|
320 (nnml-nov-delete-article group article)
|
|
321 (and last (nnml-save-nov))))
|
|
322 result))
|
|
323
|
|
324 (defun nnml-request-accept-article (group &optional last)
|
|
325 (let (result)
|
|
326 (if (stringp group)
|
|
327 (and
|
|
328 (nnmail-activate 'nnml)
|
|
329 ;; We trick the choosing function into believing that only one
|
|
330 ;; group is availiable.
|
|
331 (let ((nnmail-split-methods (list (list group ""))))
|
|
332 (setq result (car (nnml-save-mail))))
|
|
333 (progn
|
|
334 (nnmail-save-active nnml-group-alist nnml-active-file)
|
|
335 (and last (nnml-save-nov))))
|
|
336 (and
|
|
337 (nnmail-activate 'nnml)
|
|
338 (setq result (car (nnml-save-mail)))
|
|
339 (progn
|
|
340 (nnmail-save-active nnml-group-alist nnml-active-file)
|
|
341 (and last (nnml-save-nov)))))
|
|
342 result))
|
|
343
|
|
344 (defun nnml-request-replace-article (article group buffer)
|
|
345 (nnml-possibly-change-directory group)
|
|
346 (save-excursion
|
|
347 (set-buffer buffer)
|
|
348 (nnml-possibly-create-directory group)
|
|
349 (if (not (condition-case ()
|
|
350 (progn
|
|
351 (write-region (point-min) (point-max)
|
|
352 (concat nnml-current-directory
|
|
353 (int-to-string article))
|
|
354 nil (if gnus-verbose-backends nil 'nomesg))
|
|
355 t)
|
|
356 (error nil)))
|
|
357 ()
|
|
358 (let ((chars (nnmail-insert-lines))
|
|
359 (art (concat (int-to-string article) "\t"))
|
|
360 nov-line)
|
|
361 (setq nov-line (nnml-make-nov-line chars))
|
|
362 ;; Replace the NOV line in the NOV file.
|
|
363 (save-excursion
|
|
364 (set-buffer (nnml-open-nov group))
|
|
365 (goto-char (point-min))
|
|
366 (if (or (looking-at art)
|
|
367 (search-forward (concat "\n" art) nil t))
|
|
368 ;; Delete the old NOV line.
|
|
369 (delete-region (progn (beginning-of-line) (point))
|
|
370 (progn (forward-line 1) (point)))
|
|
371 ;; The line isn't here, so we have to find out where
|
|
372 ;; we should insert it. (This situation should never
|
|
373 ;; occur, but one likes to make sure...)
|
|
374 (while (and (looking-at "[0-9]+\t")
|
|
375 (< (string-to-int
|
|
376 (buffer-substring
|
|
377 (match-beginning 0) (match-end 0)))
|
|
378 article)
|
|
379 (zerop (forward-line 1)))))
|
|
380 (beginning-of-line)
|
|
381 (insert (int-to-string article) nov-line)
|
|
382 (nnml-save-nov)
|
|
383 t)))))
|
|
384
|
|
385
|
|
386
|
|
387 ;;; Internal functions
|
|
388
|
|
389 (defun nnml-retrieve-headers-with-nov (articles)
|
|
390 (if (or gnus-nov-is-evil nnml-nov-is-evil)
|
|
391 nil
|
|
392 (let ((first (car articles))
|
|
393 (last (progn (while (cdr articles) (setq articles (cdr articles)))
|
|
394 (car articles)))
|
|
395 (nov (concat nnml-current-directory nnml-nov-file-name)))
|
|
396 (if (file-exists-p nov)
|
|
397 (save-excursion
|
|
398 (set-buffer nntp-server-buffer)
|
|
399 (erase-buffer)
|
|
400 (insert-file-contents nov)
|
|
401 (goto-char (point-min))
|
|
402 (while (and (not (eobp)) (< first (read (current-buffer))))
|
|
403 (forward-line 1))
|
|
404 (beginning-of-line)
|
|
405 (if (not (eobp)) (delete-region 1 (point)))
|
|
406 (while (and (not (eobp)) (>= last (read (current-buffer))))
|
|
407 (forward-line 1))
|
|
408 (beginning-of-line)
|
|
409 (if (not (eobp)) (delete-region (point) (point-max)))
|
|
410 t)))))
|
|
411
|
|
412 (defun nnml-possibly-change-directory (newsgroup &optional force)
|
|
413 (if newsgroup
|
|
414 (let ((pathname (nnmail-article-pathname newsgroup nnml-directory)))
|
|
415 (and (or force (file-directory-p pathname))
|
|
416 (setq nnml-current-directory pathname)))
|
|
417 t))
|
|
418
|
|
419 (defun nnml-possibly-create-directory (group)
|
|
420 (let (dir dirs)
|
|
421 (setq dir (nnmail-article-pathname group nnml-directory))
|
|
422 (while (not (file-directory-p dir))
|
|
423 (setq dirs (cons dir dirs))
|
|
424 (setq dir (file-name-directory (directory-file-name dir))))
|
|
425 (while dirs
|
|
426 (make-directory (directory-file-name (car dirs)))
|
|
427 (and gnus-verbose-backends
|
|
428 (message "Creating mail directory %s" (car dirs)))
|
|
429 (setq dirs (cdr dirs)))))
|
|
430
|
|
431 (defun nnml-save-mail ()
|
|
432 "Called narrowed to an article."
|
|
433 (let ((group-art (nreverse (nnmail-article-group 'nnml-active-number)))
|
|
434 chars nov-line)
|
|
435 (setq chars (nnmail-insert-lines))
|
|
436 (nnmail-insert-xref group-art)
|
|
437 (run-hooks 'nnml-prepare-save-mail-hook)
|
|
438 (goto-char (point-min))
|
|
439 (while (looking-at "From ")
|
|
440 (replace-match "X-From-Line: ")
|
|
441 (forward-line 1))
|
|
442 ;; We save the article in all the newsgroups it belongs in.
|
|
443 (let ((ga group-art)
|
|
444 first)
|
|
445 (while ga
|
|
446 (nnml-possibly-create-directory (car (car ga)))
|
|
447 (let ((file (concat (nnmail-article-pathname
|
|
448 (car (car ga)) nnml-directory)
|
|
449 (int-to-string (cdr (car ga))))))
|
|
450 (if first
|
|
451 ;; It was already saved, so we just make a hard link.
|
|
452 (add-name-to-file first file t)
|
|
453 ;; Save the article.
|
|
454 (write-region (point-min) (point-max) file nil
|
|
455 (if gnus-verbose-backends nil 'nomesg))
|
|
456 (setq first file)))
|
|
457 (setq ga (cdr ga))))
|
|
458 ;; Generate a nov line for this article. We generate the nov
|
|
459 ;; line after saving, because nov generation destroys the
|
|
460 ;; header.
|
|
461 (setq nov-line (nnml-make-nov-line chars))
|
|
462 ;; Output the nov line to all nov databases that should have it.
|
|
463 (let ((ga group-art))
|
|
464 (while ga
|
|
465 (nnml-add-nov (car (car ga)) (cdr (car ga)) nov-line)
|
|
466 (setq ga (cdr ga))))
|
|
467 group-art))
|
|
468
|
|
469 (defun nnml-active-number (group)
|
|
470 "Compute the next article number in GROUP."
|
|
471 (let ((active (car (cdr (assoc group nnml-group-alist)))))
|
|
472 ;; The group wasn't known to nnml, so we just create an active
|
|
473 ;; entry for it.
|
|
474 (or active
|
|
475 (progn
|
|
476 (setq active (cons 1 0))
|
|
477 (setq nnml-group-alist (cons (list group active) nnml-group-alist))))
|
|
478 (setcdr active (1+ (cdr active)))
|
|
479 (while (file-exists-p
|
|
480 (concat (nnmail-article-pathname group nnml-directory)
|
|
481 (int-to-string (cdr active))))
|
|
482 (setcdr active (1+ (cdr active))))
|
|
483 (cdr active)))
|
|
484
|
|
485 (defun nnml-get-new-mail (&optional group)
|
|
486 "Read new incoming mail."
|
|
487 (let* ((spools (nnmail-get-spool-files group))
|
|
488 (group-in group)
|
|
489 incoming incomings)
|
|
490 (if (or (not nnml-get-new-mail) (not nnmail-spool-file))
|
|
491 ()
|
|
492 ;; We first activate all the groups.
|
|
493 (nnmail-activate 'nnml)
|
|
494 ;; The we go through all the existing spool files and split the
|
|
495 ;; mail from each.
|
|
496 (while spools
|
|
497 (and
|
|
498 (file-exists-p (car spools))
|
|
499 (> (nth 7 (file-attributes (car spools))) 0)
|
|
500 (progn
|
|
501 (and gnus-verbose-backends
|
|
502 (message "nnml: Reading incoming mail..."))
|
|
503 (if (not (setq incoming
|
|
504 (nnmail-move-inbox
|
|
505 (car spools) (concat nnml-directory "Incoming"))))
|
|
506 ()
|
|
507 (setq group (nnmail-get-split-group (car spools) group-in))
|
|
508 (nnmail-split-incoming incoming 'nnml-save-mail nil group)
|
|
509 (setq incomings (cons incoming incomings)))))
|
|
510 (setq spools (cdr spools)))
|
|
511 ;; If we did indeed read any incoming spools, we save all info.
|
|
512 (if incoming
|
|
513 (progn
|
|
514 (nnmail-save-active nnml-group-alist nnml-active-file)
|
|
515 (nnml-save-nov)
|
|
516 (run-hooks 'nnmail-read-incoming-hook)
|
|
517 (and gnus-verbose-backends
|
|
518 (message "nnml: Reading incoming mail...done"))))
|
|
519 (while incomings
|
|
520 (setq incoming (car incomings))
|
|
521 (and nnmail-delete-incoming
|
|
522 (file-exists-p incoming)
|
|
523 (file-writable-p incoming)
|
|
524 (delete-file incoming))
|
|
525 (setq incomings (cdr incomings))))))
|
|
526
|
|
527
|
|
528 (defun nnml-add-nov (group article line)
|
|
529 "Add a nov line for the GROUP base."
|
|
530 (save-excursion
|
|
531 (set-buffer (nnml-open-nov group))
|
|
532 (goto-char (point-max))
|
|
533 (insert (int-to-string article) line)))
|
|
534
|
|
535 (defsubst nnml-header-value ()
|
|
536 (buffer-substring (match-end 0) (save-excursion (end-of-line) (point))))
|
|
537
|
|
538 (defun nnml-make-nov-line (chars)
|
|
539 "Create a nov from the current headers."
|
|
540 (let ((case-fold-search t)
|
|
541 subject from date id references lines xref in-reply-to char)
|
|
542 (save-excursion
|
|
543 (save-restriction
|
|
544 (goto-char (point-min))
|
|
545 (narrow-to-region
|
|
546 (point)
|
|
547 (1- (or (search-forward "\n\n" nil t) (point-max))))
|
|
548 ;; Fold continuation lines.
|
|
549 (goto-char (point-min))
|
|
550 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
|
|
551 (replace-match " " t t))
|
|
552 (subst-char-in-region (point-min) (point-max) ?\t ? )
|
|
553 ;; [number subject from date id references chars lines xref]
|
|
554 (save-excursion
|
|
555 (goto-char (point-min))
|
|
556 (while (re-search-forward "^\\(from\\|subject\\|message-id\\|date\\|lines\\|xref\\|references\\|in-reply-to\\): "
|
|
557 nil t)
|
|
558 (beginning-of-line)
|
|
559 (setq char (downcase (following-char)))
|
|
560 (cond
|
|
561 ((eq char ?s)
|
|
562 (setq subject (nnml-header-value)))
|
|
563 ((eq char ?f)
|
|
564 (setq from (nnml-header-value)))
|
|
565 ((eq char ?x)
|
|
566 (setq xref (nnml-header-value)))
|
|
567 ((eq char ?l)
|
|
568 (setq lines (nnml-header-value)))
|
|
569 ((eq char ?d)
|
|
570 (setq date (nnml-header-value)))
|
|
571 ((eq char ?m)
|
|
572 (setq id (setq id (nnml-header-value))))
|
|
573 ((eq char ?r)
|
|
574 (setq references (nnml-header-value)))
|
|
575 ((eq char ?i)
|
|
576 (setq in-reply-to (nnml-header-value))))
|
|
577 (forward-line 1))
|
|
578
|
|
579 (and (not references)
|
|
580 in-reply-to
|
|
581 (string-match "<[^>]+>" in-reply-to)
|
|
582 (setq references
|
|
583 (substring in-reply-to (match-beginning 0)
|
|
584 (match-end 0)))))
|
|
585 ;; [number subject from date id references chars lines xref]
|
|
586 (format "\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t\n"
|
|
587 (or subject "(none)")
|
|
588 (or from "(nobody)") (or date "")
|
|
589 (or id (concat "nnml-dummy-id-"
|
|
590 (mapconcat
|
|
591 (lambda (time) (int-to-string time))
|
|
592 (current-time) "-")))
|
|
593 (or references "")
|
|
594 (or chars 0) (or lines "0") (or xref ""))))))
|
|
595
|
|
596 (defun nnml-open-nov (group)
|
|
597 (or (cdr (assoc group nnml-nov-buffer-alist))
|
|
598 (let ((buffer (find-file-noselect
|
|
599 (concat (nnmail-article-pathname
|
|
600 group nnml-directory) nnml-nov-file-name))))
|
|
601 (save-excursion
|
|
602 (set-buffer buffer)
|
|
603 (buffer-disable-undo (current-buffer)))
|
|
604 (setq nnml-nov-buffer-alist
|
|
605 (cons (cons group buffer) nnml-nov-buffer-alist))
|
|
606 buffer)))
|
|
607
|
|
608 (defun nnml-save-nov ()
|
|
609 (save-excursion
|
|
610 (while nnml-nov-buffer-alist
|
|
611 (if (buffer-name (cdr (car nnml-nov-buffer-alist)))
|
|
612 (progn
|
|
613 (set-buffer (cdr (car nnml-nov-buffer-alist)))
|
|
614 (and (buffer-modified-p)
|
|
615 (write-region
|
|
616 1 (point-max) (buffer-file-name) nil 'nomesg))
|
|
617 (set-buffer-modified-p nil)
|
|
618 (kill-buffer (current-buffer))))
|
|
619 (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
|
|
620
|
|
621 ;;;###autoload
|
|
622 (defun nnml-generate-nov-databases (dir)
|
|
623 "Generate nov databases in all nnml mail newsgroups."
|
|
624 (interactive
|
|
625 (progn
|
|
626 (setq nnml-group-alist nil)
|
|
627 (list nnml-directory)))
|
|
628 (nnml-open-server (or nnml-current-server ""))
|
|
629 (let ((dirs (directory-files dir t nil t)))
|
|
630 (while dirs
|
|
631 (if (and (not (string-match "/\\.\\.$" (car dirs)))
|
|
632 (not (string-match "/\\.$" (car dirs)))
|
|
633 (file-directory-p (car dirs)))
|
|
634 (nnml-generate-nov-databases (car dirs)))
|
|
635 (setq dirs (cdr dirs))))
|
|
636 (let ((files (sort
|
|
637 (mapcar
|
|
638 (function
|
|
639 (lambda (name)
|
|
640 (string-to-int name)))
|
|
641 (directory-files dir nil "^[0-9]+$" t))
|
|
642 (function <)))
|
|
643 (nov (concat dir "/" nnml-nov-file-name))
|
|
644 (nov-buffer (get-buffer-create "*nov*"))
|
|
645 nov-line chars)
|
|
646 (if files
|
|
647 (setq nnml-group-alist
|
|
648 (cons (list (nnmail-replace-chars-in-string
|
|
649 (substring (expand-file-name dir)
|
|
650 (length (expand-file-name
|
|
651 nnml-directory)))
|
|
652 ?/ ?.)
|
|
653 (cons (car files)
|
|
654 (let ((f files))
|
|
655 (while (cdr f) (setq f (cdr f)))
|
|
656 (car f))))
|
|
657 nnml-group-alist)))
|
|
658 (if files
|
|
659 (save-excursion
|
|
660 (set-buffer nntp-server-buffer)
|
|
661 (if (file-exists-p nov)
|
|
662 (delete-file nov))
|
|
663 (save-excursion
|
|
664 (set-buffer nov-buffer)
|
|
665 (buffer-disable-undo (current-buffer))
|
|
666 (erase-buffer))
|
|
667 (while files
|
|
668 (erase-buffer)
|
|
669 (insert-file-contents (concat dir "/" (int-to-string (car files))))
|
|
670 (goto-char (point-min))
|
|
671 (narrow-to-region 1 (save-excursion (search-forward "\n\n" nil t)
|
|
672 (setq chars (- (point-max)
|
|
673 (point)))
|
|
674 (point)))
|
|
675 (if (not (= 0 chars)) ; none of them empty files...
|
|
676 (progn
|
|
677 (setq nov-line (nnml-make-nov-line chars))
|
|
678 (save-excursion
|
|
679 (set-buffer nov-buffer)
|
|
680 (goto-char (point-max))
|
|
681 (insert (int-to-string (car files)) nov-line))))
|
|
682 (widen)
|
|
683 (setq files (cdr files)))
|
|
684 (save-excursion
|
|
685 (set-buffer nov-buffer)
|
|
686 (write-region 1 (point-max) (expand-file-name nov) nil
|
|
687 'nomesg)
|
|
688 (kill-buffer (current-buffer)))))
|
|
689 (nnmail-save-active nnml-group-alist nnml-active-file)))
|
|
690
|
|
691 (defun nnml-nov-delete-article (group article)
|
|
692 (save-excursion
|
|
693 (set-buffer (nnml-open-nov group))
|
|
694 (goto-char (point-min))
|
|
695 (if (re-search-forward (concat "^" (int-to-string article) "\t") nil t)
|
|
696 (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
|
|
697 t))
|
|
698
|
|
699 (provide 'nnml)
|
|
700
|
|
701 ;;; nnml.el ends here
|