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