8fbe994046c525fe9838c9f8836aac61a6c21b60
[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 (concat nnsoup-directory "replies/")
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 (concat nnsoup-directory "active")
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 (concat nnsoup-tmp-directory
427                                       (gnus-soup-area-prefix area) ".IDX")))
428               (rename-file file (nnsoup-file cur-prefix)))
429             (when (file-exists-p
430                    (setq file (concat nnsoup-tmp-directory
431                                       (gnus-soup-area-prefix area) ".MSG")))
432               (rename-file file (nnsoup-file cur-prefix t))
433               (gnus-soup-set-area-prefix area cur-prefix)
434               ;; Find the number of new articles in this area.
435               (setq number (nnsoup-number-of-articles area))
436               (if (not (setq entry (assoc (gnus-soup-area-name area)
437                                           nnsoup-group-alist)))
438                   ;; If this is a new area (group), we just add this info to
439                   ;; the group alist.
440                   (push (list (gnus-soup-area-name area)
441                               (cons 1 number)
442                               (list (cons 1 number) area))
443                         nnsoup-group-alist)
444                 ;; There are already articles in this group, so we add this
445                 ;; info to the end of the entry.
446                 (nconc entry (list (list (cons (1+ (setq lnum (cdadr entry)))
447                                                (+ lnum number))
448                                          area)))
449                 (setcdr (cadr entry) (+ lnum number))))))
450         (nnsoup-write-active-file t)
451         (delete-file areas-file)))))
452
453 (defun nnsoup-number-of-articles (area)
454   (save-excursion
455     (cond
456      ;; If the number is in the area info, we just return it.
457      ((gnus-soup-area-number area)
458       (gnus-soup-area-number area))
459      ;; If there is an index file, we just count the lines.
460      ((/= (gnus-soup-encoding-index (gnus-soup-area-encoding area)) ?n)
461       (set-buffer (nnsoup-index-buffer (gnus-soup-area-prefix area)))
462       (count-lines (point-min) (point-max)))
463      ;; We do it the hard way - re-searching through the message
464      ;; buffer.
465      (t
466       (set-buffer (nnsoup-message-buffer (gnus-soup-area-prefix area)))
467       (unless (assoc (gnus-soup-area-prefix area) nnsoup-article-alist)
468         (nnsoup-dissect-buffer area))
469       (length (cdr (assoc (gnus-soup-area-prefix area)
470                           nnsoup-article-alist)))))))
471
472 (defun nnsoup-dissect-buffer (area)
473   (let ((mbox-delim (concat "^" message-unix-mail-delimiter))
474         (format (gnus-soup-encoding-format (gnus-soup-area-encoding area)))
475         (i 0)
476         alist len)
477     (goto-char (point-min))
478     (cond
479      ;; rnews batch format
480      ((or (= format ?u)
481           (= format ?n)) ;; Gnus back compatibility.
482       (while (looking-at "^#! *rnews \\(+[0-9]+\\) *$")
483         (forward-line 1)
484         (push (list
485                (incf i) (point)
486                (progn
487                  (forward-char (string-to-number (match-string 1)))
488                  (point)))
489               alist)))
490      ;; Unix mbox format
491      ((= format ?m)
492       (while (looking-at mbox-delim)
493         (forward-line 1)
494         (push (list
495                (incf i) (point)
496                (progn
497                  (if (re-search-forward mbox-delim nil t)
498                      (beginning-of-line)
499                    (goto-char (point-max)))
500                  (point)))
501               alist)))
502      ;; MMDF format
503      ((= format ?M)
504       (while (looking-at "\^A\^A\^A\^A\n")
505         (forward-line 1)
506         (push (list
507                (incf i) (point)
508                (progn
509                  (if (search-forward "\n\^A\^A\^A\^A\n" nil t)
510                      (beginning-of-line)
511                    (goto-char (point-max)))
512                  (point)))
513               alist)))
514      ;; Binary format
515      ((or (= format ?B) (= format ?b))
516       (while (not (eobp))
517         (setq len (+ (* (char-after (point)) (expt 2.0 24))
518                      (* (char-after (+ (point) 1)) (expt 2 16))
519                      (* (char-after (+ (point) 2)) (expt 2 8))
520                      (char-after (+ (point) 3))))
521         (push (list
522                (incf i) (+ (point) 4)
523                (progn
524                  (forward-char (floor (+ len 4)))
525                  (point)))
526               alist)))
527      (t
528       (error "Unknown format: %c" format)))
529     (push (cons (gnus-soup-area-prefix area) alist) nnsoup-article-alist)))
530
531 (defun nnsoup-index-buffer (prefix &optional message)
532   (let* ((file (concat prefix (if message ".MSG" ".IDX")))
533          (buffer-name (concat " *nnsoup " file "*")))
534     (or (get-buffer buffer-name)        ; File already loaded.
535         (when (file-exists-p (concat nnsoup-directory file))
536           (save-excursion               ; Load the file.
537             (set-buffer (get-buffer-create buffer-name))
538             (buffer-disable-undo)
539             (push (cons nnsoup-current-group (current-buffer)) nnsoup-buffers)
540             (nnheader-insert-file-contents (concat nnsoup-directory file))
541             (current-buffer))))))
542
543 (defun nnsoup-file (prefix &optional message)
544   (expand-file-name
545    (concat nnsoup-directory prefix (if message ".MSG" ".IDX"))))
546
547 (defun nnsoup-message-buffer (prefix)
548   (nnsoup-index-buffer prefix 'msg))
549
550 (defun nnsoup-unpack-packets ()
551   "Unpack all packets in `nnsoup-packet-directory'."
552   (let ((packets (directory-files
553                   nnsoup-packet-directory t nnsoup-packet-regexp))
554         packet)
555     (while (setq packet (pop packets))
556       (nnheader-message 5 "nnsoup: unpacking %s..." packet)
557       (if (not (gnus-soup-unpack-packet
558                 nnsoup-tmp-directory nnsoup-unpacker packet))
559           (nnheader-message 5 "Couldn't unpack %s" packet)
560         (delete-file packet)
561         (nnsoup-read-areas)
562         (nnheader-message 5 "Unpacking...done")))))
563
564 (defun nnsoup-narrow-to-article (article &optional area head)
565   (let* ((area (or area (nnsoup-article-to-area article nnsoup-current-group)))
566          (prefix (and area (gnus-soup-area-prefix (nth 1 area))))
567          (msg-buf (and prefix (nnsoup-index-buffer prefix 'msg)))
568          beg end)
569     (when area
570       (save-excursion
571         (cond
572          ;; There is no MSG file.
573          ((null msg-buf)
574           nil)
575          ;; We use the index file to find out where the article
576          ;; begins and ends.
577          ((and (= (gnus-soup-encoding-index
578                    (gnus-soup-area-encoding (nth 1 area)))
579                   ?c)
580                (file-exists-p (nnsoup-file prefix)))
581           (set-buffer (nnsoup-index-buffer prefix))
582           (widen)
583           (goto-char (point-min))
584           (forward-line (- article (caar area)))
585           (setq beg (read (current-buffer)))
586           (forward-line 1)
587           (if (looking-at "[0-9]+")
588               (progn
589                 (setq end (read (current-buffer)))
590                 (set-buffer msg-buf)
591                 (widen)
592                 (let ((format (gnus-soup-encoding-format
593                                (gnus-soup-area-encoding (nth 1 area)))))
594                   (goto-char end)
595                   (when (or (= format ?u) (= format ?n) (= format ?m))
596                     (setq end (progn (forward-line -1) (point))))))
597             (set-buffer msg-buf))
598           (widen)
599           (narrow-to-region beg (or end (point-max))))
600          (t
601           (set-buffer msg-buf)
602           (widen)
603           (unless (assoc (gnus-soup-area-prefix (nth 1 area))
604                          nnsoup-article-alist)
605             (nnsoup-dissect-buffer (nth 1 area)))
606           (let ((entry (assq article (cdr (assoc (gnus-soup-area-prefix
607                                                   (nth 1 area))
608                                                  nnsoup-article-alist)))))
609             (when entry
610               (narrow-to-region (cadr entry) (caddr entry))))))
611         (goto-char (point-min))
612         (if (not head)
613             ()
614           (narrow-to-region
615            (point-min)
616            (if (search-forward "\n\n" nil t)
617                (1- (point))
618              (point-max))))
619         msg-buf))))
620
621 ;;;###autoload
622 (defun nnsoup-pack-replies ()
623   "Make an outbound package of SOUP replies."
624   (interactive)
625   (unless (file-exists-p nnsoup-replies-directory)
626     (nnheader-message 5 "No such directory: %s" nnsoup-replies-directory))
627   ;; Write all data buffers.
628   (gnus-soup-save-areas)
629   ;; Write the active file.
630   (nnsoup-write-active-file)
631   ;; Write the REPLIES file.
632   (nnsoup-write-replies)
633   ;; Check whether there is anything here.
634   (when (null (directory-files nnsoup-replies-directory nil "\\.MSG$"))
635     (error "No files to pack"))
636   ;; Pack all these files into a SOUP packet.
637   (gnus-soup-pack nnsoup-replies-directory nnsoup-packer))
638
639 (defun nnsoup-write-replies ()
640   "Write the REPLIES file."
641   (when nnsoup-replies-list
642     (gnus-soup-write-replies nnsoup-replies-directory nnsoup-replies-list)
643     (setq nnsoup-replies-list nil)))
644
645 (defun nnsoup-article-to-area (article group)
646   "Return the area that ARTICLE in GROUP is located in."
647   (let ((areas (cddr (assoc group nnsoup-group-alist))))
648     (while (and areas (< (cdaar areas) article))
649       (setq areas (cdr areas)))
650     (and areas (car areas))))
651
652 (defvar nnsoup-old-functions
653   (list message-send-mail-function message-send-news-function))
654
655 ;;;###autoload
656 (defun nnsoup-set-variables ()
657   "Use the SOUP methods for posting news and mailing mail."
658   (interactive)
659   (setq message-send-news-function 'nnsoup-request-post)
660   (setq message-send-mail-function 'nnsoup-request-mail))
661
662 ;;;###autoload
663 (defun nnsoup-revert-variables ()
664   "Revert posting and mailing methods to the standard Emacs methods."
665   (interactive)
666   (setq message-send-mail-function (car nnsoup-old-functions))
667   (setq message-send-news-function (cadr nnsoup-old-functions)))
668
669 (defun nnsoup-store-reply (kind)
670   ;; Mostly stolen from `message.el'.
671   (require 'mail-utils)
672   (let ((tembuf (generate-new-buffer " message temp"))
673         (case-fold-search nil)
674         delimline
675         (mailbuf (current-buffer)))
676     (unwind-protect
677         (save-excursion
678           (save-restriction
679             (message-narrow-to-headers)
680             (if (equal kind "mail")
681                 (message-generate-headers message-required-mail-headers)
682               (message-generate-headers message-required-news-headers)))
683           (set-buffer tembuf)
684           (erase-buffer)
685           (insert-buffer-substring mailbuf)
686           ;; Remove some headers.
687           (save-restriction
688             (message-narrow-to-headers)
689             ;; Remove some headers.
690             (message-remove-header message-ignored-mail-headers t))
691           (goto-char (point-max))
692           ;; require one newline at the end.
693           (or (= (preceding-char) ?\n)
694               (insert ?\n))
695           (let ((case-fold-search t))
696             ;; Change header-delimiter to be what sendmail expects.
697             (goto-char (point-min))
698             (re-search-forward
699              (concat "^" (regexp-quote mail-header-separator) "\n"))
700             (replace-match "\n")
701             (backward-char 1)
702             (setq delimline (point-marker))
703             (goto-char (1+ delimline))
704             (let ((msg-buf
705                    (gnus-soup-store
706                     nnsoup-replies-directory
707                     (nnsoup-kind-to-prefix kind) nil nnsoup-replies-format-type
708                     nnsoup-replies-index-type))
709                   (num 0))
710               (when (and msg-buf (bufferp msg-buf))
711                 (save-excursion
712                   (set-buffer msg-buf)
713                   (goto-char (point-min))
714                   (while (re-search-forward "^#! *rnews" nil t)
715                     (incf num))
716                   (when nnsoup-always-save
717                     (save-buffer)))
718                 (nnheader-message 5 "Stored %d messages" num)))
719             (nnsoup-write-replies)
720             (kill-buffer tembuf))))))
721
722 (defun nnsoup-kind-to-prefix (kind)
723   (unless nnsoup-replies-list
724     (setq nnsoup-replies-list
725           (gnus-soup-parse-replies
726            (concat nnsoup-replies-directory "REPLIES"))))
727   (let ((replies nnsoup-replies-list))
728     (while (and replies
729                 (not (string= kind (gnus-soup-reply-kind (car replies)))))
730       (setq replies (cdr replies)))
731     (if replies
732         (gnus-soup-reply-prefix (car replies))
733       (push (vector (gnus-soup-unique-prefix nnsoup-replies-directory)
734                     kind
735                     (format "%c%c%c"
736                             nnsoup-replies-format-type
737                             nnsoup-replies-index-type
738                             (if (string= kind "news")
739                                 ?n ?m)))
740             nnsoup-replies-list)
741       (gnus-soup-reply-prefix (car nnsoup-replies-list)))))
742
743 (defun nnsoup-make-active ()
744   "(Re-)create the SOUP active file."
745   (interactive)
746   (let ((files (sort (directory-files nnsoup-directory t "IDX$")
747                      (lambda (f1 f2)
748                        (< (progn (string-match "/\\([0-9]+\\)\\." f1)
749                                  (string-to-int (match-string 1 f1)))
750                           (progn (string-match "/\\([0-9]+\\)\\." f2)
751                                  (string-to-int (match-string 1 f2)))))))
752         active group lines ident elem min)
753     (set-buffer (get-buffer-create " *nnsoup work*"))
754     (while files
755       (nnheader-message 5 "Doing %s..." (car files))
756       (erase-buffer)
757       (nnheader-insert-file-contents (car files))
758       (goto-char (point-min))
759       (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))
760           (setq group "unknown")
761         (setq group (match-string 2)))
762       (setq lines (count-lines (point-min) (point-max)))
763       (setq ident (progn (string-match
764                           "/\\([0-9]+\\)\\." (car files))
765                          (substring
766                           (car files) (match-beginning 1)
767                           (match-end 1))))
768       (if (not (setq elem (assoc group active)))
769           (push (list group (cons 1 lines)
770                       (list (cons 1 lines)
771                             (vector ident group "ucm" "" lines)))
772                 active)
773         (nconc elem
774                (list
775                 (list (cons (1+ (setq min (cdadr elem)))
776                             (+ min lines))
777                       (vector ident group "ucm" "" lines))))
778         (setcdr (cadr elem) (+ min lines)))
779       (setq files (cdr files)))
780     (nnheader-message 5 "")
781     (setq nnsoup-group-alist active)
782     (nnsoup-write-active-file t)))
783
784 (defun nnsoup-delete-unreferenced-message-files ()
785   "Delete any *.MSG and *.IDX files that aren't known by nnsoup."
786   (interactive)
787   (let* ((known (apply 'nconc (mapcar
788                                (lambda (ga)
789                                  (mapcar
790                                   (lambda (area)
791                                     (gnus-soup-area-prefix (cadr area)))
792                                   (cddr ga)))
793                                nnsoup-group-alist)))
794          (regexp "\\.MSG$\\|\\.IDX$")
795          (files (directory-files nnsoup-directory nil regexp))
796          non-files file)
797     ;; Find all files that aren't known by nnsoup.
798     (while (setq file (pop files))
799       (string-match regexp file)
800       (unless (member (substring file 0 (match-beginning 0)) known)
801         (push file non-files)))
802     ;; Sort and delete the files.
803     (setq non-files (sort non-files 'string<))
804     (map-y-or-n-p "Delete file %s? "
805                   (lambda (file) (delete-file (concat nnsoup-directory file)))
806                   non-files)))
807
808 (provide 'nnsoup)
809
810 ;;; nnsoup.el ends here