*** 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   ;; Try to guess the type based on the first articl ein the group.
240   (when (not article)
241     (setq article
242           (cdaar (cddr (assoc group nnsoup-group-alist)))))
243   (if (not article)
244       'unknown
245     (let ((kind (gnus-soup-encoding-kind 
246                  (gnus-soup-area-encoding
247                   (nth 1 (nnsoup-article-to-area
248                           article nnsoup-current-group))))))
249       (cond ((= kind ?m) 'mail)
250             ((= kind ?n) 'news)
251             (t 'unknown)))))
252
253 (deffoo nnsoup-close-group (group &optional server)
254   ;; Kill all nnsoup buffers.
255   (let ((buffers nnsoup-buffers)
256         elem)
257     (while buffers
258       (when (equal (car (setq elem (pop buffers))) group)
259         (setq nnsoup-buffers (delq elem nnsoup-buffers))
260         (and (cdr elem) (buffer-name (cdr elem))
261              (kill-buffer (cdr elem))))))
262   t)
263
264 (deffoo nnsoup-request-list (&optional server)
265   (save-excursion
266     (set-buffer nntp-server-buffer)
267     (erase-buffer)
268     (unless nnsoup-group-alist
269       (nnsoup-read-active-file))
270     (let ((alist nnsoup-group-alist)
271           (standard-output (current-buffer))
272           entry)
273       (while (setq entry (pop alist))
274         (insert (car entry) " ")
275         (princ (cdadr entry))
276         (insert " ")
277         (princ (caadr entry))
278         (insert " y\n"))
279       t)))
280
281 (deffoo nnsoup-request-scan (group &optional server)
282   (nnsoup-unpack-packets))
283
284 (deffoo nnsoup-request-newgroups (date &optional server)
285   (nnsoup-request-list))
286
287 (deffoo nnsoup-request-list-newsgroups (&optional server)
288   nil)
289
290 (deffoo nnsoup-request-post (&optional server)
291   (nnsoup-store-reply "news")
292   t)
293
294 (deffoo nnsoup-request-mail (&optional server)
295   (nnsoup-store-reply "mail")
296   t)
297
298 (deffoo nnsoup-request-expire-articles (articles group &optional server force)
299   (nnsoup-possibly-change-group group)
300   (let* ((total-infolist (assoc group nnsoup-group-alist))
301          (active (cadr total-infolist))
302          (infolist (cddr total-infolist))
303          info range-list mod-time prefix)
304     (while infolist
305       (setq info (pop infolist)
306             range-list (gnus-uncompress-range (car info))
307             prefix (gnus-soup-area-prefix (nth 1 info)))
308       (when ;; All the articles in this file are marked for expiry.
309           (and (or (setq mod-time (nth 5 (file-attributes
310                                           (nnsoup-file prefix))))
311                    (setq mod-time (nth 5 (file-attributes
312                                           (nnsoup-file prefix t)))))
313                (gnus-sublist-p articles range-list)
314                ;; This file is old enough. 
315                (nnmail-expired-article-p group mod-time force))
316         ;; Ok, we delete this file.
317         (when (ignore-errors
318                 (nnheader-message 
319                  5 "Deleting %s in group %s..." (nnsoup-file prefix)
320                  group)
321                 (when (file-exists-p (nnsoup-file prefix))
322                   (delete-file (nnsoup-file prefix)))
323                 (nnheader-message 
324                  5 "Deleting %s in group %s..." (nnsoup-file prefix t)
325                  group)
326                 (when (file-exists-p (nnsoup-file prefix t))
327                   (delete-file (nnsoup-file prefix t)))
328                 t)
329           (setcdr (cdr total-infolist) (delq info (cddr total-infolist)))
330           (setq articles (gnus-sorted-complement articles range-list))))
331       (when (not mod-time)
332         (setcdr (cdr total-infolist) (delq info (cddr total-infolist)))))
333     (if (cddr total-infolist)
334         (setcar active (caaadr (cdr total-infolist)))
335       (setcar active (1+ (cdr active))))
336     (nnsoup-write-active-file t)
337     ;; Return the articles that weren't expired.
338     articles))
339
340 \f
341 ;;; Internal functions
342
343 (defun nnsoup-possibly-change-group (group &optional force)
344   (if group
345       (setq nnsoup-current-group group)
346     t))
347
348 (defun nnsoup-read-active-file ()
349   (setq nnsoup-group-alist nil)
350   (when (file-exists-p nnsoup-active-file)
351     (ignore-errors
352       (load nnsoup-active-file t t t))
353     ;; Be backwards compatible.
354     (when (and nnsoup-group-alist
355                (not (atom (caadar nnsoup-group-alist))))
356       (let ((alist nnsoup-group-alist)
357             entry e min max)
358         (while (setq e (cdr (setq entry (pop alist))))
359           (setq min (caaar e))
360           (while (cdr e)
361             (setq e (cdr e)))
362           (setq max (cdaar e))
363           (setcdr entry (cons (cons min max) (cdr entry)))))
364       (setq nnsoup-group-alist-touched t))
365     nnsoup-group-alist))
366
367 (defun nnsoup-write-active-file (&optional force)
368   (when (and nnsoup-group-alist
369              (or force 
370                  nnsoup-group-alist-touched))
371     (setq nnsoup-group-alist-touched nil)
372     (nnheader-temp-write nnsoup-active-file
373       (gnus-prin1 `(setq nnsoup-group-alist ',nnsoup-group-alist))
374       (insert "\n")
375       (gnus-prin1 `(setq nnsoup-current-prefix ,nnsoup-current-prefix))
376       (insert "\n"))))
377
378 (defun nnsoup-next-prefix ()
379   "Return the next free prefix."
380   (let (prefix)
381     (while (or (file-exists-p 
382                 (nnsoup-file (setq prefix (int-to-string
383                                            nnsoup-current-prefix))))
384                (file-exists-p (nnsoup-file prefix t)))
385       (incf nnsoup-current-prefix))
386     (incf nnsoup-current-prefix)
387     prefix))
388
389 (defun nnsoup-read-areas ()
390   (when (file-exists-p (concat nnsoup-tmp-directory "AREAS"))
391     (save-excursion
392       (set-buffer nntp-server-buffer)
393       (let ((areas (gnus-soup-parse-areas 
394                     (concat nnsoup-tmp-directory "AREAS")))
395             entry number area lnum cur-prefix file)
396         ;; Go through all areas in the new AREAS file.
397         (while (setq area (pop areas))
398           ;; Change the name to the permanent name and move the files.
399           (setq cur-prefix (nnsoup-next-prefix))
400           (message "Incorporating file %s..." cur-prefix)
401           (when (file-exists-p 
402                  (setq file (concat nnsoup-tmp-directory
403                                     (gnus-soup-area-prefix area) ".IDX")))
404             (rename-file file (nnsoup-file cur-prefix)))
405           (when (file-exists-p 
406                  (setq file (concat nnsoup-tmp-directory 
407                                     (gnus-soup-area-prefix area) ".MSG")))
408             (rename-file file (nnsoup-file cur-prefix t))
409             (gnus-soup-set-area-prefix area cur-prefix)
410             ;; Find the number of new articles in this area.
411             (setq number (nnsoup-number-of-articles area))
412             (if (not (setq entry (assoc (gnus-soup-area-name area)
413                                         nnsoup-group-alist)))
414                 ;; If this is a new area (group), we just add this info to
415                 ;; the group alist. 
416                 (push (list (gnus-soup-area-name area)
417                             (cons 1 number)
418                             (list (cons 1 number) area))
419                       nnsoup-group-alist)
420               ;; There are already articles in this group, so we add this
421               ;; info to the end of the entry.
422               (nconc entry (list (list (cons (1+ (setq lnum (cdadr entry)))
423                                              (+ lnum number))
424                                        area)))
425               (setcdr (cadr entry) (+ lnum number))))))
426       (nnsoup-write-active-file t)
427       (delete-file (concat nnsoup-tmp-directory "AREAS")))))
428
429 (defun nnsoup-number-of-articles (area)
430   (save-excursion
431     (cond 
432      ;; If the number is in the area info, we just return it.
433      ((gnus-soup-area-number area)
434       (gnus-soup-area-number area))
435      ;; If there is an index file, we just count the lines.
436      ((/= (gnus-soup-encoding-index (gnus-soup-area-encoding area)) ?n)
437       (set-buffer (nnsoup-index-buffer (gnus-soup-area-prefix area)))
438       (count-lines (point-min) (point-max)))
439      ;; We do it the hard way - re-searching through the message
440      ;; buffer. 
441      (t
442       (set-buffer (nnsoup-message-buffer (gnus-soup-area-prefix area)))
443       (goto-char (point-min))
444       (let ((regexp (nnsoup-header (gnus-soup-encoding-format 
445                                     (gnus-soup-area-encoding area))))
446             (num 0))
447         (while (re-search-forward regexp nil t)
448           (setq num (1+ num)))
449         num)))))
450
451 (defun nnsoup-index-buffer (prefix &optional message)
452   (let* ((file (concat prefix (if message ".MSG" ".IDX")))
453          (buffer-name (concat " *nnsoup " file "*")))
454     (or (get-buffer buffer-name)        ; File already loaded.
455         (when (file-exists-p (concat nnsoup-directory file))
456           (save-excursion               ; Load the file.
457             (set-buffer (get-buffer-create buffer-name))
458             (buffer-disable-undo (current-buffer))
459             (push (cons nnsoup-current-group (current-buffer)) nnsoup-buffers)
460             (nnheader-insert-file-contents (concat nnsoup-directory file))
461             (current-buffer))))))
462
463 (defun nnsoup-file (prefix &optional message)
464   (expand-file-name
465    (concat nnsoup-directory prefix (if message ".MSG" ".IDX"))))
466
467 (defun nnsoup-message-buffer (prefix)
468   (nnsoup-index-buffer prefix 'msg))
469
470 (defun nnsoup-unpack-packets ()
471   "Unpack all packets in `nnsoup-packet-directory'."
472   (let ((packets (directory-files
473                   nnsoup-packet-directory t nnsoup-packet-regexp))
474         packet)
475     (while (setq packet (pop packets))
476       (message "nnsoup: unpacking %s..." packet)
477       (if (not (gnus-soup-unpack-packet 
478                 nnsoup-tmp-directory nnsoup-unpacker packet))
479           (message "Couldn't unpack %s" packet)
480         (delete-file packet)
481         (nnsoup-read-areas)
482         (message "Unpacking...done")))))
483
484 (defun nnsoup-narrow-to-article (article &optional area head)
485   (let* ((area (or area (nnsoup-article-to-area article nnsoup-current-group)))
486          (prefix (and area (gnus-soup-area-prefix (nth 1 area))))
487          (msg-buf (and prefix (nnsoup-index-buffer prefix 'msg)))
488          beg end)
489     (when area
490       (save-excursion
491         (cond
492          ;; There is no MSG file.
493          ((null msg-buf)
494           nil)
495          ;; We use the index file to find out where the article 
496          ;; begins and ends. 
497          ((and (= (gnus-soup-encoding-index 
498                    (gnus-soup-area-encoding (nth 1 area)))
499                   ?c)
500                (file-exists-p (nnsoup-file prefix)))
501           (set-buffer (nnsoup-index-buffer prefix))
502           (widen)
503           (goto-char (point-min))
504           (forward-line (- article (caar area)))
505           (setq beg (read (current-buffer)))
506           (forward-line 1)
507           (if (looking-at "[0-9]+")
508               (progn
509                 (setq end (read (current-buffer)))
510                 (set-buffer msg-buf)
511                 (widen)
512                 (let ((format (gnus-soup-encoding-format
513                                (gnus-soup-area-encoding (nth 1 area)))))
514                   (goto-char end)
515                   (when (or (= format ?n) (= format ?m))
516                     (setq end (progn (forward-line -1) (point))))))
517             (set-buffer msg-buf))
518           (widen)
519           (narrow-to-region beg (or end (point-max))))
520          (t
521           (set-buffer msg-buf)
522           (widen)
523           (goto-char (point-min))
524           (let ((header (nnsoup-header 
525                          (gnus-soup-encoding-format 
526                           (gnus-soup-area-encoding (nth 1 area))))))
527             (re-search-forward header nil t (- article (caar area)))
528             (narrow-to-region
529              (match-beginning 0)
530              (if (re-search-forward header nil t)
531                  (match-beginning 0)
532                (point-max))))))
533         (goto-char (point-min))
534         (if (not head)
535             ()
536           (narrow-to-region
537            (point-min)
538            (if (search-forward "\n\n" nil t)
539                (1- (point))
540              (point-max))))
541         msg-buf))))
542
543 (defun nnsoup-header (format)
544   (cond 
545    ((= format ?n)
546     "^#! *rnews +[0-9]+ *$")
547    ((= format ?m)
548     (concat "^" message-unix-mail-delimiter))
549    ((= format ?M)
550     "^\^A\^A\^A\^A\n")
551    (t
552     (error "Unknown format: %c" format))))
553
554 ;;;###autoload
555 (defun nnsoup-pack-replies ()
556   "Make an outbound package of SOUP replies."
557   (interactive)
558   (unless (file-exists-p nnsoup-replies-directory)
559     (message "No such directory: %s" nnsoup-replies-directory))
560   ;; Write all data buffers.
561   (gnus-soup-save-areas)
562   ;; Write the active file.
563   (nnsoup-write-active-file)
564   ;; Write the REPLIES file.
565   (nnsoup-write-replies)
566   ;; Check whether there is anything here.
567   (when (null (directory-files nnsoup-replies-directory nil "\\.MSG$"))
568     (error "No files to pack."))
569   ;; Pack all these files into a SOUP packet.
570   (gnus-soup-pack nnsoup-replies-directory nnsoup-packer))
571
572 (defun nnsoup-write-replies ()
573   "Write the REPLIES file."
574   (when nnsoup-replies-list
575     (gnus-soup-write-replies nnsoup-replies-directory nnsoup-replies-list)
576     (setq nnsoup-replies-list nil)))
577
578 (defun nnsoup-article-to-area (article group)
579   "Return the area that ARTICLE in GROUP is located in."
580   (let ((areas (cddr (assoc group nnsoup-group-alist))))
581     (while (and areas (< (cdaar areas) article))
582       (setq areas (cdr areas)))
583     (and areas (car areas))))
584
585 (defvar nnsoup-old-functions
586   (list message-send-mail-function message-send-news-function))
587
588 ;;;###autoload
589 (defun nnsoup-set-variables ()
590   "Use the SOUP methods for posting news and mailing mail."
591   (interactive)
592   (setq message-send-news-function 'nnsoup-request-post)
593   (setq message-send-mail-function 'nnsoup-request-mail))
594
595 ;;;###autoload
596 (defun nnsoup-revert-variables ()
597   "Revert posting and mailing methods to the standard Emacs methods."
598   (interactive)
599   (setq message-send-mail-function (car nnsoup-old-functions))
600   (setq message-send-news-function (cadr nnsoup-old-functions)))
601
602 (defun nnsoup-store-reply (kind)
603   ;; Mostly stolen from `message.el'.
604   (require 'mail-utils)
605   (let ((tembuf (generate-new-buffer " message temp"))
606         (case-fold-search nil)
607         delimline
608         (mailbuf (current-buffer)))
609     (unwind-protect
610         (save-excursion
611           (save-restriction
612             (message-narrow-to-headers)
613             (if (equal kind "mail")
614                 (message-generate-headers message-required-mail-headers)
615               (message-generate-headers message-required-news-headers)))
616           (set-buffer tembuf)
617           (erase-buffer)
618           (insert-buffer-substring mailbuf)
619           ;; Remove some headers.
620           (save-restriction
621             (message-narrow-to-headers)
622             ;; Remove some headers.
623             (message-remove-header message-ignored-mail-headers t))
624           (goto-char (point-max))
625           ;; require one newline at the end.
626           (or (= (preceding-char) ?\n)
627               (insert ?\n))
628           (let ((case-fold-search t))
629             ;; Change header-delimiter to be what sendmail expects.
630             (goto-char (point-min))
631             (re-search-forward
632              (concat "^" (regexp-quote mail-header-separator) "\n"))
633             (replace-match "\n")
634             (backward-char 1)
635             (setq delimline (point-marker))
636             ;; Insert an extra newline if we need it to work around
637             ;; Sun's bug that swallows newlines.
638             (goto-char (1+ delimline))
639             (when (eval message-mailer-swallows-blank-line)
640               (newline))
641             (let ((msg-buf
642                    (gnus-soup-store 
643                     nnsoup-replies-directory 
644                     (nnsoup-kind-to-prefix kind) nil nnsoup-replies-format-type
645                     nnsoup-replies-index-type))
646                   (num 0))
647               (when (and msg-buf (bufferp msg-buf))
648                 (save-excursion
649                   (set-buffer msg-buf)
650                   (goto-char (point-min))
651                   (while (re-search-forward "^#! *rnews" nil t)
652                     (incf num)))
653                 (message "Stored %d messages" num)))
654             (nnsoup-write-replies)
655             (kill-buffer tembuf))))))
656
657 (defun nnsoup-kind-to-prefix (kind)
658   (unless nnsoup-replies-list
659     (setq nnsoup-replies-list
660           (gnus-soup-parse-replies 
661            (concat nnsoup-replies-directory "REPLIES"))))
662   (let ((replies nnsoup-replies-list))
663     (while (and replies 
664                 (not (string= kind (gnus-soup-reply-kind (car replies)))))
665       (setq replies (cdr replies)))
666     (if replies
667         (gnus-soup-reply-prefix (car replies))
668       (push (vector (gnus-soup-unique-prefix nnsoup-replies-directory)
669                     kind 
670                     (format "%c%c%c"
671                             nnsoup-replies-format-type
672                             nnsoup-replies-index-type
673                             (if (string= kind "news")
674                                 ?n ?m)))
675             nnsoup-replies-list)
676       (gnus-soup-reply-prefix (car nnsoup-replies-list)))))
677
678 (defun nnsoup-make-active ()
679   "(Re-)create the SOUP active file."
680   (interactive)
681   (let ((files (sort (directory-files nnsoup-directory t "IDX$")
682                      (lambda (f1 f2)
683                        (< (progn (string-match "/\\([0-9]+\\)\\." f1)
684                                  (string-to-int (match-string 1 f1)))
685                           (progn (string-match "/\\([0-9]+\\)\\." f2)
686                                  (string-to-int (match-string 1 f2)))))))
687         active group lines ident elem min)
688     (set-buffer (get-buffer-create " *nnsoup work*"))
689     (buffer-disable-undo (current-buffer))
690     (while files
691       (message "Doing %s..." (car files))
692       (erase-buffer)
693       (nnheader-insert-file-contents (car files))
694       (goto-char (point-min))
695       (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))
696           (setq group "unknown")
697         (setq group (match-string 2)))
698       (setq lines (count-lines (point-min) (point-max)))
699       (setq ident (progn (string-match
700                           "/\\([0-9]+\\)\\." (car files))
701                          (substring 
702                           (car files) (match-beginning 1)
703                           (match-end 1))))
704       (if (not (setq elem (assoc group active)))
705           (push (list group (cons 1 lines)
706                       (list (cons 1 lines)
707                             (vector ident group "ncm" "" lines)))
708                 active)
709         (nconc elem
710                (list
711                 (list (cons (1+ (setq min (cdadr elem)))
712                             (+ min lines))
713                       (vector ident group "ncm" "" lines))))
714         (setcdr (cadr elem) (+ min lines)))
715       (setq files (cdr files)))
716     (message "")
717     (setq nnsoup-group-alist active)
718     (nnsoup-write-active-file t)))
719
720 (defun nnsoup-delete-unreferenced-message-files ()
721   "Delete any *.MSG and *.IDX files that aren't known by nnsoup."
722   (interactive)
723   (let* ((known (apply 'nconc (mapcar 
724                                (lambda (ga)
725                                  (mapcar
726                                   (lambda (area)
727                                     (gnus-soup-area-prefix (cadr area)))
728                                   (cddr ga)))
729                                nnsoup-group-alist)))
730          (regexp "\\.MSG$\\|\\.IDX$")
731          (files (directory-files nnsoup-directory nil regexp))
732          non-files file)
733     ;; Find all files that aren't known by nnsoup.
734     (while (setq file (pop files))
735       (string-match regexp file)
736       (unless (member (substring file 0 (match-beginning 0)) known)
737         (push file non-files)))
738     ;; Sort and delete the files.
739     (setq non-files (sort non-files 'string<))
740     (map-y-or-n-p "Delete file %s? "
741                   (lambda (file) (delete-file (concat nnsoup-directory file)))
742                   non-files)))
743
744 (provide 'nnsoup)
745
746 ;;; nnsoup.el ends here