(gnus-group-expire-articles-1): Make sure server is open.
[gnus] / 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 (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
42     (cond ((fboundp 'temp-directory) (temp-directory))
43           ((boundp 'temporary-file-directory) temporary-file-directory)
44           ("/tmp/"))
45   "*Where nnsoup will store temporary files.")
46
47 (defvoo nnsoup-replies-directory (concat nnsoup-directory "replies/")
48   "*Directory where outgoing packets will be composed.")
49
50 (defvoo nnsoup-replies-format-type ?u  ;; u is USENET news format.
51   "*Format of the replies packages.")
52
53 (defvoo nnsoup-replies-index-type ?n
54   "*Index type of the replies packages.")
55
56 (defvoo nnsoup-active-file (concat nnsoup-directory "active")
57   "Active file.")
58
59 (defvoo nnsoup-packer "tar cf - %s | gzip > $HOME/Soupin%d.tgz"
60   "Format string command for packing a SOUP packet.
61 The SOUP files will be inserted where the %s is in the string.
62 This string MUST contain both %s and %d.  The file number will be
63 inserted where %d appears.")
64
65 (defvoo nnsoup-unpacker "gunzip -c %s | tar xvf -"
66   "*Format string command for unpacking a SOUP packet.
67 The SOUP packet file name will be inserted at the %s.")
68
69 (defvoo nnsoup-packet-directory "~/"
70   "*Where nnsoup will look for incoming packets.")
71
72 (defvoo nnsoup-packet-regexp "Soupout"
73   "*Regular expression matching SOUP packets in `nnsoup-packet-directory'.")
74
75 (defvoo nnsoup-always-save t
76   "If non nil commit the reply buffer on each message send.
77 This is necessary if using message mode outside Gnus with nnsoup as a
78 backend for the messages.")
79
80 \f
81
82 (defconst nnsoup-version "nnsoup 0.0"
83   "nnsoup version.")
84
85 (defvoo nnsoup-status-string "")
86 (defvoo nnsoup-group-alist nil)
87 (defvoo nnsoup-current-prefix 0)
88 (defvoo nnsoup-replies-list nil)
89 (defvoo nnsoup-buffers nil)
90 (defvoo nnsoup-current-group nil)
91 (defvoo nnsoup-group-alist-touched nil)
92 (defvoo nnsoup-article-alist nil)
93 \f
94
95 ;;; Interface functions.
96
97 (nnoo-define-basics nnsoup)
98
99 (deffoo nnsoup-retrieve-headers (sequence &optional group server fetch-old)
100   (nnsoup-possibly-change-group group)
101   (save-excursion
102     (set-buffer nntp-server-buffer)
103     (erase-buffer)
104     (let ((areas (cddr (assoc nnsoup-current-group nnsoup-group-alist)))
105           (articles sequence)
106           (use-nov t)
107           useful-areas this-area-seq msg-buf)
108       (if (stringp (car sequence))
109           ;; We don't support fetching by Message-ID.
110           'headers
111         ;; We go through all the areas and find which files the
112         ;; articles in SEQUENCE come from.
113         (while (and areas sequence)
114           ;; Peel off areas that are below sequence.
115           (while (and areas (< (cdaar areas) (car sequence)))
116             (setq areas (cdr areas)))
117           (when areas
118             ;; This is a useful area.
119             (push (car areas) useful-areas)
120             (setq this-area-seq nil)
121             ;; We take note whether this MSG has a corresponding IDX
122             ;; for later use.
123             (when (or (= (gnus-soup-encoding-index
124                           (gnus-soup-area-encoding (nth 1 (car areas)))) ?n)
125                       (not (file-exists-p
126                             (nnsoup-file
127                              (gnus-soup-area-prefix (nth 1 (car areas)))))))
128               (setq use-nov nil))
129             ;; We assign the portion of `sequence' that is relevant to
130             ;; this MSG packet to this packet.
131             (while (and sequence (<= (car sequence) (cdaar areas)))
132               (push (car sequence) this-area-seq)
133               (setq sequence (cdr sequence)))
134             (setcar useful-areas (cons (nreverse this-area-seq)
135                                        (car useful-areas)))))
136
137         ;; We now have a list of article numbers and corresponding
138         ;; areas.
139         (setq useful-areas (nreverse useful-areas))
140
141         ;; Two different approaches depending on whether all the MSG
142         ;; files have corresponding IDX files.  If they all do, we
143         ;; simply return the relevant IDX files and let Gnus sort out
144         ;; what lines are relevant.  If some of the IDX files are
145         ;; missing, we must return HEADs for all the articles.
146         (if use-nov
147             ;; We have IDX files for all areas.
148             (progn
149               (while useful-areas
150                 (goto-char (point-max))
151                 (let ((b (point))
152                       (number (car (nth 1 (car useful-areas))))
153                       (index-buffer (nnsoup-index-buffer
154                                      (gnus-soup-area-prefix
155                                       (nth 2 (car useful-areas))))))
156                   (when index-buffer
157                     (insert-buffer-substring index-buffer)
158                     (goto-char b)
159                     ;; We have to remove the index number entires and
160                     ;; insert article numbers instead.
161                     (while (looking-at "[0-9]+")
162                       (replace-match (int-to-string number) t t)
163                       (incf number)
164                       (forward-line 1))))
165                 (setq useful-areas (cdr useful-areas)))
166               'nov)
167           ;; We insert HEADs.
168           (while useful-areas
169             (setq articles (caar useful-areas)
170                   useful-areas (cdr useful-areas))
171             (while articles
172               (when (setq msg-buf
173                           (nnsoup-narrow-to-article
174                            (car articles) (cdar useful-areas) 'head))
175                 (goto-char (point-max))
176                 (insert (format "221 %d Article retrieved.\n" (car articles)))
177                 (insert-buffer-substring msg-buf)
178                 (goto-char (point-max))
179                 (insert ".\n"))
180               (setq articles (cdr articles))))
181
182           (nnheader-fold-continuation-lines)
183           'headers)))))
184
185 (deffoo nnsoup-open-server (server &optional defs)
186   (nnoo-change-server 'nnsoup server defs)
187   (when (not (file-exists-p nnsoup-directory))
188     (condition-case ()
189         (make-directory nnsoup-directory t)
190       (error t)))
191   (cond
192    ((not (file-exists-p nnsoup-directory))
193     (nnsoup-close-server)
194     (nnheader-report 'nnsoup "Couldn't create directory: %s" nnsoup-directory))
195    ((not (file-directory-p (file-truename nnsoup-directory)))
196     (nnsoup-close-server)
197     (nnheader-report 'nnsoup "Not a directory: %s" nnsoup-directory))
198    (t
199     (nnsoup-read-active-file)
200     (nnheader-report 'nnsoup "Opened server %s using directory %s"
201                      server nnsoup-directory)
202     t)))
203
204 (deffoo nnsoup-request-close ()
205   (nnsoup-write-active-file)
206   (nnsoup-write-replies)
207   (gnus-soup-save-areas)
208   ;; Kill all nnsoup buffers.
209   (let (buffer)
210     (while nnsoup-buffers
211       (setq buffer (cdr (pop nnsoup-buffers)))
212       (and buffer
213            (buffer-name buffer)
214            (kill-buffer buffer))))
215   (setq nnsoup-group-alist nil
216         nnsoup-group-alist-touched nil
217         nnsoup-current-group nil
218         nnsoup-replies-list nil)
219   (nnoo-close-server 'nnoo)
220   t)
221
222 (deffoo nnsoup-request-article (id &optional newsgroup server buffer)
223   (nnsoup-possibly-change-group newsgroup)
224   (let (buf)
225     (save-excursion
226       (set-buffer (or buffer nntp-server-buffer))
227       (erase-buffer)
228       (when (and (not (stringp id))
229                  (setq buf (nnsoup-narrow-to-article id)))
230         (insert-buffer-substring buf)
231         t))))
232
233 (deffoo nnsoup-request-group (group &optional server dont-check)
234   (nnsoup-possibly-change-group group)
235   (if dont-check
236       t
237     (let ((active (cadr (assoc group nnsoup-group-alist))))
238       (if (not active)
239           (nnheader-report 'nnsoup "No such group: %s" group)
240         (nnheader-insert
241          "211 %d %d %d %s\n"
242          (max (1+ (- (cdr active) (car active))) 0)
243          (car active) (cdr active) group)))))
244
245 (deffoo nnsoup-request-type (group &optional article)
246   (nnsoup-possibly-change-group group)
247   ;; Try to guess the type based on the first article in the group.
248   (when (not article)
249     (setq article
250           (cdaar (cddr (assoc group nnsoup-group-alist)))))
251   (if (not article)
252       'unknown
253     (let ((kind (gnus-soup-encoding-kind
254                  (gnus-soup-area-encoding
255                   (nth 1 (nnsoup-article-to-area
256                           article nnsoup-current-group))))))
257       (cond ((= kind ?m) 'mail)
258             ((= kind ?n) 'news) 
259             (t 'unknown)))))
260
261 (deffoo nnsoup-close-group (group &optional server)
262   ;; Kill all nnsoup buffers.
263   (let ((buffers nnsoup-buffers)
264         elem)
265     (while buffers
266       (when (equal (car (setq elem (pop buffers))) group)
267         (setq nnsoup-buffers (delq elem nnsoup-buffers))
268         (and (cdr elem) (buffer-name (cdr elem))
269              (kill-buffer (cdr elem))))))
270   t)
271
272 (deffoo nnsoup-request-list (&optional server)
273   (save-excursion
274     (set-buffer nntp-server-buffer)
275     (erase-buffer)
276     (unless nnsoup-group-alist
277       (nnsoup-read-active-file))
278     (let ((alist nnsoup-group-alist)
279           (standard-output (current-buffer))
280           entry)
281       (while (setq entry (pop alist))
282         (insert (car entry) " ")
283         (princ (cdadr entry))
284         (insert " ")
285         (princ (caadr entry))
286         (insert " y\n"))
287       t)))
288
289 (deffoo nnsoup-request-scan (group &optional server)
290   (nnsoup-unpack-packets))
291
292 (deffoo nnsoup-request-newgroups (date &optional server)
293   (nnsoup-request-list))
294
295 (deffoo nnsoup-request-list-newsgroups (&optional server)
296   nil)
297
298 (deffoo nnsoup-request-post (&optional server)
299   (nnsoup-store-reply "news")
300   t)
301
302 (deffoo nnsoup-request-mail (&optional server)
303   (nnsoup-store-reply "mail")
304   t)
305
306 (deffoo nnsoup-request-expire-articles (articles group &optional server force)
307   (nnsoup-possibly-change-group group)
308   (let* ((total-infolist (assoc group nnsoup-group-alist))
309          (active (cadr total-infolist))
310          (infolist (cddr total-infolist))
311          info range-list mod-time prefix)
312     (while infolist
313       (setq info (pop infolist)
314             range-list (gnus-uncompress-range (car info))
315             prefix (gnus-soup-area-prefix (nth 1 info)))
316       (when;; All the articles in this file are marked for expiry.
317           (and (or (setq mod-time (nth 5 (file-attributes
318                                           (nnsoup-file prefix))))
319                    (setq mod-time (nth 5 (file-attributes
320                                           (nnsoup-file prefix t)))))
321                (gnus-sublist-p articles range-list)
322                ;; This file is old enough.
323                (nnmail-expired-article-p group mod-time force))
324         ;; Ok, we delete this file.
325         (when (ignore-errors
326                 (nnheader-message
327                  5 "Deleting %s in group %s..." (nnsoup-file prefix)
328                  group)
329                 (when (file-exists-p (nnsoup-file prefix))
330                   (delete-file (nnsoup-file prefix)))
331                 (nnheader-message
332                  5 "Deleting %s in group %s..." (nnsoup-file prefix t)
333                  group)
334                 (when (file-exists-p (nnsoup-file prefix t))
335                   (delete-file (nnsoup-file prefix t)))
336                 t)
337           (setcdr (cdr total-infolist) (delq info (cddr total-infolist)))
338           (setq articles (gnus-sorted-complement articles range-list))))
339       (when (not mod-time)
340         (setcdr (cdr total-infolist) (delq info (cddr total-infolist)))))
341     (if (cddr total-infolist)
342         (setcar active (caaadr (cdr total-infolist)))
343       (setcar active (1+ (cdr active))))
344     (nnsoup-write-active-file t)
345     ;; Return the articles that weren't expired.
346     articles))
347
348 \f
349 ;;; Internal functions
350
351 (defun nnsoup-possibly-change-group (group &optional force)
352   (when (and group
353              (not (equal nnsoup-current-group group)))
354     (setq nnsoup-article-alist nil)
355     (setq nnsoup-current-group group))
356   t)
357
358 (defun nnsoup-read-active-file ()
359   (setq nnsoup-group-alist nil)
360   (when (file-exists-p nnsoup-active-file)
361     (ignore-errors
362       (load nnsoup-active-file t t t))
363     ;; Be backwards compatible.
364     (when (and nnsoup-group-alist
365                (not (atom (caadar nnsoup-group-alist))))
366       (let ((alist nnsoup-group-alist)
367             entry e min max)
368         (while (setq e (cdr (setq entry (pop alist))))
369           (setq min (caaar e))
370           (while (cdr e)
371             (setq e (cdr e)))
372           (setq max (cdaar e))
373           (setcdr entry (cons (cons min max) (cdr entry)))))
374       (setq nnsoup-group-alist-touched t))
375     nnsoup-group-alist))
376
377 (defun nnsoup-write-active-file (&optional force)
378   (when (and nnsoup-group-alist
379              (or force
380                  nnsoup-group-alist-touched))
381     (setq nnsoup-group-alist-touched nil)
382     (with-temp-file nnsoup-active-file
383       (gnus-prin1 `(setq nnsoup-group-alist ',nnsoup-group-alist))
384       (insert "\n")
385       (gnus-prin1 `(setq nnsoup-current-prefix ,nnsoup-current-prefix))
386       (insert "\n"))))
387
388 (defun nnsoup-next-prefix ()
389   "Return the next free prefix."
390   (let (prefix)
391     (while (or (file-exists-p
392                 (nnsoup-file (setq prefix (int-to-string
393                                            nnsoup-current-prefix))))
394                (file-exists-p (nnsoup-file prefix t)))
395       (incf nnsoup-current-prefix))
396     (incf nnsoup-current-prefix)
397     prefix))
398
399 (defun nnsoup-file-name (dir file)
400   "Return the full path of FILE (in any case) in DIR."
401   (let* ((case-fold-search t)
402          (files (directory-files dir t))
403          (regexp (concat (regexp-quote file) "$")))
404     (car (delq nil
405                (mapcar
406                 (lambda (file)
407                   (if (string-match regexp file)
408                       file
409                     nil))
410                 files)))))
411
412 (defun nnsoup-read-areas ()
413   (let ((areas-file (nnsoup-file-name nnsoup-tmp-directory "areas")))
414     (when areas-file
415       (save-excursion
416         (set-buffer nntp-server-buffer)
417         (let ((areas (gnus-soup-parse-areas areas-file))
418               entry number area lnum cur-prefix file)
419           ;; Go through all areas in the new AREAS file.
420           (while (setq area (pop areas))
421             ;; Change the name to the permanent name and move the files.
422             (setq cur-prefix (nnsoup-next-prefix))
423             (nnheader-message 5 "Incorporating file %s..." cur-prefix)
424             (when (file-exists-p
425                    (setq file (concat nnsoup-tmp-directory
426                                       (gnus-soup-area-prefix area) ".IDX")))
427               (rename-file file (nnsoup-file cur-prefix)))
428             (when (file-exists-p
429                    (setq file (concat nnsoup-tmp-directory
430                                       (gnus-soup-area-prefix area) ".MSG")))
431               (rename-file file (nnsoup-file cur-prefix t))
432               (gnus-soup-set-area-prefix area cur-prefix)
433               ;; Find the number of new articles in this area.
434               (setq number (nnsoup-number-of-articles area))
435               (if (not (setq entry (assoc (gnus-soup-area-name area)
436                                           nnsoup-group-alist)))
437                   ;; If this is a new area (group), we just add this info to
438                   ;; the group alist.
439                   (push (list (gnus-soup-area-name area)
440                               (cons 1 number)
441                               (list (cons 1 number) area))
442                         nnsoup-group-alist)
443                 ;; There are already articles in this group, so we add this
444                 ;; info to the end of the entry.
445                 (nconc entry (list (list (cons (1+ (setq lnum (cdadr entry)))
446                                                (+ lnum number))
447                                          area)))
448                 (setcdr (cadr entry) (+ lnum number))))))
449         (nnsoup-write-active-file t)
450         (delete-file areas-file)))))
451
452 (defun nnsoup-number-of-articles (area)
453   (save-excursion
454     (cond
455      ;; If the number is in the area info, we just return it.
456      ((gnus-soup-area-number area)
457       (gnus-soup-area-number area))
458      ;; If there is an index file, we just count the lines.
459      ((/= (gnus-soup-encoding-index (gnus-soup-area-encoding area)) ?n)
460       (set-buffer (nnsoup-index-buffer (gnus-soup-area-prefix area)))
461       (count-lines (point-min) (point-max)))
462      ;; We do it the hard way - re-searching through the message
463      ;; buffer.
464      (t
465       (set-buffer (nnsoup-message-buffer (gnus-soup-area-prefix area)))
466       (unless (assoc (gnus-soup-area-prefix area) nnsoup-article-alist)
467         (nnsoup-dissect-buffer area))
468       (length (cdr (assoc (gnus-soup-area-prefix area)
469                           nnsoup-article-alist)))))))
470
471 (defun nnsoup-dissect-buffer (area)
472   (let ((mbox-delim (concat "^" message-unix-mail-delimiter))
473         (format (gnus-soup-encoding-format (gnus-soup-area-encoding area)))
474         (i 0)
475         alist len)
476     (goto-char (point-min))
477     (cond
478      ;; rnews batch format
479      ((or (= format ?u)
480           (= format ?n)) ;; Gnus back compatibility.
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 ?u) (= 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 "ucm" "" lines)))
771                 active)
772         (nconc elem
773                (list
774                 (list (cons (1+ (setq min (cdadr elem)))
775                             (+ min lines))
776                       (vector ident group "ucm" "" 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