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