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