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