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