*** empty log message ***
[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)
93   (nnsoup-possibly-change-group group)
94   (save-excursion
95     (set-buffer nntp-server-buffer)
96     (erase-buffer)
97     (let ((count 0)
98           (areas (cdr (assoc nnsoup-current-group nnsoup-group-alist)))
99           (articles sequence)
100           (use-nov t)
101           beg article useful-areas this-area-seq)
102       (if (stringp (car sequence))
103           'headers
104         ;; We go through all the areas and find which files the
105         ;; articles in SEQUENCE come from.
106         (while (and areas sequence)
107           ;; Peel off areas that are below sequence.
108           (while (and areas (< (cdr (car (car areas))) (car sequence)))
109             (setq areas (cdr areas)))
110           (if (not areas)
111               ()
112             ;; This is a useful area.
113             (setq useful-areas (cons (car areas) useful-areas)
114                   this-area-seq nil)
115             ;; We take note whether this MSG has a corresponding IDX
116             ;; for later use.
117             (if (or (= (gnus-soup-encoding-index 
118                         (gnus-soup-area-encoding (nth 1 (car areas)))) ?n)
119                     (not (file-exists-p
120                           (nnsoup-file
121                            (gnus-soup-area-prefix (nth 1 (car areas)))))))
122                 (setq use-nov nil))
123             ;; We assing the portion of `sequence' that is relevant to
124             ;; this MSG packet to this packet.
125             (while (and sequence (<= (car sequence) (cdr (car (car areas)))))
126               (setq this-area-seq (cons (car sequence) this-area-seq)
127                     sequence (cdr sequence)))
128             (setcar useful-areas (cons (nreverse this-area-seq)
129                                        (car useful-areas)))))
130
131         ;; We now have a list of article numbers and corresponding
132         ;; areas. 
133         (setq useful-areas (nreverse useful-areas))
134
135         ;; Two different approaches depending on whether all the MSG
136         ;; files have corresponding IDX files.  If they all do, we
137         ;; simply return the relevant IDX files and let Gnus sort out
138         ;; what lines are relevant.  If some of the IDX files are
139         ;; missing, we must return HEADs for all the articles.
140         (if use-nov
141             (while useful-areas
142               (goto-char (point-max))
143               (let ((b (point))
144                     (number (car (nth 1 (car useful-areas)))))
145                 (insert-buffer-substring
146                  (nnsoup-index-buffer
147                   (gnus-soup-area-prefix
148                    (nth 2 (car useful-areas)))))
149                 (goto-char b)
150                 ;; We have to remove the index number entires and
151                 ;; insert article numbers instead.
152                 (while (looking-at "[0-9]+")
153                   (replace-match (int-to-string number) t t)
154                   (setq number (1+ number))
155                   (forward-line 1)))
156               (setq useful-areas (cdr useful-areas)))
157           ;; We insert HEADs.
158           (while useful-areas
159             (setq articles (car (car useful-areas))
160                   useful-areas (cdr useful-areas))
161             (while articles
162               (goto-char (point-max))
163               (insert (format "221 %d Article retrieved.\n" (car articles)))
164               (insert-buffer-substring
165                (nnsoup-narrow-to-article 
166                 (car articles) (cdr (car useful-areas)) 'head))
167               (goto-char (point-max))
168               (insert ".\n")
169               (setq articles (cdr articles))))
170
171           ;; Fold continuation lines.
172           (goto-char (point-min))
173           (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
174             (replace-match " " t t)))
175         (if use-nov 'nov 'headers)))))
176
177 (defun nnsoup-open-server (server &optional defs)
178   (nnsoup-set-variables)
179   (nnheader-init-server-buffer)
180   (if (equal server nnsoup-current-server)
181       t
182     (if nnsoup-current-server
183         (setq nnsoup-server-alist 
184               (cons (list nnsoup-current-server
185                           (nnheader-save-variables nnsoup-server-variables))
186                     nnsoup-server-alist)))
187     (let ((state (assoc server nnsoup-server-alist)))
188       (if state 
189           (progn
190             (nnheader-restore-variables (nth 1 state))
191             (setq nnsoup-server-alist (delq state nnsoup-server-alist)))
192         (nnheader-set-init-variables nnsoup-server-variables defs)))
193     (setq nnsoup-current-server server))
194   (nnsoup-read-active-file))
195
196 (defun nnsoup-request-close ()
197   (nnsoup-write-active-file)
198   (nnsoup-write-replies)
199   (while nnsoup-buffers
200     (and (car nnsoup-buffers)
201          (buffer-name (car nnsoup-buffers))
202          (kill-buffer (car nnsoup-buffers)))
203     (setq nnsoup-buffers (cdr nnsoup-buffers)))
204   (setq nnsoup-group-alist nil
205         nnsoup-current-group nil
206         nnsoup-current-server nil
207         nnsoup-server-alist nil
208         nnsoup-replies-list nil)
209   t)
210
211 (defun nnsoup-close-server (&optional server)
212   t)
213
214 (defun nnsoup-server-opened (&optional server)
215   (and (equal server nnsoup-current-server)
216        nntp-server-buffer
217        (buffer-name nntp-server-buffer)))
218
219 (defun nnsoup-status-message (&optional server)
220   nnsoup-status-string)
221
222 (defun nnsoup-request-article (id &optional newsgroup server buffer)
223   (nnsoup-possibly-change-group newsgroup)
224   (let ((buffer (or buffer nntp-server-buffer)))
225     (save-excursion
226       (set-buffer buffer)
227       (erase-buffer)
228       (if (stringp id)
229           ()
230         (insert-buffer-substring
231          (nnsoup-narrow-to-article id))
232         t))))
233
234 (defun nnsoup-request-group (group &optional server dont-check)
235   (nnsoup-possibly-change-group group)
236   (if dont-check 
237       ()
238     (let ((area (cdr (assoc group nnsoup-group-alist)))
239           min max)
240       (save-excursion
241         (set-buffer nntp-server-buffer)
242         (erase-buffer)
243         (setq min (car (car (car area))))
244         (while (cdr area)
245           (setq area (cdr area)))
246         (setq max (cdr (car (car area))))
247         (insert (format "211 %d %d %d %s\n" 
248                         (max (1+ (- max min)) 0) min max group)))))
249   t)
250
251 (defun nnsoup-close-group (group &optional server)
252   t)
253
254 (defun nnsoup-request-list (&optional server)
255   (or nnsoup-group-alist (nnsoup-read-areas))
256   (nnsoup-unpack-packets)
257   (save-excursion
258     (set-buffer nntp-server-buffer)
259     (erase-buffer)
260     (let ((alist nnsoup-group-alist)
261           min)
262       (while alist
263         (setq min (car (car (nth 1 (car alist)))))
264         (insert (format "%s %d %d y\n" (car (car alist))
265                         (let ((areas (car alist)))
266                           (while (cdr areas)
267                             (setq areas (cdr areas)))
268                           (cdr (car (car areas)))) min))
269         (setq alist (cdr alist)))
270       t)))
271
272 (defun nnsoup-request-newgroups (date &optional server)
273   (nnsoup-request-list))
274
275 (defun nnsoup-request-list-newsgroups (&optional server)
276   nil)
277
278 (defun nnsoup-request-post (&optional server)
279   (nnsoup-store-reply "news")
280   t)
281
282 (defun nnsoup-request-mail ()
283   (nnsoup-store-reply "mail")
284   t)
285
286 (defun nnsoup-request-post-buffer (post group &rest args)
287   (nnsoup-possibly-change-group group)
288   (apply
289    ;; Find out whether the source for this group is a mail or a news
290    ;; group and call the right function for getting a buffer.
291    (let ((enc (nth 1 (car (cdr (assoc nnsoup-current-group
292                                       nnsoup-group-alist))))))
293      (if (and enc
294               (= (gnus-soup-encoding-kind (gnus-soup-area-encoding enc)) ?m))
295          'nnmail-request-post-buffer 
296        'nntp-request-post-buffer))
297    post group args))
298
299 \f
300 ;;; Internal functions
301
302 (defun nnsoup-possibly-change-group (group &optional force)
303   (if group
304       (setq nnsoup-current-group group)
305     t))
306
307 (defun nnsoup-read-active-file ()
308   (if (file-exists-p nnsoup-active-file)
309       (condition-case ()
310           (load nnsoup-active-file)
311         (error nil))))
312
313 (defun nnsoup-write-active-file ()
314   (save-excursion
315     (set-buffer (get-buffer-create " *nnsoup work*"))
316     (buffer-disable-undo (current-buffer))
317     (erase-buffer)
318     (insert (format "(setq nnsoup-group-alist '%S)\n" nnsoup-group-alist))
319     (write-region (point-min) (point-max) nnsoup-active-file
320                   nil 'silent)
321     (kill-buffer (current-buffer))))
322
323 (defun nnsoup-read-areas ()
324   (save-excursion
325     (set-buffer nntp-server-buffer)
326     (let ((areas (gnus-soup-parse-areas (concat nnsoup-directory "AREAS")))
327           entry number area lnum)
328       ;; Go through all areas in the new AREAS file.
329       (while areas
330         (setq area (car areas)
331               areas (cdr areas))
332         ;; Find the number of new articles in this area.
333         (setq number (nnsoup-number-of-articles area))
334         (if (not (setq entry (assoc (gnus-soup-area-name area)
335                                     nnsoup-group-alist)))
336             ;; If this is a new area (group), we just add this info to
337             ;; the group alist. 
338             (setq nnsoup-group-alist
339                   (cons (list (gnus-soup-area-name area)
340                               (list (cons 1 number) area))
341                         nnsoup-group-alist))
342           ;; There are already articles in this group, so we add this
343           ;; info to the end of the entry.
344           (let ((e (cdr entry)))
345             (while (cdr e)
346               (setq e (cdr e)))
347             (setcdr e (list (list (cons (setq lnum (1+ (cdr (car (car e)))))
348                                         (+ lnum number)) 
349                                   area)))))))))
350
351 (defun nnsoup-number-of-articles (area)
352   (save-excursion
353     (cond 
354      ;; If the number is in the area info, we just return it.
355      ((gnus-soup-area-number area)
356       (gnus-soup-area-number area))
357      ;; If there is an index file, we just count the lines.
358      ((/= (gnus-soup-encoding-index (gnus-soup-area-encoding area)) ?n)
359       (set-buffer (nnsoup-index-buffer (gnus-soup-area-prefix area)))
360       (count-lines (point-min) (point-max)))
361      ;; We do it the hard way - re-searching through the message
362      ;; buffer. 
363      (t
364       (set-buffer (nnsoup-message-buffer (gnus-soup-area-prefix area)))
365       (goto-char (point-min))
366       (let ((regexp (nnsoup-header (gnus-soup-encoding-format 
367                                     (gnus-soup-area-encoding area))))
368             (num 0))
369         (while (re-search-forward regexp nil t)
370           (setq num (1+ num)))
371         num)))))
372
373 (defun nnsoup-index-buffer (prefix &optional message)
374   (let* ((file (concat prefix (if message ".MSG" ".IDX")))
375          (buffer-name (concat " *nnsoup " file "*")))
376     (or (get-buffer buffer-name)        ; File aready loaded.
377         (save-excursion                 ; Load the file.
378           (set-buffer (get-buffer-create buffer-name))
379           (setq nnsoup-buffers (cons (current-buffer) nnsoup-buffers))
380           (insert-file-contents (concat nnsoup-directory file))
381           (current-buffer)))))
382
383 (defun nnsoup-file (prefix &optional message)
384   (concat nnsoup-directory prefix (if message ".MSG" ".IDX")))
385
386 (defun nnsoup-message-buffer (prefix)
387   (nnsoup-index-buffer prefix 'msg))
388
389 (defun nnsoup-unpack-packets ()
390   (let ((packets (directory-files
391                   nnsoup-packet-directory t nnsoup-packet-regexp))
392         msg)
393     (while packets
394       (message (setq msg (format "nnsoup: unpacking %s..." (car packets))))
395       (gnus-soup-unpack-packet nnsoup-directory nnsoup-unpacker (car packets))
396       (delete-file (car packets))
397       (nnsoup-read-areas)
398       (message "%sdone" msg)
399       (setq packets (cdr packets)))))
400
401 (defun nnsoup-narrow-to-article (article &optional area head)
402   (let* ((area (or area (nnsoup-article-to-area article nnsoup-current-group)))
403          (prefix (gnus-soup-area-prefix (nth 1 area)))
404          beg end msg-buf)
405     (setq msg-buf (nnsoup-index-buffer prefix 'msg))
406     (save-excursion
407       (cond
408        ;; We use the index file to find out where the article begins and ends. 
409        ((and (= (gnus-soup-encoding-index 
410                  (gnus-soup-area-encoding (nth 1 area)))
411                 ?c)
412              (file-exists-p (nnsoup-file prefix)))
413         (set-buffer (nnsoup-index-buffer prefix))
414         (widen)
415         (goto-char (point-min))
416         (forward-line (- article (car (car area))))
417         (setq beg (read (current-buffer)))
418         (forward-line 1)
419         (if (looking-at "[0-9]+")
420             (progn
421               (setq end (read (current-buffer)))
422               (set-buffer msg-buf)
423               (widen)
424               (let ((format (gnus-soup-encoding-format
425                              (gnus-soup-area-encoding (nth 1 area)))))
426                 (goto-char end)
427                 (if (or (= format ?n) (= format ?m))
428                     (setq end (progn (forward-line -2) (point))))))
429           (set-buffer msg-buf))
430         (widen)
431         (narrow-to-region beg (or end (point-max))))
432        (t
433         (set-buffer msg-buf)
434         (widen)
435         (goto-char (point-min))
436         (let ((header (nnsoup-header 
437                        (gnus-soup-encoding-format 
438                         (gnus-soup-area-encoding (nth 1 area))))))
439           (re-search-forward header nil t (- article (car (car area))))
440           (narrow-to-region
441            (match-beginning 0)
442            (if (re-search-forward header nil t)
443                (match-beginning 0)
444              (point-max))))))
445       (goto-char (point-min))
446       (if (not head)
447           ()
448         (narrow-to-region
449          (point-min)
450          (if (search-forward "\n\n" nil t)
451              (1- (point))
452            (point-max))))
453       msg-buf)))
454
455 (defun nnsoup-header (format)
456   (cond 
457    ((= format ?n)
458     "^#! *rnews +[0-9]+ *$")
459    ((= format ?m)
460     (concat "^" rmail-unix-mail-delimiter))
461    ((= format ?M)
462     "^\^A\^A\^A\^A\n")
463    (t
464     (error "Unknown format: %c" format))))
465
466 (defun nnsoup-pack-replies ()
467   "Make an outbound package of SOUP replies."
468   (interactive)
469   (nnsoup-write-active-file)
470   (nnsoup-write-replies)
471   (gnus-soup-pack nnsoup-replies-directory nnsoup-packer))
472
473 (defun nnsoup-write-replies ()
474   (gnus-soup-write-replies nnsoup-replies-directory nnsoup-replies-list))
475
476 (defun nnsoup-article-to-area (article group)
477   (let ((areas (cdr (assoc group nnsoup-group-alist))))
478     (while (and areas (< (cdr (car (car areas))) article))
479       (setq areas (cdr areas)))
480     (and areas (car areas))))
481
482 (defun nnsoup-set-variables ()
483   (setq gnus-inews-article-function 'nnsoup-request-post)
484   (setq gnus-mail-send-method 'nnsoup-request-mail)
485   (setq send-mail-function 'nnsoup-request-mail))
486
487 (defun nnsoup-store-reply (kind)
488   ;; Mostly stolen from `sendmail.el'.
489   (let ((tembuf (generate-new-buffer " sendmail temp"))
490         (case-fold-search nil)
491         (mailbuf (current-buffer))
492         delimline
493         prefix)
494     (save-excursion
495       (set-buffer tembuf)
496       (erase-buffer)
497       (insert-buffer-substring mailbuf)
498       (goto-char (point-max))
499       ;; require one newline at the end.
500       (or (= (preceding-char) ?\n)
501           (insert ?\n))
502       ;; Change header-delimiter to be what sendmail expects.
503       (goto-char (point-min))
504       (re-search-forward
505         (concat "^" (regexp-quote mail-header-separator) "\n"))
506       (replace-match "\n")
507       (backward-char 1)
508       (setq delimline (point-marker))
509       (if mail-aliases
510           (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