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.
32 (eval-when-compile (require 'cl))
34 (defvar nnsoup-directory "~/SOUP/"
35 "*SOUP packet directory.")
37 (defvar nnsoup-replies-directory (concat nnsoup-directory "replies/")
38 "*Directory where outgoing packets will be composed.")
40 (defvar nnsoup-replies-format-type ?n
41 "*Format of the replies packages.")
43 (defvar nnsoup-replies-index-type ?n
44 "*Index type of the replies packages.")
46 (defvar nnsoup-active-file (concat nnsoup-directory "active")
49 (defvar nnsoup-packer "tar cf - %s | gzip > $HOME/Soupin%d.tgz"
50 "Format string command for packing a SOUP packet.
51 The SOUP files will be inserted where the %s is in the string.
52 This string MUST contain both %s and %d. The file number will be
53 inserted where %d appears.")
55 (defvar nnsoup-unpacker "gunzip -c %s | tar xvf -"
56 "*Format string command for unpacking a SOUP packet.
57 The SOUP packet file name will be inserted at the %s.")
59 (defvar nnsoup-packet-directory "~/"
60 "*Where nnsoup will look for incoming packets.")
62 (defvar nnsoup-packet-regexp "Soupout"
63 "*Regular expression matching SOUP packets in `nnsoup-packet-directory'.")
67 (defconst nnsoup-version "nnsoup 0.0"
70 (defvar nnsoup-status-string "")
71 (defvar nnsoup-group-alist nil)
72 (defvar nnsoup-replies-list nil)
73 (defvar nnsoup-buffers nil)
74 (defvar nnsoup-current-group nil)
80 (defvar nnsoup-current-server nil)
81 (defvar nnsoup-server-alist nil)
82 (defvar nnsoup-server-variables
84 (list 'nnsoup-directory nnsoup-directory)
85 (list 'nnsoup-active-file nnsoup-active-file)
86 '(nnsoup-status-string "")
87 '(nnsoup-group-alist nil)))
91 ;;; Interface functions.
93 (defun nnsoup-retrieve-headers (sequence &optional group server fetch-old)
94 (nnsoup-possibly-change-group group)
96 (set-buffer nntp-server-buffer)
98 (let ((areas (cdr (assoc nnsoup-current-group nnsoup-group-alist)))
101 useful-areas this-area-seq)
102 (if (stringp (car sequence))
103 ;; We don't support fetching by Message-ID.
105 ;; We go through all the areas and find which files the
106 ;; articles in SEQUENCE come from.
107 (while (and areas sequence)
108 ;; Peel off areas that are below sequence.
109 (while (and areas (< (cdr (car (car areas))) (car sequence)))
110 (setq areas (cdr areas)))
112 ;; This is a useful area.
113 (push (car areas) useful-areas)
114 (setq this-area-seq nil)
115 ;; We take note whether this MSG has a corresponding IDX
117 (when (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 assign 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 (push (car sequence) this-area-seq)
127 (setq 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.
141 ;; We have IDX files for all areas.
144 (goto-char (point-max))
146 (number (car (nth 1 (car useful-areas)))))
147 (insert-buffer-substring
149 (gnus-soup-area-prefix
150 (nth 2 (car useful-areas)))))
152 ;; We have to remove the index number entires and
153 ;; insert article numbers instead.
154 (while (looking-at "[0-9]+")
155 (replace-match (int-to-string number) t t)
158 (setq useful-areas (cdr useful-areas)))
162 (setq articles (car (car useful-areas))
163 useful-areas (cdr useful-areas))
165 (goto-char (point-max))
166 (insert (format "221 %d Article retrieved.\n" (car articles)))
167 (insert-buffer-substring
168 (nnsoup-narrow-to-article
169 (car articles) (cdr (car useful-areas)) 'head))
170 (goto-char (point-max))
172 (setq articles (cdr articles))))
174 ;; Fold continuation lines.
175 (goto-char (point-min))
176 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
177 (replace-match " " t t))
180 (defun nnsoup-open-server (server &optional defs)
181 (nnheader-init-server-buffer)
182 (if (equal server nnsoup-current-server)
184 (if nnsoup-current-server
185 (setq nnsoup-server-alist
186 (cons (list nnsoup-current-server
187 (nnheader-save-variables nnsoup-server-variables))
188 nnsoup-server-alist)))
189 (let ((state (assoc server nnsoup-server-alist)))
192 (nnheader-restore-variables (nth 1 state))
193 (setq nnsoup-server-alist (delq state nnsoup-server-alist)))
194 (nnheader-set-init-variables nnsoup-server-variables defs)))
195 (setq nnsoup-current-server server))
196 (nnsoup-read-active-file))
198 (defun nnsoup-request-close ()
199 (nnsoup-write-active-file)
200 (nnsoup-write-replies)
201 (gnus-soup-save-areas)
202 (while nnsoup-buffers
203 (and (car nnsoup-buffers)
204 (buffer-name (car nnsoup-buffers))
205 (kill-buffer (car nnsoup-buffers)))
206 (setq nnsoup-buffers (cdr nnsoup-buffers)))
207 (setq nnsoup-group-alist nil
208 nnsoup-current-group nil
209 nnsoup-current-server nil
210 nnsoup-server-alist nil
211 nnsoup-replies-list nil)
214 (defun nnsoup-close-server (&optional server)
217 (defun nnsoup-server-opened (&optional server)
218 (and (equal server nnsoup-current-server)
220 (buffer-name nntp-server-buffer)))
222 (defun nnsoup-status-message (&optional server)
223 nnsoup-status-string)
225 (defun nnsoup-request-article (id &optional newsgroup server buffer)
226 (nnsoup-possibly-change-group newsgroup)
227 (let ((buffer (or buffer nntp-server-buffer)))
233 (insert-buffer-substring
234 (nnsoup-narrow-to-article id))
237 (defun nnsoup-request-group (group &optional server dont-check)
238 (nnsoup-possibly-change-group group)
241 (let ((area (cdr (assoc group nnsoup-group-alist)))
244 (set-buffer nntp-server-buffer)
246 (setq min (car (car (car area))))
248 (setq area (cdr area)))
249 (setq max (cdr (car (car area))))
250 (insert (format "211 %d %d %d %s\n"
251 (max (1+ (- max min)) 0) min max group)))))
254 (defun nnsoup-close-group (group &optional server)
257 (defun nnsoup-request-list (&optional server)
259 (set-buffer nntp-server-buffer)
261 (let ((alist nnsoup-group-alist)
264 (setq min (car (car (nth 1 (car alist)))))
265 (insert (format "%s %d %d y\n" (car (car alist))
266 (let ((areas (car alist)))
268 (setq areas (cdr areas)))
269 (cdr (car (car areas)))) min))
270 (setq alist (cdr alist)))
273 (defun nnsoup-request-scan (group &optional server)
274 (or nnsoup-group-alist (nnsoup-read-areas))
275 (nnsoup-unpack-packets))
277 (defun nnsoup-request-newgroups (date &optional server)
278 (nnsoup-request-list))
280 (defun nnsoup-request-list-newsgroups (&optional server)
283 (defun nnsoup-request-post (&optional server)
284 (nnsoup-store-reply "news")
287 (defun nnsoup-request-mail ()
288 (nnsoup-store-reply "mail")
291 (defun nnsoup-request-expire-articles (articles group &optional server force)
292 (nnsoup-possibly-change-group group)
293 (let* ((days (or (and nnmail-expiry-wait-function
294 (funcall nnmail-expiry-wait-function group))
296 (total-infolist (assoc group nnsoup-group-alist))
297 (infolist (cdr total-infolist))
298 info range-list mod-time prefix)
300 (setq info (pop infolist)
301 range-list (gnus-uncompress-range (car info))
302 prefix (gnus-soup-area-prefix (nth 1 info)))
303 (when ;; All the articles in this file are marked for expiry.
304 (and (gnus-sublist-p articles range-list)
305 ;; This file is old enough. We have to check for
306 ;; `(0 0)', since that's what ange-ftp files reply with.
309 (setq mod-time (nth 5 (nnsoup-file prefix)))
311 (> (nnmail-days-between
312 (current-time-string)
313 (current-time-string mod-time))
315 ;; Ok, we delete this file.
316 (when (condition-case nil
318 (delete-file (nnsoup-file prefix))
319 (delete-file (nnsoup-file prefix) t)
322 (setcdr total-infolist (delq info total-infolist))
323 (setq articles (gnus-sorted-complement articles range-list)))))
324 (nnsoup-write-active-file)
325 ;; Return the articles that weren't expired.
329 ;;; Internal functions
331 (defun nnsoup-possibly-change-group (group &optional force)
333 (setq nnsoup-current-group group)
336 (defun nnsoup-read-active-file ()
337 (if (file-exists-p nnsoup-active-file)
339 (load nnsoup-active-file)
342 (defun nnsoup-write-active-file ()
343 (when nnsoup-group-alist
345 (set-buffer (get-buffer-create " *nnsoup work*"))
346 (buffer-disable-undo (current-buffer))
348 (insert (format "(setq nnsoup-group-alist '%S)\n" nnsoup-group-alist))
349 (write-region (point-min) (point-max) nnsoup-active-file
351 (kill-buffer (current-buffer)))))
353 (defun nnsoup-read-areas ()
355 (set-buffer nntp-server-buffer)
356 (let ((areas (gnus-soup-parse-areas (concat nnsoup-directory "AREAS")))
357 entry number area lnum)
358 ;; Go through all areas in the new AREAS file.
360 (setq area (car areas)
362 ;; Find the number of new articles in this area.
363 (setq number (nnsoup-number-of-articles area))
364 (if (not (setq entry (assoc (gnus-soup-area-name area)
365 nnsoup-group-alist)))
366 ;; If this is a new area (group), we just add this info to
368 (setq nnsoup-group-alist
369 (cons (list (gnus-soup-area-name area)
370 (list (cons 1 number) area))
372 ;; There are already articles in this group, so we add this
373 ;; info to the end of the entry.
374 (let ((e (cdr entry)))
377 (setcdr e (list (list (cons (setq lnum (1+ (cdr (car (car e)))))
380 (nnsoup-write-active-file)))
382 (defun nnsoup-number-of-articles (area)
385 ;; If the number is in the area info, we just return it.
386 ((gnus-soup-area-number area)
387 (gnus-soup-area-number area))
388 ;; If there is an index file, we just count the lines.
389 ((/= (gnus-soup-encoding-index (gnus-soup-area-encoding area)) ?n)
390 (set-buffer (nnsoup-index-buffer (gnus-soup-area-prefix area)))
391 (count-lines (point-min) (point-max)))
392 ;; We do it the hard way - re-searching through the message
395 (set-buffer (nnsoup-message-buffer (gnus-soup-area-prefix area)))
396 (goto-char (point-min))
397 (let ((regexp (nnsoup-header (gnus-soup-encoding-format
398 (gnus-soup-area-encoding area))))
400 (while (re-search-forward regexp nil t)
404 (defun nnsoup-index-buffer (prefix &optional message)
405 (let* ((file (concat prefix (if message ".MSG" ".IDX")))
406 (buffer-name (concat " *nnsoup " file "*")))
407 (or (get-buffer buffer-name) ; File aready loaded.
408 (save-excursion ; Load the file.
409 (set-buffer (get-buffer-create buffer-name))
410 (buffer-disable-undo (current-buffer))
411 (setq nnsoup-buffers (cons (current-buffer) nnsoup-buffers))
412 (insert-file-contents (concat nnsoup-directory file))
415 (defun nnsoup-file (prefix &optional message)
416 (concat nnsoup-directory prefix (if message ".MSG" ".IDX")))
418 (defun nnsoup-message-buffer (prefix)
419 (nnsoup-index-buffer prefix 'msg))
421 (defun nnsoup-unpack-packets ()
422 (let ((packets (directory-files
423 nnsoup-packet-directory t nnsoup-packet-regexp))
426 (message (setq msg (format "nnsoup: unpacking %s..." (car packets))))
427 (gnus-soup-unpack-packet nnsoup-directory nnsoup-unpacker (car packets))
428 (delete-file (car packets))
430 (message "%sdone" msg)
431 (setq packets (cdr packets)))))
433 (defun nnsoup-narrow-to-article (article &optional area head)
434 (let* ((area (or area (nnsoup-article-to-area article nnsoup-current-group)))
435 (prefix (gnus-soup-area-prefix (nth 1 area)))
437 (setq msg-buf (nnsoup-index-buffer prefix 'msg))
440 ;; We use the index file to find out where the article begins and ends.
441 ((and (= (gnus-soup-encoding-index
442 (gnus-soup-area-encoding (nth 1 area)))
444 (file-exists-p (nnsoup-file prefix)))
445 (set-buffer (nnsoup-index-buffer prefix))
447 (goto-char (point-min))
448 (forward-line (- article (car (car area))))
449 (setq beg (read (current-buffer)))
451 (if (looking-at "[0-9]+")
453 (setq end (read (current-buffer)))
456 (let ((format (gnus-soup-encoding-format
457 (gnus-soup-area-encoding (nth 1 area)))))
459 (if (or (= format ?n) (= format ?m))
460 (setq end (progn (forward-line -1) (point))))))
461 (set-buffer msg-buf))
463 (narrow-to-region beg (or end (point-max))))
467 (goto-char (point-min))
468 (let ((header (nnsoup-header
469 (gnus-soup-encoding-format
470 (gnus-soup-area-encoding (nth 1 area))))))
471 (re-search-forward header nil t (- article (car (car area))))
474 (if (re-search-forward header nil t)
477 (goto-char (point-min))
482 (if (search-forward "\n\n" nil t)
487 (defun nnsoup-header (format)
490 "^#! *rnews +[0-9]+ *$")
492 (concat "^" rmail-unix-mail-delimiter))
496 (error "Unknown format: %c" format))))
499 (defun nnsoup-pack-replies ()
500 "Make an outbound package of SOUP replies."
502 ;; Write all data buffers.
503 (gnus-soup-save-areas)
504 ;; Write the active file.
505 (nnsoup-write-active-file)
506 ;; Write the REPLIES file.
507 (nnsoup-write-replies)
508 ;; Pack all these files into a SOUP packet.
509 (gnus-soup-pack nnsoup-replies-directory nnsoup-packer))
511 (defun nnsoup-write-replies ()
512 "Write the REPLIES file."
513 (when nnsoup-replies-list
514 (gnus-soup-write-replies nnsoup-replies-directory nnsoup-replies-list)
515 (setq nnsoup-replies-list nil)))
517 (defun nnsoup-article-to-area (article group)
518 "Return the area that ARTICLE in GROUP is located in."
519 (let ((areas (cdr (assoc group nnsoup-group-alist))))
520 (while (and areas (< (cdr (car (car areas))) article))
521 (setq areas (cdr areas)))
522 (and areas (car areas))))
525 (defun nnsoup-set-variables ()
526 "Use the SOUP methods for posting news and mailing mail."
528 (setq gnus-inews-article-function 'nnsoup-request-post)
529 (setq send-mail-function 'nnsoup-request-mail))
531 (defun nnsoup-store-reply (kind)
532 ;; Mostly stolen from `sendmail.el'.
533 (let ((tembuf (generate-new-buffer " sendmail temp"))
534 (case-fold-search nil)
535 (mailbuf (current-buffer))
540 (insert-buffer-substring mailbuf)
541 (goto-char (point-max))
542 ;; require one newline at the end.
543 (or (= (preceding-char) ?\n)
545 ;; Change header-delimiter to be what sendmail expects.
546 (goto-char (point-min))
548 (concat "^" (regexp-quote mail-header-separator) "\n"))
551 (setq delimline (point-marker))
552 (if mail-aliases (expand-mail-aliases (point-min) delimline))
553 (goto-char (point-min))
554 ;; ignore any blank lines in the header
555 (while (and (re-search-forward "\n\n\n*" delimline t)
556 (< (point) delimline))
557 (replace-match "\n"))
558 (let ((case-fold-search t))
559 (goto-char (point-min))
560 ;; Find and handle any FCC fields.
561 (goto-char (point-min))
562 (if (re-search-forward "^FCC:" delimline t)
563 (mail-do-fcc delimline))
564 (goto-char (point-min))
565 ;; "S:" is an abbreviation for "Subject:".
566 (goto-char (point-min))
567 (if (re-search-forward "^S:" delimline t)
568 (replace-match "Subject:"))
569 ;; Don't send out a blank subject line
570 (goto-char (point-min))
571 (if (re-search-forward "^Subject:[ \t]*\n" delimline t)
573 ;; Insert an extra newline if we need it to work around
574 ;; Sun's bug that swallows newlines.
575 (goto-char (1+ delimline))
576 (if (eval mail-mailer-swallows-blank-line)
579 nnsoup-replies-directory
580 (nnsoup-kind-to-prefix kind) nil nnsoup-replies-format-type
581 nnsoup-replies-index-type)
582 (kill-buffer tembuf))))
584 (defun nnsoup-kind-to-prefix (kind)
585 (unless nnsoup-replies-list
586 (setq nnsoup-replies-list
587 (gnus-soup-parse-replies
588 (concat nnsoup-replies-directory "REPLIES"))))
589 (let ((replies nnsoup-replies-list))
591 (not (string= kind (gnus-soup-reply-kind (car replies)))))
592 (setq replies (cdr replies)))
594 (gnus-soup-reply-prefix (car replies))
595 (setq nnsoup-replies-list
596 (cons (vector (gnus-soup-unique-prefix nnsoup-replies-directory)
599 nnsoup-replies-format-type
600 nnsoup-replies-index-type
601 (if (string= kind "news")
603 nnsoup-replies-list))
604 (gnus-soup-reply-prefix (car nnsoup-replies-list)))))
606 (defun nnsoup-make-active ()
607 (let ((files (sort (directory-files nnsoup-directory t "IDX$")
609 (< (progn (string-match "/\\([0-9]+\\)\\." f1)
610 (string-to-int (substring
611 f1 (match-beginning 1)
613 (progn (string-match "/\\([0-9]+\\)\\." f2)
614 (string-to-int (substring
615 f2 (match-beginning 1)
617 active group lines ident elem min)
618 (set-buffer (get-buffer-create " *nnsoup work*"))
619 (buffer-disable-undo)
621 (message "Doing %s..." (car files))
623 (insert-file-contents (car files))
624 (goto-char (point-min))
626 (re-search-backward "[ \t]\\([^ ]+\\):[0-9]")
627 (setq group (buffer-substring (match-beginning 1) (match-end 1)))
628 (setq lines (count-lines (point-min) (point-max)))
629 (setq ident (progn (string-match
630 "/\\([0-9]+\\)\\." (car files))
632 (car files) (match-beginning 1)
634 (if (not (setq elem (assoc group active)))
635 (push (list group (list (cons 1 lines)
636 (vector ident group "ncm" "" lines)))
638 (setcdr elem (cons (list (cons (setq min (1+ (cdr (car (car
641 (vector ident group "ncm" "" lines))
643 (setq files (cdr files)))
644 (setq nnsoup-group-alist active)
646 (setcdr (car active) (nreverse (cdr (car active))))
647 (setq active (cdr active)))))
651 ;;; nnsoup.el ends here