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