*** empty log message ***
[gnus] / lisp / nnsoup.el
1 ;;; nnsoup.el --- SOUP 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 'gnus-soup)
32 (require 'gnus-msg)
33 (require 'nnoo)
34 (eval-when-compile (require 'cl))
35
36 (nnoo-declare nnsoup)
37
38 (defvoo nnsoup-directory "~/SOUP/"
39   "*SOUP packet directory.")
40
41 (defvoo nnsoup-tmp-directory "/tmp/"
42   "*Where nnsoup will store temporary files.")
43
44 (defvoo nnsoup-replies-directory (concat nnsoup-directory "replies/")
45   "*Directory where outgoing packets will be composed.")
46
47 (defvoo nnsoup-replies-format-type ?n
48   "*Format of the replies packages.")
49
50 (defvoo nnsoup-replies-index-type ?n
51   "*Index type of the replies packages.")
52
53 (defvoo nnsoup-active-file (concat nnsoup-directory "active")
54   "Active file.")
55
56 (defvoo nnsoup-packer "tar cf - %s | gzip > $HOME/Soupin%d.tgz"
57   "Format string command for packing a SOUP packet.
58 The SOUP files will be inserted where the %s is in the string.
59 This string MUST contain both %s and %d.  The file number will be
60 inserted where %d appears.")
61
62 (defvoo nnsoup-unpacker "gunzip -c %s | tar xvf -"
63   "*Format string command for unpacking a SOUP packet.
64 The SOUP packet file name will be inserted at the %s.")
65
66 (defvoo nnsoup-packet-directory "~/"
67   "*Where nnsoup will look for incoming packets.")
68
69 (defvoo nnsoup-packet-regexp "Soupout"
70   "*Regular expression matching SOUP packets in `nnsoup-packet-directory'.")
71
72 \f
73
74 (defconst nnsoup-version "nnsoup 0.0"
75   "nnsoup version.")
76
77 (defvoo nnsoup-status-string "")
78 (defvoo nnsoup-group-alist nil)
79 (defvoo nnsoup-current-prefix 0)
80 (defvoo nnsoup-replies-list nil)
81 (defvoo nnsoup-buffers nil)
82 (defvoo nnsoup-current-group nil)
83 (defvoo nnsoup-group-alist-touched nil)
84
85 \f
86
87 ;;; Interface functions.
88
89 (nnoo-define-basics nnsoup)
90
91 (deffoo nnsoup-retrieve-headers (sequence &optional group server fetch-old)
92   (nnsoup-possibly-change-group group)
93   (save-excursion
94     (set-buffer nntp-server-buffer)
95     (erase-buffer)
96     (let ((areas (cddr (assoc nnsoup-current-group nnsoup-group-alist)))
97           (articles sequence)
98           (use-nov t)
99           useful-areas this-area-seq msg-buf)
100       (if (stringp (car sequence))
101           ;; We don't support fetching by Message-ID.
102           'headers
103         ;; We go through all the areas and find which files the
104         ;; articles in SEQUENCE come from.
105         (while (and areas sequence)
106           ;; Peel off areas that are below sequence.
107           (while (and areas (< (cdaar areas) (car sequence)))
108             (setq areas (cdr areas)))
109           (when areas
110             ;; This is a useful area.
111             (push (car areas) useful-areas)
112             (setq this-area-seq nil)
113             ;; We take note whether this MSG has a corresponding IDX
114             ;; for later use.
115             (when (or (= (gnus-soup-encoding-index 
116                           (gnus-soup-area-encoding (nth 1 (car areas)))) ?n)
117                       (not (file-exists-p
118                             (nnsoup-file
119                              (gnus-soup-area-prefix (nth 1 (car areas)))))))
120               (setq use-nov nil))
121             ;; We assign the portion of `sequence' that is relevant to
122             ;; this MSG packet to this packet.
123             (while (and sequence (<= (car sequence) (cdaar areas)))
124               (push (car sequence) this-area-seq)
125               (setq sequence (cdr sequence)))
126             (setcar useful-areas (cons (nreverse this-area-seq)
127                                        (car useful-areas)))))
128
129         ;; We now have a list of article numbers and corresponding
130         ;; areas. 
131         (setq useful-areas (nreverse useful-areas))
132
133         ;; Two different approaches depending on whether all the MSG
134         ;; files have corresponding IDX files.  If they all do, we
135         ;; simply return the relevant IDX files and let Gnus sort out
136         ;; what lines are relevant.  If some of the IDX files are
137         ;; missing, we must return HEADs for all the articles.
138         (if use-nov
139             ;; We have IDX files for all areas.
140             (progn
141               (while useful-areas
142                 (goto-char (point-max))
143                 (let ((b (point))
144                       (number (car (nth 1 (car useful-areas))))
145                       (index-buffer (nnsoup-index-buffer
146                                      (gnus-soup-area-prefix
147                                       (nth 2 (car useful-areas))))))
148                   (when index-buffer
149                     (insert-buffer-substring index-buffer)
150                     (goto-char b)
151                     ;; We have to remove the index number entires and
152                     ;; insert article numbers instead.
153                     (while (looking-at "[0-9]+")
154                       (replace-match (int-to-string number) t t)
155                       (incf number)
156                       (forward-line 1))))
157                 (setq useful-areas (cdr useful-areas)))
158               'nov)
159           ;; We insert HEADs.
160           (while useful-areas
161             (setq articles (caar useful-areas)
162                   useful-areas (cdr useful-areas))
163             (while articles
164               (when (setq msg-buf
165                           (nnsoup-narrow-to-article 
166                            (car articles) (cdar useful-areas) 'head))
167                 (goto-char (point-max))
168                 (insert (format "221 %d Article retrieved.\n" (car articles)))
169                 (insert-buffer-substring msg-buf)
170                 (goto-char (point-max))
171                 (insert ".\n"))
172               (setq articles (cdr articles))))
173
174           (nnheader-fold-continuation-lines)
175           'headers)))))
176
177 (deffoo nnsoup-open-server (server &optional defs)
178   (nnoo-change-server 'nnsoup server defs)
179   (when (not (file-exists-p nnsoup-directory))
180     (condition-case ()
181         (make-directory nnsoup-directory t)
182       (error t)))
183   (cond 
184    ((not (file-exists-p nnsoup-directory))
185     (nnsoup-close-server)
186     (nnheader-report 'nnsoup "Couldn't create directory: %s" nnsoup-directory))
187    ((not (file-directory-p (file-truename nnsoup-directory)))
188     (nnsoup-close-server)
189     (nnheader-report 'nnsoup "Not a directory: %s" nnsoup-directory))
190    (t
191     (nnsoup-read-active-file)
192     (nnheader-report 'nnsoup "Opened server %s using directory %s"
193                      server nnsoup-directory)
194     t)))
195
196 (deffoo nnsoup-request-close ()
197   (nnsoup-write-active-file)
198   (nnsoup-write-replies)
199   (gnus-soup-save-areas)
200   ;; Kill all nnsoup buffers.
201   (let (buffer)
202     (while nnsoup-buffers
203       (setq buffer (cdr (pop nnsoup-buffers)))
204       (and buffer
205            (buffer-name buffer)
206            (kill-buffer buffer))))
207   (setq nnsoup-group-alist nil
208         nnsoup-group-alist-touched nil
209         nnsoup-current-group nil
210         nnsoup-replies-list nil)
211   (nnoo-close-server 'nnoo)
212   t)
213
214 (deffoo nnsoup-request-article (id &optional newsgroup server buffer)
215   (nnsoup-possibly-change-group newsgroup)
216   (let (buf)
217     (save-excursion
218       (set-buffer (or buffer nntp-server-buffer))
219       (erase-buffer)
220       (when (and (not (stringp id))
221                  (setq buf (nnsoup-narrow-to-article id)))
222         (insert-buffer-substring buf)
223         t))))
224
225 (deffoo nnsoup-request-group (group &optional server dont-check)
226   (nnsoup-possibly-change-group group)
227   (if dont-check 
228       t
229     (let ((active (cadr (assoc group nnsoup-group-alist))))
230       (if (not active)
231           (nnheader-report 'nnsoup "No such group: %s" group)
232         (nnheader-insert 
233          "211 %d %d %d %s\n" 
234          (max (1+ (- (cdr active) (car active))) 0)
235          (car active) (cdr active) group)))))
236
237 (deffoo nnsoup-request-type (group &optional article)
238   (nnsoup-possibly-change-group group)
239   (if (not article)
240       'unknown
241     (let ((kind (gnus-soup-encoding-kind 
242                  (gnus-soup-area-encoding
243                   (nth 1 (nnsoup-article-to-area
244                           article nnsoup-current-group))))))
245       (cond ((= kind ?m) 'mail)
246             ((= kind ?n) 'news)
247             (t 'unknown)))))
248
249 (deffoo nnsoup-close-group (group &optional server)
250   ;; Kill all nnsoup buffers.
251   (let ((buffers nnsoup-buffers)
252         elem)
253     (while buffers
254       (when (equal (car (setq elem (pop buffers))) group)
255         (setq nnsoup-buffers (delq elem nnsoup-buffers))
256         (and (cdr elem) (buffer-name (cdr elem))
257              (kill-buffer (cdr elem))))))
258   t)
259
260 (deffoo nnsoup-request-list (&optional server)
261   (save-excursion
262     (set-buffer nntp-server-buffer)
263     (erase-buffer)
264     (unless nnsoup-group-alist
265       (nnsoup-read-active-file))
266     (let ((alist nnsoup-group-alist)
267           (standard-output (current-buffer))
268           entry)
269       (while (setq entry (pop alist))
270         (insert (car entry) " ")
271         (princ (cdadr entry))
272         (insert " ")
273         (princ (caadr entry))
274         (insert " y\n"))
275       t)))
276
277 (deffoo nnsoup-request-scan (group &optional server)
278   (nnsoup-unpack-packets))
279
280 (deffoo nnsoup-request-newgroups (date &optional server)
281   (nnsoup-request-list))
282
283 (deffoo nnsoup-request-list-newsgroups (&optional server)
284   nil)
285
286 (deffoo nnsoup-request-post (&optional server)
287   (nnsoup-store-reply "news")
288   t)
289
290 (deffoo nnsoup-request-mail (&optional server)
291   (nnsoup-store-reply "mail")
292   t)
293
294 (deffoo nnsoup-request-expire-articles (articles group &optional server force)
295   (nnsoup-possibly-change-group group)
296   (let* ((total-infolist (assoc group nnsoup-group-alist))
297          (active (cadr total-infolist))
298          (infolist (cddr total-infolist))
299          info range-list mod-time prefix)
300     (while infolist
301       (setq info (pop infolist)
302             range-list (gnus-uncompress-range (car info))
303             prefix (gnus-soup-area-prefix (nth 1 info)))
304       (when ;; All the articles in this file are marked for expiry.
305           (and (or (setq mod-time (nth 5 (file-attributes
306                                           (nnsoup-file prefix))))
307                    (setq mod-time (nth 5 (file-attributes
308                                           (nnsoup-file prefix t)))))
309                (gnus-sublist-p articles range-list)
310                ;; This file is old enough. 
311                (nnmail-expired-article-p group mod-time force))
312         ;; Ok, we delete this file.
313         (when (ignore-errors
314                 (nnheader-message 
315                  5 "Deleting %s in group %s..." (nnsoup-file prefix)
316                  group)
317                 (when (file-exists-p (nnsoup-file prefix))
318                   (delete-file (nnsoup-file prefix)))
319                 (nnheader-message 
320                  5 "Deleting %s in group %s..." (nnsoup-file prefix t)
321                  group)
322                 (when (file-exists-p (nnsoup-file prefix t))
323                   (delete-file (nnsoup-file prefix t)))
324                 t)
325           (setcdr (cdr total-infolist) (delq info (cddr total-infolist)))
326           (setq articles (gnus-sorted-complement articles range-list))))
327       (when (not mod-time)
328         (setcdr (cdr total-infolist) (delq info (cddr total-infolist)))))
329     (if (cddr total-infolist)
330         (setcar active (caaadr (cdr total-infolist)))
331       (setcar active (1+ (cdr active))))
332     (nnsoup-write-active-file t)
333     ;; Return the articles that weren't expired.
334     articles))
335
336 \f
337 ;;; Internal functions
338
339 (defun nnsoup-possibly-change-group (group &optional force)
340   (if group
341       (setq nnsoup-current-group group)
342     t))
343
344 (defun nnsoup-read-active-file ()
345   (setq nnsoup-group-alist nil)
346   (when (file-exists-p nnsoup-active-file)
347     (ignore-errors
348       (load nnsoup-active-file t t t))
349     ;; Be backwards compatible.
350     (when (and nnsoup-group-alist
351                (not (atom (caadar nnsoup-group-alist))))
352       (let ((alist nnsoup-group-alist)
353             entry e min max)
354         (while (setq e (cdr (setq entry (pop alist))))
355           (setq min (caaar e))
356           (while (cdr e)
357             (setq e (cdr e)))
358           (setq max (cdaar e))
359           (setcdr entry (cons (cons min max) (cdr entry)))))
360       (setq nnsoup-group-alist-touched t))
361     nnsoup-group-alist))
362
363 (defun nnsoup-write-active-file (&optional force)
364   (when (and nnsoup-group-alist
365              (or force 
366                  nnsoup-group-alist-touched))
367     (setq nnsoup-group-alist-touched nil)
368     (nnheader-temp-write nnsoup-active-file
369       (gnus-prin1 `(setq nnsoup-group-alist ',nnsoup-group-alist))
370       (insert "\n")
371       (gnus-prin1 `(setq nnsoup-current-prefix ,nnsoup-current-prefix))
372       (insert "\n"))))
373
374 (defun nnsoup-next-prefix ()
375   "Return the next free prefix."
376   (let (prefix)
377     (while (or (file-exists-p 
378                 (nnsoup-file (setq prefix (int-to-string
379                                            nnsoup-current-prefix))))
380                (file-exists-p (nnsoup-file prefix t)))
381       (incf nnsoup-current-prefix))
382     (incf nnsoup-current-prefix)
383     prefix))
384
385 (defun nnsoup-read-areas ()
386   (when (file-exists-p (concat nnsoup-tmp-directory "AREAS"))
387     (save-excursion
388       (set-buffer nntp-server-buffer)
389       (let ((areas (gnus-soup-parse-areas (concat nnsoup-tmp-directory "AREAS")))
390             entry number area lnum cur-prefix file)
391         ;; Go through all areas in the new AREAS file.
392         (while (setq area (pop areas))
393           ;; Change the name to the permanent name and move the files.
394           (setq cur-prefix (nnsoup-next-prefix))
395           (message "Incorporating file %s..." cur-prefix)
396           (when (file-exists-p 
397                  (setq file (concat nnsoup-tmp-directory
398                                     (gnus-soup-area-prefix area) ".IDX")))
399             (rename-file file (nnsoup-file cur-prefix)))
400           (when (file-exists-p 
401                  (setq file (concat nnsoup-tmp-directory 
402                                     (gnus-soup-area-prefix area) ".MSG")))
403             (rename-file file (nnsoup-file cur-prefix t))
404             (gnus-soup-set-area-prefix area cur-prefix)
405             ;; Find the number of new articles in this area.
406             (setq number (nnsoup-number-of-articles area))
407             (if (not (setq entry (assoc (gnus-soup-area-name area)
408                                         nnsoup-group-alist)))
409                 ;; If this is a new area (group), we just add this info to
410                 ;; the group alist. 
411                 (push (list (gnus-soup-area-name area)
412                             (cons 1 number)
413                             (list (cons 1 number) area))
414                       nnsoup-group-alist)
415               ;; There are already articles in this group, so we add this
416               ;; info to the end of the entry.
417               (nconc entry (list (list (cons (1+ (setq lnum (cdadr entry)))
418                                              (+ lnum number))
419                                        area)))
420               (setcdr (cadr entry) (+ lnum number))))))
421       (nnsoup-write-active-file t)
422       (delete-file (concat nnsoup-tmp-directory "AREAS")))))
423
424 (defun nnsoup-number-of-articles (area)
425   (save-excursion
426     (cond 
427      ;; If the number is in the area info, we just return it.
428      ((gnus-soup-area-number area)
429       (gnus-soup-area-number area))
430      ;; If there is an index file, we just count the lines.
431      ((/= (gnus-soup-encoding-index (gnus-soup-area-encoding area)) ?n)
432       (set-buffer (nnsoup-index-buffer (gnus-soup-area-prefix area)))
433       (count-lines (point-min) (point-max)))
434      ;; We do it the hard way - re-searching through the message
435      ;; buffer. 
436      (t
437       (set-buffer (nnsoup-message-buffer (gnus-soup-area-prefix area)))
438       (goto-char (point-min))
439       (let ((regexp (nnsoup-header (gnus-soup-encoding-format 
440                                     (gnus-soup-area-encoding area))))
441             (num 0))
442         (while (re-search-forward regexp nil t)
443           (setq num (1+ num)))
444         num)))))
445
446 (defun nnsoup-index-buffer (prefix &optional message)
447   (let* ((file (concat prefix (if message ".MSG" ".IDX")))
448          (buffer-name (concat " *nnsoup " file "*")))
449     (or (get-buffer buffer-name)        ; File already loaded.
450         (when (file-exists-p (concat nnsoup-directory file))
451           (save-excursion               ; Load the file.
452             (set-buffer (get-buffer-create buffer-name))
453             (buffer-disable-undo (current-buffer))
454             (push (cons nnsoup-current-group (current-buffer)) nnsoup-buffers)
455             (nnheader-insert-file-contents (concat nnsoup-directory file))
456             (current-buffer))))))
457
458 (defun nnsoup-file (prefix &optional message)
459   (expand-file-name
460    (concat nnsoup-directory prefix (if message ".MSG" ".IDX"))))
461
462 (defun nnsoup-message-buffer (prefix)
463   (nnsoup-index-buffer prefix 'msg))
464
465 (defun nnsoup-unpack-packets ()
466   "Unpack all packets in `nnsoup-packet-directory'."
467   (let ((packets (directory-files
468                   nnsoup-packet-directory t nnsoup-packet-regexp))
469         packet)
470     (while (setq packet (pop packets))
471       (message "nnsoup: unpacking %s..." packet)
472       (if (not (gnus-soup-unpack-packet 
473                 nnsoup-tmp-directory nnsoup-unpacker packet))
474           (message "Couldn't unpack %s" packet)
475         (delete-file packet)
476         (nnsoup-read-areas)
477         (message "Unpacking...done")))))
478
479 (defun nnsoup-narrow-to-article (article &optional area head)
480   (let* ((area (or area (nnsoup-article-to-area article nnsoup-current-group)))
481          (prefix (and area (gnus-soup-area-prefix (nth 1 area))))
482          (msg-buf (and prefix (nnsoup-index-buffer prefix 'msg)))
483          beg end)
484     (when area
485       (save-excursion
486         (cond
487          ;; There is no MSG file.
488          ((null msg-buf)
489           nil)
490        
491          ;; We use the index file to find out where the article begins and ends. 
492          ((and (= (gnus-soup-encoding-index 
493                    (gnus-soup-area-encoding (nth 1 area)))
494                   ?c)
495                (file-exists-p (nnsoup-file prefix)))
496           (set-buffer (nnsoup-index-buffer prefix))
497           (widen)
498           (goto-char (point-min))
499           (forward-line (- article (caar area)))
500           (setq beg (read (current-buffer)))
501           (forward-line 1)
502           (if (looking-at "[0-9]+")
503               (progn
504                 (setq end (read (current-buffer)))
505                 (set-buffer msg-buf)
506                 (widen)
507                 (let ((format (gnus-soup-encoding-format
508                                (gnus-soup-area-encoding (nth 1 area)))))
509                   (goto-char end)
510                   (when (or (= format ?n) (= format ?m))
511                     (setq end (progn (forward-line -1) (point))))))
512             (set-buffer msg-buf))
513           (widen)
514           (narrow-to-region beg (or end (point-max))))
515          (t
516           (set-buffer msg-buf)
517           (widen)
518           (goto-char (point-min))
519           (let ((header (nnsoup-header 
520                          (gnus-soup-encoding-format 
521                           (gnus-soup-area-encoding (nth 1 area))))))
522             (re-search-forward header nil t (- article (caar area)))
523             (narrow-to-region
524              (match-beginning 0)
525              (if (re-search-forward header nil t)
526                  (match-beginning 0)
527                (point-max))))))
528         (goto-char (point-min))
529         (if (not head)
530             ()
531           (narrow-to-region
532            (point-min)
533            (if (search-forward "\n\n" nil t)
534                (1- (point))
535              (point-max))))
536         msg-buf))))
537
538 (defun nnsoup-header (format)
539   (cond 
540    ((= format ?n)
541     "^#! *rnews +[0-9]+ *$")
542    ((= format ?m)
543     (concat "^" message-unix-mail-delimiter))
544    ((= format ?M)
545     "^\^A\^A\^A\^A\n")
546    (t
547     (error "Unknown format: %c" format))))
548
549 ;;;###autoload
550 (defun nnsoup-pack-replies ()
551   "Make an outbound package of SOUP replies."
552   (interactive)
553   (unless (file-exists-p nnsoup-replies-directory)
554     (message "No such directory: %s" nnsoup-replies-directory))
555   ;; Write all data buffers.
556   (gnus-soup-save-areas)
557   ;; Write the active file.
558   (nnsoup-write-active-file)
559   ;; Write the REPLIES file.
560   (nnsoup-write-replies)
561   ;; Check whether there is anything here.
562   (when (null (directory-files nnsoup-replies-directory nil "\\.MSG$"))
563     (error "No files to pack."))
564   ;; Pack all these files into a SOUP packet.
565   (gnus-soup-pack nnsoup-replies-directory nnsoup-packer))
566
567 (defun nnsoup-write-replies ()
568   "Write the REPLIES file."
569   (when nnsoup-replies-list
570     (gnus-soup-write-replies nnsoup-replies-directory nnsoup-replies-list)
571     (setq nnsoup-replies-list nil)))
572
573 (defun nnsoup-article-to-area (article group)
574   "Return the area that ARTICLE in GROUP is located in."
575   (let ((areas (cddr (assoc group nnsoup-group-alist))))
576     (while (and areas (< (cdaar areas) article))
577       (setq areas (cdr areas)))
578     (and areas (car areas))))
579
580 (defvar nnsoup-old-functions
581   (list message-send-mail-function message-send-news-function))
582
583 ;;;###autoload
584 (defun nnsoup-set-variables ()
585   "Use the SOUP methods for posting news and mailing mail."
586   (interactive)
587   (setq message-send-news-function 'nnsoup-request-post)
588   (setq message-send-mail-function 'nnsoup-request-mail))
589
590 ;;;###autoload
591 (defun nnsoup-revert-variables ()
592   "Revert posting and mailing methods to the standard Emacs methods."
593   (interactive)
594   (setq message-send-mail-function (car nnsoup-old-functions))
595   (setq message-send-news-function (cadr nnsoup-old-functions)))
596
597 (defun nnsoup-store-reply (kind)
598   ;; Mostly stolen from `message.el'.
599   (require 'mail-utils)
600   (let ((tembuf (generate-new-buffer " message temp"))
601         (case-fold-search nil)
602         delimline
603         (mailbuf (current-buffer)))
604     (unwind-protect
605         (save-excursion
606           (save-restriction
607             (message-narrow-to-headers)
608             (if (equal kind "mail")
609                 (message-generate-headers message-required-mail-headers)
610               (message-generate-headers message-required-news-headers)))
611           (set-buffer tembuf)
612           (erase-buffer)
613           (insert-buffer-substring mailbuf)
614           ;; Remove some headers.
615           (save-restriction
616             (message-narrow-to-headers)
617             ;; Remove some headers.
618             (message-remove-header message-ignored-mail-headers t))
619           (goto-char (point-max))
620           ;; require one newline at the end.
621           (or (= (preceding-char) ?\n)
622               (insert ?\n))
623           (let ((case-fold-search t))
624             ;; Change header-delimiter to be what sendmail expects.
625             (goto-char (point-min))
626             (re-search-forward
627              (concat "^" (regexp-quote mail-header-separator) "\n"))
628             (replace-match "\n")
629             (backward-char 1)
630             (setq delimline (point-marker))
631             ;; Insert an extra newline if we need it to work around
632             ;; Sun's bug that swallows newlines.
633             (goto-char (1+ delimline))
634             (when (eval message-mailer-swallows-blank-line)
635               (newline))
636             (let ((msg-buf
637                    (gnus-soup-store 
638                     nnsoup-replies-directory 
639                     (nnsoup-kind-to-prefix kind) nil nnsoup-replies-format-type
640                     nnsoup-replies-index-type))
641                   (num 0))
642               (when (and msg-buf (bufferp msg-buf))
643                 (save-excursion
644                   (set-buffer msg-buf)
645                   (goto-char (point-min))
646                   (while (re-search-forward "^#! *rnews" nil t)
647                     (incf num)))
648                 (message "Stored %d messages" num)))
649             (nnsoup-write-replies)
650             (kill-buffer tembuf))))))
651
652 (defun nnsoup-kind-to-prefix (kind)
653   (unless nnsoup-replies-list
654     (setq nnsoup-replies-list
655           (gnus-soup-parse-replies 
656            (concat nnsoup-replies-directory "REPLIES"))))
657   (let ((replies nnsoup-replies-list))
658     (while (and replies 
659                 (not (string= kind (gnus-soup-reply-kind (car replies)))))
660       (setq replies (cdr replies)))
661     (if replies
662         (gnus-soup-reply-prefix (car replies))
663       (push (vector (gnus-soup-unique-prefix nnsoup-replies-directory)
664                     kind 
665                     (format "%c%c%c"
666                             nnsoup-replies-format-type
667                             nnsoup-replies-index-type
668                             (if (string= kind "news")
669                                 ?n ?m)))
670             nnsoup-replies-list)
671       (gnus-soup-reply-prefix (car nnsoup-replies-list)))))
672
673 (defun nnsoup-make-active ()
674   "(Re-)create the SOUP active file."
675   (interactive)
676   (let ((files (sort (directory-files nnsoup-directory t "IDX$")
677                      (lambda (f1 f2)
678                        (< (progn (string-match "/\\([0-9]+\\)\\." f1)
679                                  (string-to-int (match-string 1 f1)))
680                           (progn (string-match "/\\([0-9]+\\)\\." f2)
681                                  (string-to-int (match-string 1 f2)))))))
682         active group lines ident elem min)
683     (set-buffer (get-buffer-create " *nnsoup work*"))
684     (buffer-disable-undo (current-buffer))
685     (while files
686       (message "Doing %s..." (car files))
687       (erase-buffer)
688       (nnheader-insert-file-contents (car files))
689       (goto-char (point-min))
690       (if (not (re-search-forward "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t *\\(Xref: \\)? *[^ ]* \\([^ ]+\\):[0-9]" nil t))
691           (setq group "unknown")
692         (setq group (match-string 2)))
693       (setq lines (count-lines (point-min) (point-max)))
694       (setq ident (progn (string-match
695                           "/\\([0-9]+\\)\\." (car files))
696                          (substring 
697                           (car files) (match-beginning 1)
698                           (match-end 1))))
699       (if (not (setq elem (assoc group active)))
700           (push (list group (cons 1 lines)
701                       (list (cons 1 lines)
702                             (vector ident group "ncm" "" lines)))
703                 active)
704         (nconc elem
705                (list
706                 (list (cons (1+ (setq min (cdadr elem)))
707                             (+ min lines))
708                       (vector ident group "ncm" "" lines))))
709         (setcdr (cadr elem) (+ min lines)))
710       (setq files (cdr files)))
711     (message "")
712     (setq nnsoup-group-alist active)
713     (nnsoup-write-active-file t)))
714
715 (defun nnsoup-delete-unreferenced-message-files ()
716   "Delete any *.MSG and *.IDX files that aren't known by nnsoup."
717   (interactive)
718   (let* ((known (apply 'nconc (mapcar 
719                                (lambda (ga)
720                                  (mapcar
721                                   (lambda (area)
722                                     (gnus-soup-area-prefix (cadr area)))
723                                   (cddr ga)))
724                                nnsoup-group-alist)))
725          (regexp "\\.MSG$\\|\\.IDX$")
726          (files (directory-files nnsoup-directory nil regexp))
727          non-files file)
728     ;; Find all files that aren't known by nnsoup.
729     (while (setq file (pop files))
730       (string-match regexp file)
731       (unless (member (substring file 0 (match-beginning 0)) known)
732         (push file non-files)))
733     ;; Sort and delete the files.
734     (setq non-files (sort non-files 'string<))
735     (map-y-or-n-p "Delete file %s? "
736                   (lambda (file) (delete-file (concat nnsoup-directory file)))
737                   non-files)))
738
739 (provide 'nnsoup)
740
741 ;;; nnsoup.el ends here