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