bf7dfdc92fcdff1f45c6c5df650289961f528c97
[gnus] / lisp / nnsoup.el
1 ;;; nnsoup.el --- SOUP access for Gnus
2 ;; Copyright (C) 1995 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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'nnheader)
29 (require 'nnmail)
30 (require 'gnus-soup)
31 (require 'gnus-msg)
32
33 (defvar nnsoup-directory "~/SOUP/"
34   "*SOUP packet directory directory.")
35
36 (defvar nnsoup-replies-directory (concat nnsoup-directory "replies/")
37   "*Directory where outgoing packets will be composed.")
38
39 (defvar nnsoup-replies-format-type ?n
40   "*Format of the replies packages.")
41
42 (defvar nnsoup-replies-index-type ?n
43   "*Index type of the replies packages.")
44
45 (defvar nnsoup-active-file (concat nnsoup-directory "active")
46   "Active file.")
47
48 (defvar nnsoup-packer "tar cf - %s | gzip > $HOME/Soupin%d.tgz"
49   "Format string command for packing a SOUP packet.
50 The SOUP files will be inserted where the %s is in the string.
51 This string MUST contain both %s and %d. The file number will be
52 inserted where %d appears.")
53
54 (defvar nnsoup-unpacker "gunzip -c %s | tar xvf -"
55   "*Format string command for unpacking a SOUP packet.
56 The SOUP packet file name will be inserted at the %s.")
57
58 (defvar nnsoup-packet-directory "~/"
59   "*Where nnsoup will look for incoming packets.")
60
61 (defvar nnsoup-packet-regexp "Soupout"
62   "*Regular expression matching SOUP packets in `nnsoup-packet-directory'.")
63
64 \f
65
66 (defconst nnsoup-version "nnsoup 0.0"
67   "nnsoup version.")
68
69 (defvar nnsoup-status-string "")
70 (defvar nnsoup-group-alist nil)
71 (defvar nnsoup-replies-list nil)
72 (defvar nnsoup-buffers nil)
73 (defvar nnsoup-current-group nil)
74
75 \f
76
77 ;; Server variables.
78
79 (defvar nnsoup-current-server nil)
80 (defvar nnsoup-server-alist nil)
81 (defvar nnsoup-server-variables 
82   (list 
83    (list 'nnsoup-directory nnsoup-directory)
84    (list 'nnsoup-active-file nnsoup-active-file)
85    '(nnsoup-status-string "")
86    '(nnsoup-group-alist nil)))
87
88 \f
89
90 ;;; Interface functions.
91
92 (defun nnsoup-retrieve-headers (sequence &optional group server fetch-old)
93   (nnsoup-possibly-change-group group)
94   (save-excursion
95     (set-buffer nntp-server-buffer)
96     (erase-buffer)
97     (let ((areas (cdr (assoc nnsoup-current-group nnsoup-group-alist)))
98           (articles sequence)
99           (use-nov t)
100           useful-areas this-area-seq)
101       (if (stringp (car sequence))
102           'headers
103         ;; We go through all the areas and find which files the
104         ;; articles in SEQUENCE come from.
105         (while (and areas sequence)
106           ;; Peel off areas that are below sequence.
107           (while (and areas (< (cdr (car (car areas))) (car sequence)))
108             (setq areas (cdr areas)))
109           (if (not areas)
110               ()
111             ;; This is a useful area.
112             (setq useful-areas (cons (car areas) useful-areas)
113                   this-area-seq nil)
114             ;; We take note whether this MSG has a corresponding IDX
115             ;; for later use.
116             (if (or (= (gnus-soup-encoding-index 
117                         (gnus-soup-area-encoding (nth 1 (car areas)))) ?n)
118                     (not (file-exists-p
119                           (nnsoup-file
120                            (gnus-soup-area-prefix (nth 1 (car areas)))))))
121                 (setq use-nov nil))
122             ;; We assing the portion of `sequence' that is relevant to
123             ;; this MSG packet to this packet.
124             (while (and sequence (<= (car sequence) (cdr (car (car areas)))))
125               (setq this-area-seq (cons (car sequence) this-area-seq)
126                     sequence (cdr sequence)))
127             (setcar useful-areas (cons (nreverse this-area-seq)
128                                        (car useful-areas)))))
129
130         ;; We now have a list of article numbers and corresponding
131         ;; areas. 
132         (setq useful-areas (nreverse useful-areas))
133
134         ;; Two different approaches depending on whether all the MSG
135         ;; files have corresponding IDX files.  If they all do, we
136         ;; simply return the relevant IDX files and let Gnus sort out
137         ;; what lines are relevant.  If some of the IDX files are
138         ;; missing, we must return HEADs for all the articles.
139         (if use-nov
140             (while useful-areas
141               (goto-char (point-max))
142               (let ((b (point))
143                     (number (car (nth 1 (car useful-areas)))))
144                 (insert-buffer-substring
145                  (nnsoup-index-buffer
146                   (gnus-soup-area-prefix
147                    (nth 2 (car useful-areas)))))
148                 (goto-char b)
149                 ;; We have to remove the index number entires and
150                 ;; insert article numbers instead.
151                 (while (looking-at "[0-9]+")
152                   (replace-match (int-to-string number) t t)
153                   (setq number (1+ number))
154                   (forward-line 1)))
155               (setq useful-areas (cdr useful-areas)))
156           ;; We insert HEADs.
157           (while useful-areas
158             (setq articles (car (car useful-areas))
159                   useful-areas (cdr useful-areas))
160             (while articles
161               (goto-char (point-max))
162               (insert (format "221 %d Article retrieved.\n" (car articles)))
163               (insert-buffer-substring
164                (nnsoup-narrow-to-article 
165                 (car articles) (cdr (car useful-areas)) 'head))
166               (goto-char (point-max))
167               (insert ".\n")
168               (setq articles (cdr articles))))
169
170           ;; Fold continuation lines.
171           (goto-char (point-min))
172           (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
173             (replace-match " " t t)))
174         (if use-nov 'nov 'headers)))))
175
176 (defun nnsoup-open-server (server &optional defs)
177   (nnsoup-set-variables)
178   (nnheader-init-server-buffer)
179   (if (equal server nnsoup-current-server)
180       t
181     (if nnsoup-current-server
182         (setq nnsoup-server-alist 
183               (cons (list nnsoup-current-server
184                           (nnheader-save-variables nnsoup-server-variables))
185                     nnsoup-server-alist)))
186     (let ((state (assoc server nnsoup-server-alist)))
187       (if state 
188           (progn
189             (nnheader-restore-variables (nth 1 state))
190             (setq nnsoup-server-alist (delq state nnsoup-server-alist)))
191         (nnheader-set-init-variables nnsoup-server-variables defs)))
192     (setq nnsoup-current-server server))
193   (nnsoup-read-active-file))
194
195 (defun nnsoup-request-close ()
196   (nnsoup-write-active-file)
197   (nnsoup-write-replies)
198   (while nnsoup-buffers
199     (and (car nnsoup-buffers)
200          (buffer-name (car nnsoup-buffers))
201          (kill-buffer (car nnsoup-buffers)))
202     (setq nnsoup-buffers (cdr nnsoup-buffers)))
203   (setq nnsoup-group-alist nil
204         nnsoup-current-group nil
205         nnsoup-current-server nil
206         nnsoup-server-alist nil
207         nnsoup-replies-list nil)
208   t)
209
210 (defun nnsoup-close-server (&optional server)
211   t)
212
213 (defun nnsoup-server-opened (&optional server)
214   (and (equal server nnsoup-current-server)
215        nntp-server-buffer
216        (buffer-name nntp-server-buffer)))
217
218 (defun nnsoup-status-message (&optional server)
219   nnsoup-status-string)
220
221 (defun nnsoup-request-article (id &optional newsgroup server buffer)
222   (nnsoup-possibly-change-group newsgroup)
223   (let ((buffer (or buffer nntp-server-buffer)))
224     (save-excursion
225       (set-buffer buffer)
226       (erase-buffer)
227       (if (stringp id)
228           ()
229         (insert-buffer-substring
230          (nnsoup-narrow-to-article id))
231         t))))
232
233 (defun nnsoup-request-group (group &optional server dont-check)
234   (nnsoup-possibly-change-group group)
235   (if dont-check 
236       ()
237     (let ((area (cdr (assoc group nnsoup-group-alist)))
238           min max)
239       (save-excursion
240         (set-buffer nntp-server-buffer)
241         (erase-buffer)
242         (setq min (car (car (car area))))
243         (while (cdr area)
244           (setq area (cdr area)))
245         (setq max (cdr (car (car area))))
246         (insert (format "211 %d %d %d %s\n" 
247                         (max (1+ (- max min)) 0) min max group)))))
248   t)
249
250 (defun nnsoup-close-group (group &optional server)
251   t)
252
253 (defun nnsoup-request-list (&optional server)
254   (save-excursion
255     (set-buffer nntp-server-buffer)
256     (erase-buffer)
257     (let ((alist nnsoup-group-alist)
258           min)
259       (while alist
260         (setq min (car (car (nth 1 (car alist)))))
261         (insert (format "%s %d %d y\n" (car (car alist))
262                         (let ((areas (car alist)))
263                           (while (cdr areas)
264                             (setq areas (cdr areas)))
265                           (cdr (car (car areas)))) min))
266         (setq alist (cdr alist)))
267       t)))
268
269 (defun nnsoup-request-scan (group &optional server)
270   (or nnsoup-group-alist (nnsoup-read-areas))
271   (nnsoup-unpack-packets))
272
273 (defun nnsoup-request-newgroups (date &optional server)
274   (nnsoup-request-list))
275
276 (defun nnsoup-request-list-newsgroups (&optional server)
277   nil)
278
279 (defun nnsoup-request-post (&optional server)
280   (nnsoup-store-reply "news")
281   t)
282
283 (defun nnsoup-request-mail ()
284   (nnsoup-store-reply "mail")
285   t)
286
287 (defun nnsoup-request-post-buffer (post group &rest args)
288   (nnsoup-possibly-change-group group)
289   (apply
290    ;; Find out whether the source for this group is a mail or a news
291    ;; group and call the right function for getting a buffer.
292    (let ((enc (nth 1 (car (cdr (assoc nnsoup-current-group
293                                       nnsoup-group-alist))))))
294      (if (and enc
295               (= (gnus-soup-encoding-kind (gnus-soup-area-encoding enc)) ?m))
296          'nnmail-request-post-buffer 
297        'nntp-request-post-buffer))
298    post group args))
299
300 \f
301 ;;; Internal functions
302
303 (defun nnsoup-possibly-change-group (group &optional force)
304   (if group
305       (setq nnsoup-current-group group)
306     t))
307
308 (defun nnsoup-read-active-file ()
309   (if (file-exists-p nnsoup-active-file)
310       (condition-case ()
311           (load nnsoup-active-file)
312         (error nil))))
313
314 (defun nnsoup-write-active-file ()
315   (save-excursion
316     (set-buffer (get-buffer-create " *nnsoup work*"))
317     (buffer-disable-undo (current-buffer))
318     (erase-buffer)
319     (insert (format "(setq nnsoup-group-alist '%S)\n" nnsoup-group-alist))
320     (write-region (point-min) (point-max) nnsoup-active-file
321                   nil 'silent)
322     (kill-buffer (current-buffer))))
323
324 (defun nnsoup-read-areas ()
325   (save-excursion
326     (set-buffer nntp-server-buffer)
327     (let ((areas (gnus-soup-parse-areas (concat nnsoup-directory "AREAS")))
328           entry number area lnum)
329       ;; Go through all areas in the new AREAS file.
330       (while areas
331         (setq area (car areas)
332               areas (cdr areas))
333         ;; Find the number of new articles in this area.
334         (setq number (nnsoup-number-of-articles area))
335         (if (not (setq entry (assoc (gnus-soup-area-name area)
336                                     nnsoup-group-alist)))
337             ;; If this is a new area (group), we just add this info to
338             ;; the group alist. 
339             (setq nnsoup-group-alist
340                   (cons (list (gnus-soup-area-name area)
341                               (list (cons 1 number) area))
342                         nnsoup-group-alist))
343           ;; There are already articles in this group, so we add this
344           ;; info to the end of the entry.
345           (let ((e (cdr entry)))
346             (while (cdr e)
347               (setq e (cdr e)))
348             (setcdr e (list (list (cons (setq lnum (1+ (cdr (car (car e)))))
349                                         (+ lnum number)) 
350                                   area)))))))
351     (nnsoup-write-active-file)))
352
353 (defun nnsoup-number-of-articles (area)
354   (save-excursion
355     (cond 
356      ;; If the number is in the area info, we just return it.
357      ((gnus-soup-area-number area)
358       (gnus-soup-area-number area))
359      ;; If there is an index file, we just count the lines.
360      ((/= (gnus-soup-encoding-index (gnus-soup-area-encoding area)) ?n)
361       (set-buffer (nnsoup-index-buffer (gnus-soup-area-prefix area)))
362       (count-lines (point-min) (point-max)))
363      ;; We do it the hard way - re-searching through the message
364      ;; buffer. 
365      (t
366       (set-buffer (nnsoup-message-buffer (gnus-soup-area-prefix area)))
367       (goto-char (point-min))
368       (let ((regexp (nnsoup-header (gnus-soup-encoding-format 
369                                     (gnus-soup-area-encoding area))))
370             (num 0))
371         (while (re-search-forward regexp nil t)
372           (setq num (1+ num)))
373         num)))))
374
375 (defun nnsoup-index-buffer (prefix &optional message)
376   (let* ((file (concat prefix (if message ".MSG" ".IDX")))
377          (buffer-name (concat " *nnsoup " file "*")))
378     (or (get-buffer buffer-name)        ; File aready loaded.
379         (save-excursion                 ; Load the file.
380           (set-buffer (get-buffer-create buffer-name))
381           (setq nnsoup-buffers (cons (current-buffer) nnsoup-buffers))
382           (insert-file-contents (concat nnsoup-directory file))
383           (current-buffer)))))
384
385 (defun nnsoup-file (prefix &optional message)
386   (concat nnsoup-directory prefix (if message ".MSG" ".IDX")))
387
388 (defun nnsoup-message-buffer (prefix)
389   (nnsoup-index-buffer prefix 'msg))
390
391 (defun nnsoup-unpack-packets ()
392   (let ((packets (directory-files
393                   nnsoup-packet-directory t nnsoup-packet-regexp))
394         msg)
395     (while packets
396       (message (setq msg (format "nnsoup: unpacking %s..." (car packets))))
397       (gnus-soup-unpack-packet nnsoup-directory nnsoup-unpacker (car packets))
398       (delete-file (car packets))
399       (nnsoup-read-areas)
400       (message "%sdone" msg)
401       (setq packets (cdr packets)))))
402
403 (defun nnsoup-narrow-to-article (article &optional area head)
404   (let* ((area (or area (nnsoup-article-to-area article nnsoup-current-group)))
405          (prefix (gnus-soup-area-prefix (nth 1 area)))
406          beg end msg-buf)
407     (setq msg-buf (nnsoup-index-buffer prefix 'msg))
408     (save-excursion
409       (cond
410        ;; We use the index file to find out where the article begins and ends. 
411        ((and (= (gnus-soup-encoding-index 
412                  (gnus-soup-area-encoding (nth 1 area)))
413                 ?c)
414              (file-exists-p (nnsoup-file prefix)))
415         (set-buffer (nnsoup-index-buffer prefix))
416         (widen)
417         (goto-char (point-min))
418         (forward-line (- article (car (car area))))
419         (setq beg (read (current-buffer)))
420         (forward-line 1)
421         (if (looking-at "[0-9]+")
422             (progn
423               (setq end (read (current-buffer)))
424               (set-buffer msg-buf)
425               (widen)
426               (let ((format (gnus-soup-encoding-format
427                              (gnus-soup-area-encoding (nth 1 area)))))
428                 (goto-char end)
429                 (if (or (= format ?n) (= format ?m))
430                     (setq end (progn (forward-line -1) (point))))))
431           (set-buffer msg-buf))
432         (widen)
433         (narrow-to-region beg (or end (point-max))))
434        (t
435         (set-buffer msg-buf)
436         (widen)
437         (goto-char (point-min))
438         (let ((header (nnsoup-header 
439                        (gnus-soup-encoding-format 
440                         (gnus-soup-area-encoding (nth 1 area))))))
441           (re-search-forward header nil t (- article (car (car area))))
442           (narrow-to-region
443            (match-beginning 0)
444            (if (re-search-forward header nil t)
445                (match-beginning 0)
446              (point-max))))))
447       (goto-char (point-min))
448       (if (not head)
449           ()
450         (narrow-to-region
451          (point-min)
452          (if (search-forward "\n\n" nil t)
453              (1- (point))
454            (point-max))))
455       msg-buf)))
456
457 (defun nnsoup-header (format)
458   (cond 
459    ((= format ?n)
460     "^#! *rnews +[0-9]+ *$")
461    ((= format ?m)
462     (concat "^" rmail-unix-mail-delimiter))
463    ((= format ?M)
464     "^\^A\^A\^A\^A\n")
465    (t
466     (error "Unknown format: %c" format))))
467
468 (defun nnsoup-pack-replies ()
469   "Make an outbound package of SOUP replies."
470   (interactive)
471   (nnsoup-write-active-file)
472   (nnsoup-write-replies)
473   (gnus-soup-pack nnsoup-replies-directory nnsoup-packer))
474
475 (defun nnsoup-write-replies ()
476   (gnus-soup-write-replies nnsoup-replies-directory nnsoup-replies-list))
477
478 (defun nnsoup-article-to-area (article group)
479   (let ((areas (cdr (assoc group nnsoup-group-alist))))
480     (while (and areas (< (cdr (car (car areas))) article))
481       (setq areas (cdr areas)))
482     (and areas (car areas))))
483
484 (defun nnsoup-set-variables ()
485   (setq gnus-inews-article-function 'nnsoup-request-post)
486   (setq gnus-mail-send-method 'nnsoup-request-mail)
487   (setq send-mail-function 'nnsoup-request-mail))
488
489 (defun nnsoup-store-reply (kind)
490   ;; Mostly stolen from `sendmail.el'.
491   (let ((tembuf (generate-new-buffer " sendmail temp"))
492         (case-fold-search nil)
493         (mailbuf (current-buffer))
494         delimline)
495     (save-excursion
496       (set-buffer tembuf)
497       (erase-buffer)
498       (insert-buffer-substring mailbuf)
499       (goto-char (point-max))
500       ;; require one newline at the end.
501       (or (= (preceding-char) ?\n)
502           (insert ?\n))
503       ;; Change header-delimiter to be what sendmail expects.
504       (goto-char (point-min))
505       (re-search-forward
506         (concat "^" (regexp-quote mail-header-separator) "\n"))
507       (replace-match "\n")
508       (backward-char 1)
509       (setq delimline (point-marker))
510       (if mail-aliases (expand-mail-aliases (point-min) delimline))
511       (goto-char (point-min))
512       ;; ignore any blank lines in the header
513       (while (and (re-search-forward "\n\n\n*" delimline t)
514                   (< (point) delimline))
515         (replace-match "\n"))
516       (let ((case-fold-search t))
517         (goto-char (point-min))
518         ;; Find and handle any FCC fields.
519         (goto-char (point-min))
520         (if (re-search-forward "^FCC:" delimline t)
521             (mail-do-fcc delimline))
522         (goto-char (point-min))
523         ;; "S:" is an abbreviation for "Subject:".
524         (goto-char (point-min))
525         (if (re-search-forward "^S:" delimline t)
526             (replace-match "Subject:"))
527         ;; Don't send out a blank subject line
528         (goto-char (point-min))
529         (if (re-search-forward "^Subject:[ \t]*\n" delimline t)
530             (replace-match ""))
531         ;; Insert an extra newline if we need it to work around
532         ;; Sun's bug that swallows newlines.
533         (goto-char (1+ delimline))
534         (if (eval mail-mailer-swallows-blank-line)
535             (newline)))
536       (gnus-soup-store 
537        nnsoup-replies-directory 
538        (nnsoup-kind-to-prefix kind) nil nnsoup-replies-format-type
539        nnsoup-replies-index-type)
540       (kill-buffer tembuf))))
541
542 (defun nnsoup-kind-to-prefix (kind)
543   (or nnsoup-replies-list
544       (setq nnsoup-replies-list
545             (gnus-soup-parse-replies 
546              (concat nnsoup-replies-directory "REPLIES"))))
547   (let ((replies nnsoup-replies-list))
548     (while (and replies 
549                 (not (string= kind (gnus-soup-reply-kind (car replies)))))
550       (setq replies (cdr replies)))
551     (if replies
552         (gnus-soup-reply-prefix (car replies))
553       (setq nnsoup-replies-list
554             (cons (vector (gnus-soup-unique-prefix nnsoup-replies-directory)
555                           kind 
556                           (format "%c%c%c"
557                                   nnsoup-replies-format-type
558                                   nnsoup-replies-index-type
559                                   (if (string= kind "news")
560                                       ?n ?m)))
561                   nnsoup-replies-list))
562       (gnus-soup-reply-prefix (car nnsoup-replies-list)))))
563         
564 (provide 'nnsoup)
565
566 ;;; nnsoup.el ends here