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