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