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)
93 (nnsoup-possibly-change-group group)
95 (set-buffer nntp-server-buffer)
98 (areas (cdr (assoc nnsoup-current-group nnsoup-group-alist)))
101 beg article useful-areas this-area-seq)
102 (if (stringp (car sequence))
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)))
112 ;; This is a useful area.
113 (setq useful-areas (cons (car areas) useful-areas)
115 ;; We take note whether this MSG has a corresponding IDX
117 (if (or (= (gnus-soup-encoding-index
118 (gnus-soup-area-encoding (nth 1 (car areas)))) ?n)
121 (gnus-soup-area-prefix (nth 1 (car areas)))))))
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)))))
131 ;; We now have a list of article numbers and corresponding
133 (setq useful-areas (nreverse useful-areas))
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.
142 (goto-char (point-max))
144 (number (car (nth 1 (car useful-areas)))))
145 (insert-buffer-substring
147 (gnus-soup-area-prefix
148 (nth 2 (car useful-areas)))))
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))
156 (setq useful-areas (cdr useful-areas)))
159 (setq articles (car (car useful-areas))
160 useful-areas (cdr useful-areas))
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))
169 (setq articles (cdr articles))))
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)))))
177 (defun nnsoup-open-server (server &optional defs)
178 (nnsoup-set-variables)
179 (nnheader-init-server-buffer)
180 (if (equal server nnsoup-current-server)
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)))
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)))
195 (defun nnsoup-request-close ()
196 (nnsoup-write-replies)
197 (while nnsoup-buffers
198 (and (car nnsoup-buffers)
199 (buffer-name (car nnsoup-buffers))
200 (kill-buffer (car nnsoup-buffers)))
201 (setq nnsoup-buffers (cdr nnsoup-buffers)))
202 (setq nnsoup-group-alist nil
203 nnsoup-current-group nil
204 nnsoup-current-server nil
205 nnsoup-server-alist nil)
208 (defun nnsoup-close-server (&optional server)
211 (defun nnsoup-server-opened (&optional server)
212 (and (equal server nnsoup-current-server)
214 (buffer-name nntp-server-buffer)))
216 (defun nnsoup-status-message (&optional server)
217 nnsoup-status-string)
219 (defun nnsoup-request-article (id &optional newsgroup server buffer)
220 (nnsoup-possibly-change-group newsgroup)
221 (let ((buffer (or buffer nntp-server-buffer)))
227 (insert-buffer-substring
228 (nnsoup-narrow-to-article id))
231 (defun nnsoup-request-group (group &optional server dont-check)
232 (nnsoup-possibly-change-group group)
235 (let ((area (cdr (assoc group nnsoup-group-alist)))
238 (set-buffer nntp-server-buffer)
240 (setq min (car (car (car area))))
242 (setq area (cdr area)))
243 (setq max (cdr (car (car area))))
244 (insert (format "211 %d %d %d %s\n"
245 (max (1+ (- max min)) 0) min max group)))))
248 (defun nnsoup-close-group (group &optional server)
251 (defun nnsoup-request-list (&optional server)
252 (or nnsoup-group-alist (nnsoup-read-areas))
253 (nnsoup-unpack-packets)
255 (set-buffer nntp-server-buffer)
257 (let ((alist nnsoup-group-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)))
264 (setq areas (cdr areas)))
265 (cdr (car (car areas)))) min))
266 (setq alist (cdr alist)))
269 (defun nnsoup-request-newgroups (date &optional server)
270 (nnsoup-request-list))
272 (defun nnsoup-request-list-newsgroups (&optional server)
275 (defun nnsoup-request-post (&optional server)
276 (nnsoup-store-reply "news")
279 (defun nnsoup-request-mail ()
280 (nnsoup-store-reply "mail")
283 (defun nnsoup-request-post-buffer (post group &rest args)
284 (nnsoup-possibly-change-group group)
286 ;; Find out whether the source for this group is a mail or a news
287 ;; group and call the right function for getting a buffer.
288 (let ((enc (nth 1 (car (cdr (assoc nnsoup-current-group
289 nnsoup-group-alist))))))
291 (= (gnus-soup-encoding-kind (gnus-soup-area-encoding enc)) ?m))
292 'nnmail-request-post-buffer
293 'nntp-request-post-buffer))
297 ;;; Internal functions
299 (defun nnsoup-possibly-change-group (group &optional force)
301 (setq nnsoup-current-group group)
304 (defun nnsoup-read-active-file ()
305 (if (file-exists-p nnsoup-active-file)
307 (load nnsoup-active-file)
310 (defun nnsoup-write-active-file ()
312 (set-buffer (get-buffer-create " *nnsoup work*"))
313 (buffer-disable-undo (current-buffer))
315 (insert (format "(setq nnsoup-group-alist '%S)\n" nnsoup-group-alist))
316 (write-region (point-min) (point-max) nnsoup-active-file
318 (kill-buffer (current-buffer))))
320 (defun nnsoup-read-areas ()
322 (set-buffer nntp-server-buffer)
323 (let ((areas (gnus-soup-parse-areas (concat nnsoup-directory "AREAS")))
324 entry number area lnum)
325 ;; Go through all areas in the new AREAS file.
327 (setq area (car areas)
329 ;; Find the number of new articles in this area.
330 (setq number (nnsoup-number-of-articles area))
331 (if (not (setq entry (assoc (gnus-soup-area-name area)
332 nnsoup-group-alist)))
333 ;; If this is a new area (group), we just add this info to
335 (setq nnsoup-group-alist
336 (cons (list (gnus-soup-area-name area)
337 (list (cons 1 number) area))
339 ;; There are already articles in this group, so we add this
340 ;; info to the end of the entry.
341 (let ((e (cdr entry)))
344 (setcdr e (list (list (cons (setq lnum (1+ (cdr (car (car e)))))
348 (defun nnsoup-number-of-articles (area)
351 ;; If the number is in the area info, we just return it.
352 ((gnus-soup-area-number area)
353 (gnus-soup-area-number area))
354 ;; If there is an index file, we just count the lines.
355 ((/= (gnus-soup-encoding-index (gnus-soup-area-encoding area)) ?n)
356 (set-buffer (nnsoup-index-buffer (gnus-soup-area-prefix area)))
357 (count-lines (point-min) (point-max)))
358 ;; We do it the hard way - re-searching through the message
361 (set-buffer (nnsoup-message-buffer (gnus-soup-area-prefix area)))
362 (goto-char (point-min))
363 (let ((regexp (nnsoup-header (gnus-soup-encoding-format
364 (gnus-soup-area-encoding area))))
366 (while (re-search-forward regexp nil t)
370 (defun nnsoup-index-buffer (prefix &optional message)
371 (let* ((file (concat prefix (if message ".MSG" ".IDX")))
372 (buffer-name (concat " *nnsoup " file "*")))
373 (or (get-buffer buffer-name) ; File aready loaded.
374 (save-excursion ; Load the file.
375 (set-buffer (get-buffer-create buffer-name))
376 (setq nnsoup-buffers (cons (current-buffer) nnsoup-buffers))
377 (insert-file-contents (concat nnsoup-directory file))
380 (defun nnsoup-file (prefix &optional message)
381 (concat nnsoup-directory prefix (if message ".MSG" ".IDX")))
383 (defun nnsoup-message-buffer (prefix)
384 (nnsoup-index-buffer prefix 'msg))
386 (defun nnsoup-unpack-packets ()
387 (let ((packets (directory-files
388 nnsoup-packet-directory t nnsoup-packet-regexp))
391 (message (setq msg (format "nnsoup: unpacking %s..." (car packets))))
392 (gnus-soup-unpack-packet nnsoup-directory nnsoup-unpacker (car packets))
393 (delete-file (car packets))
395 (message "%sdone" msg)
396 (setq packets (cdr packets)))))
398 (defun nnsoup-narrow-to-article (article &optional area head)
399 (let* ((area (or area (nnsoup-article-to-area article nnsoup-current-group)))
400 (prefix (gnus-soup-area-prefix (nth 1 area)))
402 (setq msg-buf (nnsoup-index-buffer prefix 'msg))
405 ;; We use the index file to find out where the article begins and ends.
406 ((and (= (gnus-soup-encoding-index
407 (gnus-soup-area-encoding (nth 1 area)))
409 (file-exists-p (nnsoup-file prefix)))
410 (set-buffer (nnsoup-index-buffer prefix))
412 (goto-char (point-min))
413 (forward-line (- article (car (car area))))
414 (setq beg (read (current-buffer)))
416 (if (looking-at "[0-9]+")
418 (setq end (read (current-buffer)))
421 (let ((format (gnus-soup-encoding-format
422 (gnus-soup-area-encoding (nth 1 area)))))
424 (if (or (= format ?n) (= format ?m))
425 (setq end (progn (forward-line -2) (point))))))
426 (set-buffer msg-buf))
428 (narrow-to-region beg (or end (point-max))))
432 (goto-char (point-min))
433 (let ((header (nnsoup-header
434 (gnus-soup-encoding-format
435 (gnus-soup-area-encoding (nth 1 area))))))
436 (re-search-forward header nil t (- article (car (car area))))
439 (if (re-search-forward header nil t)
442 (goto-char (point-min))
447 (if (search-forward "\n\n" nil t)
452 (defun nnsoup-header (format)
455 "^#! *rnews +[0-9]+ *$")
457 (concat "^" rmail-unix-mail-delimiter))
461 (error "Unknown format: %c" format))))
463 (defun nnsoup-pack-replies ()
464 "Make an outbound package of SOUP replies."
466 (nnsoup-write-active-file)
467 (nnsoup-write-replies)
468 (gnus-soup-pack nnsoup-replies-directory nnsoup-packer))
470 (defun nnsoup-write-replies ()
471 (gnus-soup-write-replies nnsoup-replies-directory nnsoup-replies-list))
473 (defun nnsoup-article-to-area (article group)
474 (let ((areas (cdr (assoc group nnsoup-group-alist))))
475 (while (and areas (< (cdr (car (car areas))) article))
476 (setq areas (cdr areas)))
477 (and areas (car areas))))
479 (defun nnsoup-set-variables ()
480 (setq gnus-inews-article-function 'nnsoup-request-post)
481 (setq gnus-mail-send-method 'nnsoup-request-mail)
482 (setq send-mail-function 'nnsoup-request-mail))
484 (defun nnsoup-store-reply (kind)
485 ;; Mostly stolen from `sendmail.el'.
486 (let ((tembuf (generate-new-buffer " sendmail temp"))
487 (case-fold-search nil)
488 (mailbuf (current-buffer))
494 (insert-buffer-substring mailbuf)
495 (goto-char (point-max))
496 ;; require one newline at the end.
497 (or (= (preceding-char) ?\n)
499 ;; Change header-delimiter to be what sendmail expects.
500 (goto-char (point-min))
502 (concat "^" (regexp-quote mail-header-separator) "\n"))
505 (setq delimline (point-marker))
507 (expand-mail-aliases (point-min) delimline))
508 (goto-char (point-min))
509 ;; ignore any blank lines in the header
510 (while (and (re-search-forward "\n\n\n*" delimline t)
511 (< (point) delimline))
512 (replace-match "\n"))
513 (let ((case-fold-search t))
514 (goto-char (point-min))
515 ;; Find and handle any FCC fields.
516 (goto-char (point-min))
517 (if (re-search-forward "^FCC:" delimline t)
518 (mail-do-fcc delimline))
519 (goto-char (point-min))
520 ;; "S:" is an abbreviation for "Subject:".
521 (goto-char (point-min))
522 (if (re-search-forward "^S:" delimline t)
523 (replace-match "Subject:"))
524 ;; Don't send out a blank subject line
525 (goto-char (point-min))
526 (if (re-search-forward "^Subject:[ \t]*\n" delimline t)
528 ;; Insert an extra newline if we need it to work around
529 ;; Sun's bug that swallows newlines.
530 (goto-char (1+ delimline))
531 (if (eval mail-mailer-swallows-blank-line)
534 nnsoup-replies-directory
535 (nnsoup-kind-to-prefix kind) nil nnsoup-replies-format-type
536 nnsoup-replies-index-type)
537 (kill-buffer tembuf))))
539 (defun nnsoup-kind-to-prefix (kind)
540 (or nnsoup-replies-list
541 (setq nnsoup-replies-list
542 (gnus-soup-parse-replies
543 (concat nnsoup-replies-directory "REPLIES"))))
544 (let ((replies nnsoup-replies-list))
546 (not (string= kind (gnus-soup-reply-kind (car replies)))))
547 (setq replies (cdr replies)))
549 (gnus-soup-reply-prefix (car replies))
550 (setq nnsoup-replies-list
551 (cons (vector (gnus-soup-unique-prefix nnsoup-replies-directory)
554 nnsoup-replies-format-type
555 nnsoup-replies-index-type
556 (if (string= kind "news")
558 nnsoup-replies-list))
559 (gnus-soup-reply-prefix (car nnsoup-replies-list)))))
563 ;;; nnsoup.el ends here