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