1 ;;; nnsoup.el --- SOUP access for Gnus
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;; Keywords: news, mail
8 ;; This file is part of GNU Emacs.
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)
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.
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.
33 (defvar nnsoup-directory "~/SOUP/"
34 "*SOUP packet directory directory.")
36 (defvar nnsoup-replies-directory (concat nnsoup-directory "replies/")
37 "*Directory where outgoing packets will be composed.")
39 (defvar nnsoup-replies-format-type ?n
40 "*Format of the replies packages.")
42 (defvar nnsoup-replies-index-type ?n
43 "*Index type of the replies packages.")
45 (defvar nnsoup-active-file (concat nnsoup-directory "active")
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.")
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.")
58 (defvar nnsoup-packet-directory "~/"
59 "*Where nnsoup will look for incoming packets.")
61 (defvar nnsoup-packet-regexp "Soupout"
62 "*Regular expression matching SOUP packets in `nnsoup-packet-directory'.")
66 (defconst nnsoup-version "nnsoup 0.0"
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)
79 (defvar nnsoup-current-server nil)
80 (defvar nnsoup-server-alist nil)
81 (defvar nnsoup-server-variables
83 (list 'nnsoup-directory nnsoup-directory)
84 (list 'nnsoup-active-file nnsoup-active-file)
85 '(nnsoup-status-string "")
86 '(nnsoup-group-alist nil)))
90 ;;; Interface functions.
92 (defun nnsoup-retrieve-headers (sequence &optional group server fetch-old)
93 (nnsoup-possibly-change-group group)
95 (set-buffer nntp-server-buffer)
97 (let ((areas (cdr (assoc nnsoup-current-group nnsoup-group-alist)))
100 useful-areas this-area-seq)
101 (if (stringp (car sequence))
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)))
111 ;; This is a useful area.
112 (setq useful-areas (cons (car areas) useful-areas)
114 ;; We take note whether this MSG has a corresponding IDX
116 (if (or (= (gnus-soup-encoding-index
117 (gnus-soup-area-encoding (nth 1 (car areas)))) ?n)
120 (gnus-soup-area-prefix (nth 1 (car areas)))))))
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)))))
130 ;; We now have a list of article numbers and corresponding
132 (setq useful-areas (nreverse useful-areas))
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.
141 (goto-char (point-max))
143 (number (car (nth 1 (car useful-areas)))))
144 (insert-buffer-substring
146 (gnus-soup-area-prefix
147 (nth 2 (car useful-areas)))))
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))
155 (setq useful-areas (cdr useful-areas)))
158 (setq articles (car (car useful-areas))
159 useful-areas (cdr useful-areas))
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))
168 (setq articles (cdr articles))))
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)))))
176 (defun nnsoup-open-server (server &optional defs)
177 (nnsoup-set-variables)
178 (nnheader-init-server-buffer)
179 (if (equal server nnsoup-current-server)
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)))
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))
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)
210 (defun nnsoup-close-server (&optional server)
213 (defun nnsoup-server-opened (&optional server)
214 (and (equal server nnsoup-current-server)
216 (buffer-name nntp-server-buffer)))
218 (defun nnsoup-status-message (&optional server)
219 nnsoup-status-string)
221 (defun nnsoup-request-article (id &optional newsgroup server buffer)
222 (nnsoup-possibly-change-group newsgroup)
223 (let ((buffer (or buffer nntp-server-buffer)))
229 (insert-buffer-substring
230 (nnsoup-narrow-to-article id))
233 (defun nnsoup-request-group (group &optional server dont-check)
234 (nnsoup-possibly-change-group group)
237 (let ((area (cdr (assoc group nnsoup-group-alist)))
240 (set-buffer nntp-server-buffer)
242 (setq min (car (car (car 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)))))
250 (defun nnsoup-close-group (group &optional server)
253 (defun nnsoup-request-list (&optional server)
254 (or nnsoup-group-alist (nnsoup-read-areas))
255 (nnsoup-unpack-packets)
257 (set-buffer nntp-server-buffer)
259 (let ((alist nnsoup-group-alist)
262 (setq min (car (car (nth 1 (car alist)))))
263 (insert (format "%s %d %d y\n" (car (car alist))
264 (let ((areas (car alist)))
266 (setq areas (cdr areas)))
267 (cdr (car (car areas)))) min))
268 (setq alist (cdr alist)))
271 (defun nnsoup-request-newgroups (date &optional server)
272 (nnsoup-request-list))
274 (defun nnsoup-request-list-newsgroups (&optional server)
277 (defun nnsoup-request-post (&optional server)
278 (nnsoup-store-reply "news")
281 (defun nnsoup-request-mail ()
282 (nnsoup-store-reply "mail")
285 (defun nnsoup-request-post-buffer (post group &rest args)
286 (nnsoup-possibly-change-group group)
288 ;; Find out whether the source for this group is a mail or a news
289 ;; group and call the right function for getting a buffer.
290 (let ((enc (nth 1 (car (cdr (assoc nnsoup-current-group
291 nnsoup-group-alist))))))
293 (= (gnus-soup-encoding-kind (gnus-soup-area-encoding enc)) ?m))
294 'nnmail-request-post-buffer
295 'nntp-request-post-buffer))
299 ;;; Internal functions
301 (defun nnsoup-possibly-change-group (group &optional force)
303 (setq nnsoup-current-group group)
306 (defun nnsoup-read-active-file ()
307 (if (file-exists-p nnsoup-active-file)
309 (load nnsoup-active-file)
312 (defun nnsoup-write-active-file ()
314 (set-buffer (get-buffer-create " *nnsoup work*"))
315 (buffer-disable-undo (current-buffer))
317 (insert (format "(setq nnsoup-group-alist '%S)\n" nnsoup-group-alist))
318 (write-region (point-min) (point-max) nnsoup-active-file
320 (kill-buffer (current-buffer))))
322 (defun nnsoup-read-areas ()
324 (set-buffer nntp-server-buffer)
325 (let ((areas (gnus-soup-parse-areas (concat nnsoup-directory "AREAS")))
326 entry number area lnum)
327 ;; Go through all areas in the new AREAS file.
329 (setq area (car areas)
331 ;; Find the number of new articles in this area.
332 (setq number (nnsoup-number-of-articles area))
333 (if (not (setq entry (assoc (gnus-soup-area-name area)
334 nnsoup-group-alist)))
335 ;; If this is a new area (group), we just add this info to
337 (setq nnsoup-group-alist
338 (cons (list (gnus-soup-area-name area)
339 (list (cons 1 number) area))
341 ;; There are already articles in this group, so we add this
342 ;; info to the end of the entry.
343 (let ((e (cdr entry)))
346 (setcdr e (list (list (cons (setq lnum (1+ (cdr (car (car e)))))
349 (nnsoup-write-active-file)))
351 (defun nnsoup-number-of-articles (area)
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
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))))
369 (while (re-search-forward regexp nil t)
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))
383 (defun nnsoup-file (prefix &optional message)
384 (concat nnsoup-directory prefix (if message ".MSG" ".IDX")))
386 (defun nnsoup-message-buffer (prefix)
387 (nnsoup-index-buffer prefix 'msg))
389 (defun nnsoup-unpack-packets ()
390 (let ((packets (directory-files
391 nnsoup-packet-directory t nnsoup-packet-regexp))
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))
398 (message "%sdone" msg)
399 (setq packets (cdr packets)))))
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)))
405 (setq msg-buf (nnsoup-index-buffer prefix 'msg))
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)))
412 (file-exists-p (nnsoup-file prefix)))
413 (set-buffer (nnsoup-index-buffer prefix))
415 (goto-char (point-min))
416 (forward-line (- article (car (car area))))
417 (setq beg (read (current-buffer)))
419 (if (looking-at "[0-9]+")
421 (setq end (read (current-buffer)))
424 (let ((format (gnus-soup-encoding-format
425 (gnus-soup-area-encoding (nth 1 area)))))
427 (if (or (= format ?n) (= format ?m))
428 (setq end (progn (forward-line -1) (point))))))
429 (set-buffer msg-buf))
431 (narrow-to-region beg (or end (point-max))))
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))))
442 (if (re-search-forward header nil t)
445 (goto-char (point-min))
450 (if (search-forward "\n\n" nil t)
455 (defun nnsoup-header (format)
458 "^#! *rnews +[0-9]+ *$")
460 (concat "^" rmail-unix-mail-delimiter))
464 (error "Unknown format: %c" format))))
466 (defun nnsoup-pack-replies ()
467 "Make an outbound package of SOUP replies."
469 (nnsoup-write-active-file)
470 (nnsoup-write-replies)
471 (gnus-soup-pack nnsoup-replies-directory nnsoup-packer))
473 (defun nnsoup-write-replies ()
474 (gnus-soup-write-replies nnsoup-replies-directory nnsoup-replies-list))
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))))
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))
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))
496 (insert-buffer-substring mailbuf)
497 (goto-char (point-max))
498 ;; require one newline at the end.
499 (or (= (preceding-char) ?\n)
501 ;; Change header-delimiter to be what sendmail expects.
502 (goto-char (point-min))
504 (concat "^" (regexp-quote mail-header-separator) "\n"))
507 (setq delimline (point-marker))
508 (if mail-aliases (expand-mail-aliases (point-min) delimline))
509 (goto-char (point-min))
510 ;; ignore any blank lines in the header
511 (while (and (re-search-forward "\n\n\n*" delimline t)
512 (< (point) delimline))
513 (replace-match "\n"))
514 (let ((case-fold-search t))
515 (goto-char (point-min))
516 ;; Find and handle any FCC fields.
517 (goto-char (point-min))
518 (if (re-search-forward "^FCC:" delimline t)
519 (mail-do-fcc delimline))
520 (goto-char (point-min))
521 ;; "S:" is an abbreviation for "Subject:".
522 (goto-char (point-min))
523 (if (re-search-forward "^S:" delimline t)
524 (replace-match "Subject:"))
525 ;; Don't send out a blank subject line
526 (goto-char (point-min))
527 (if (re-search-forward "^Subject:[ \t]*\n" delimline t)
529 ;; Insert an extra newline if we need it to work around
530 ;; Sun's bug that swallows newlines.
531 (goto-char (1+ delimline))
532 (if (eval mail-mailer-swallows-blank-line)
535 nnsoup-replies-directory
536 (nnsoup-kind-to-prefix kind) nil nnsoup-replies-format-type
537 nnsoup-replies-index-type)
538 (kill-buffer tembuf))))
540 (defun nnsoup-kind-to-prefix (kind)
541 (or nnsoup-replies-list
542 (setq nnsoup-replies-list
543 (gnus-soup-parse-replies
544 (concat nnsoup-replies-directory "REPLIES"))))
545 (let ((replies nnsoup-replies-list))
547 (not (string= kind (gnus-soup-reply-kind (car replies)))))
548 (setq replies (cdr replies)))
550 (gnus-soup-reply-prefix (car replies))
551 (setq nnsoup-replies-list
552 (cons (vector (gnus-soup-unique-prefix nnsoup-replies-directory)
555 nnsoup-replies-format-type
556 nnsoup-replies-index-type
557 (if (string= kind "news")
559 nnsoup-replies-list))
560 (gnus-soup-reply-prefix (car nnsoup-replies-list)))))
564 ;;; nnsoup.el ends here