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