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