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 ;; Kill all nnsoup buffers.
204 (while nnsoup-buffers
205 (setq buffer (cdr (pop nnsoup-buffers)))
208 (kill-buffer buffer))))
209 (setq nnsoup-group-alist nil
210 nnsoup-current-group nil
211 nnsoup-current-server nil
212 nnsoup-server-alist nil
213 nnsoup-replies-list nil)
216 (defun nnsoup-close-server (&optional server)
219 (defun nnsoup-server-opened (&optional server)
220 (and (equal server nnsoup-current-server)
222 (buffer-name nntp-server-buffer)))
224 (defun nnsoup-status-message (&optional server)
225 nnsoup-status-string)
227 (defun nnsoup-request-article (id &optional newsgroup server buffer)
228 (nnsoup-possibly-change-group newsgroup)
229 (let ((buffer (or buffer nntp-server-buffer)))
235 (insert-buffer-substring
236 (nnsoup-narrow-to-article id))
239 (defun nnsoup-request-group (group &optional server dont-check)
240 (nnsoup-possibly-change-group group)
243 (let ((area (cdr (assoc group nnsoup-group-alist)))
246 (set-buffer nntp-server-buffer)
248 (setq min (car (car (car area))))
250 (setq area (cdr area)))
251 (setq max (cdr (car (car area))))
252 (insert (format "211 %d %d %d %s\n"
253 (max (1+ (- max min)) 0) min max group)))))
256 (defun nnsoup-request-type (group &optional article)
257 (nnsoup-possibly-change-group group)
260 (let ((kind (gnus-soup-encoding-kind
261 (gnus-soup-area-encoding
262 (nth 1 (nnsoup-article-to-area
263 article nnsoup-current-group))))))
264 (cond ((= kind ?m) 'mail)
268 (defun nnsoup-close-group (group &optional server)
269 ;; Kill all nnsoup buffers.
270 (let ((buffers nnsoup-buffers)
273 (when (equal (car (setq elem (pop buffers))) group)
274 (setq nnsoup-buffers (delq elem nnsoup-buffers))
275 (and (cdr elem) (buffer-name (cdr elem))
276 (kill-buffer (cdr elem))))))
279 (defun nnsoup-request-list (&optional server)
281 (set-buffer nntp-server-buffer)
283 (let ((alist nnsoup-group-alist)
286 (setq min (car (car (nth 1 (car alist)))))
287 (insert (format "%s %d %d y\n" (car (car alist))
288 (let ((areas (car alist)))
290 (setq areas (cdr areas)))
291 (cdr (car (car areas)))) min))
292 (setq alist (cdr alist)))
295 (defun nnsoup-request-scan (group &optional server)
296 (or nnsoup-group-alist (nnsoup-read-areas))
297 (nnsoup-unpack-packets))
299 (defun nnsoup-request-newgroups (date &optional server)
300 (nnsoup-request-list))
302 (defun nnsoup-request-list-newsgroups (&optional server)
305 (defun nnsoup-request-post (&optional server)
306 (nnsoup-store-reply "news")
309 (defun nnsoup-request-mail ()
310 (nnsoup-store-reply "mail")
313 (defun nnsoup-request-expire-articles (articles group &optional server force)
314 (nnsoup-possibly-change-group group)
315 (let* ((total-infolist (assoc group nnsoup-group-alist))
316 (infolist (cdr total-infolist))
317 info range-list mod-time prefix)
319 (setq info (pop infolist)
320 range-list (gnus-uncompress-range (car info))
321 prefix (gnus-soup-area-prefix (nth 1 info)))
322 (when ;; All the articles in this file are marked for expiry.
323 (and (gnus-sublist-p articles range-list)
324 ;; This file is old enough.
325 (setq mod-time (nth 5 (nnsoup-file prefix)))
326 (nnmail-expired-article-p group mod-time force))
327 ;; Ok, we delete this file.
328 (when (condition-case nil
330 (delete-file (nnsoup-file prefix))
331 (delete-file (nnsoup-file prefix) t)
334 (setcdr total-infolist (delq info total-infolist))
335 (setq articles (gnus-sorted-complement articles range-list)))))
336 (nnsoup-write-active-file)
337 ;; Return the articles that weren't expired.
341 ;;; Internal functions
343 (defun nnsoup-possibly-change-group (group &optional force)
345 (setq nnsoup-current-group group)
348 (defun nnsoup-read-active-file ()
349 (if (file-exists-p nnsoup-active-file)
351 (load nnsoup-active-file)
354 (defun nnsoup-write-active-file ()
355 (when nnsoup-group-alist
357 (set-buffer (get-buffer-create " *nnsoup work*"))
358 (buffer-disable-undo (current-buffer))
360 (insert (format "(setq nnsoup-group-alist '%S)\n" nnsoup-group-alist))
361 (write-region (point-min) (point-max) nnsoup-active-file
363 (kill-buffer (current-buffer)))))
365 (defun nnsoup-read-areas ()
367 (set-buffer nntp-server-buffer)
368 (let ((areas (gnus-soup-parse-areas (concat nnsoup-directory "AREAS")))
369 entry number area lnum)
370 ;; Go through all areas in the new AREAS file.
372 (setq area (car areas)
374 ;; Find the number of new articles in this area.
375 (setq number (nnsoup-number-of-articles area))
376 (if (not (setq entry (assoc (gnus-soup-area-name area)
377 nnsoup-group-alist)))
378 ;; If this is a new area (group), we just add this info to
380 (setq nnsoup-group-alist
381 (cons (list (gnus-soup-area-name area)
382 (list (cons 1 number) area))
384 ;; There are already articles in this group, so we add this
385 ;; info to the end of the entry.
386 (let ((e (cdr entry)))
389 (setcdr e (list (list (cons (setq lnum (1+ (cdr (car (car e)))))
392 (nnsoup-write-active-file)))
394 (defun nnsoup-number-of-articles (area)
397 ;; If the number is in the area info, we just return it.
398 ((gnus-soup-area-number area)
399 (gnus-soup-area-number area))
400 ;; If there is an index file, we just count the lines.
401 ((/= (gnus-soup-encoding-index (gnus-soup-area-encoding area)) ?n)
402 (set-buffer (nnsoup-index-buffer (gnus-soup-area-prefix area)))
403 (count-lines (point-min) (point-max)))
404 ;; We do it the hard way - re-searching through the message
407 (set-buffer (nnsoup-message-buffer (gnus-soup-area-prefix area)))
408 (goto-char (point-min))
409 (let ((regexp (nnsoup-header (gnus-soup-encoding-format
410 (gnus-soup-area-encoding area))))
412 (while (re-search-forward regexp nil t)
416 (defun nnsoup-index-buffer (prefix &optional message)
417 (let* ((file (concat prefix (if message ".MSG" ".IDX")))
418 (buffer-name (concat " *nnsoup " file "*")))
419 (or (get-buffer buffer-name) ; File aready loaded.
420 (save-excursion ; Load the file.
421 (set-buffer (get-buffer-create buffer-name))
422 (buffer-disable-undo (current-buffer))
423 (push (cons nnsoup-current-group (current-buffer)) nnsoup-buffers)
424 (insert-file-contents (concat nnsoup-directory file))
427 (defun nnsoup-file (prefix &optional message)
428 (concat nnsoup-directory prefix (if message ".MSG" ".IDX")))
430 (defun nnsoup-message-buffer (prefix)
431 (nnsoup-index-buffer prefix 'msg))
433 (defun nnsoup-unpack-packets ()
434 (let ((packets (directory-files
435 nnsoup-packet-directory t nnsoup-packet-regexp))
438 (message (setq msg (format "nnsoup: unpacking %s..." (car packets))))
439 (gnus-soup-unpack-packet nnsoup-directory nnsoup-unpacker (car packets))
440 (delete-file (car packets))
442 (message "%sdone" msg)
443 (setq packets (cdr packets)))))
445 (defun nnsoup-narrow-to-article (article &optional area head)
446 (let* ((area (or area (nnsoup-article-to-area article nnsoup-current-group)))
447 (prefix (gnus-soup-area-prefix (nth 1 area)))
449 (setq msg-buf (nnsoup-index-buffer prefix 'msg))
452 ;; We use the index file to find out where the article begins and ends.
453 ((and (= (gnus-soup-encoding-index
454 (gnus-soup-area-encoding (nth 1 area)))
456 (file-exists-p (nnsoup-file prefix)))
457 (set-buffer (nnsoup-index-buffer prefix))
459 (goto-char (point-min))
460 (forward-line (- article (car (car area))))
461 (setq beg (read (current-buffer)))
463 (if (looking-at "[0-9]+")
465 (setq end (read (current-buffer)))
468 (let ((format (gnus-soup-encoding-format
469 (gnus-soup-area-encoding (nth 1 area)))))
471 (if (or (= format ?n) (= format ?m))
472 (setq end (progn (forward-line -1) (point))))))
473 (set-buffer msg-buf))
475 (narrow-to-region beg (or end (point-max))))
479 (goto-char (point-min))
480 (let ((header (nnsoup-header
481 (gnus-soup-encoding-format
482 (gnus-soup-area-encoding (nth 1 area))))))
483 (re-search-forward header nil t (- article (car (car area))))
486 (if (re-search-forward header nil t)
489 (goto-char (point-min))
494 (if (search-forward "\n\n" nil t)
499 (defun nnsoup-header (format)
502 "^#! *rnews +[0-9]+ *$")
504 (concat "^" rmail-unix-mail-delimiter))
508 (error "Unknown format: %c" format))))
511 (defun nnsoup-pack-replies ()
512 "Make an outbound package of SOUP replies."
514 ;; Write all data buffers.
515 (gnus-soup-save-areas)
516 ;; Write the active file.
517 (nnsoup-write-active-file)
518 ;; Write the REPLIES file.
519 (nnsoup-write-replies)
520 ;; Pack all these files into a SOUP packet.
521 (gnus-soup-pack nnsoup-replies-directory nnsoup-packer))
523 (defun nnsoup-write-replies ()
524 "Write the REPLIES file."
525 (when nnsoup-replies-list
526 (gnus-soup-write-replies nnsoup-replies-directory nnsoup-replies-list)
527 (setq nnsoup-replies-list nil)))
529 (defun nnsoup-article-to-area (article group)
530 "Return the area that ARTICLE in GROUP is located in."
531 (let ((areas (cdr (assoc group nnsoup-group-alist))))
532 (while (and areas (< (cdr (car (car areas))) article))
533 (setq areas (cdr areas)))
534 (and areas (car areas))))
537 (defun nnsoup-set-variables ()
538 "Use the SOUP methods for posting news and mailing mail."
540 (setq gnus-inews-article-function 'nnsoup-request-post)
541 (setq send-mail-function 'nnsoup-request-mail))
543 (defun nnsoup-store-reply (kind)
544 ;; Mostly stolen from `sendmail.el'.
545 (let ((tembuf (generate-new-buffer " sendmail temp"))
546 (case-fold-search nil)
547 (mailbuf (current-buffer))
552 (insert-buffer-substring mailbuf)
553 (goto-char (point-max))
554 ;; require one newline at the end.
555 (or (= (preceding-char) ?\n)
557 ;; Change header-delimiter to be what sendmail expects.
558 (goto-char (point-min))
560 (concat "^" (regexp-quote mail-header-separator) "\n"))
563 (setq delimline (point-marker))
564 (if mail-aliases (expand-mail-aliases (point-min) delimline))
565 (goto-char (point-min))
566 ;; ignore any blank lines in the header
567 (while (and (re-search-forward "\n\n\n*" delimline t)
568 (< (point) delimline))
569 (replace-match "\n"))
570 (let ((case-fold-search t))
571 (goto-char (point-min))
572 ;; Find and handle any FCC fields.
573 (goto-char (point-min))
574 (if (re-search-forward "^FCC:" delimline t)
575 (mail-do-fcc delimline))
576 (goto-char (point-min))
577 ;; "S:" is an abbreviation for "Subject:".
578 (goto-char (point-min))
579 (if (re-search-forward "^S:" delimline t)
580 (replace-match "Subject:"))
581 ;; Don't send out a blank subject line
582 (goto-char (point-min))
583 (if (re-search-forward "^Subject:[ \t]*\n" delimline t)
585 ;; Insert an extra newline if we need it to work around
586 ;; Sun's bug that swallows newlines.
587 (goto-char (1+ delimline))
588 (if (eval mail-mailer-swallows-blank-line)
592 nnsoup-replies-directory
593 (nnsoup-kind-to-prefix kind) nil nnsoup-replies-format-type
594 nnsoup-replies-index-type))
596 (when (and msg-buf (bufferp msg-buf))
599 (goto-char (point-min))
600 (while (re-search-forward "^#! *rnews" nil t)
602 (message "Stored %d messages" num)))
603 (kill-buffer tembuf))))
605 (defun nnsoup-kind-to-prefix (kind)
606 (unless nnsoup-replies-list
607 (setq nnsoup-replies-list
608 (gnus-soup-parse-replies
609 (concat nnsoup-replies-directory "REPLIES"))))
610 (let ((replies nnsoup-replies-list))
612 (not (string= kind (gnus-soup-reply-kind (car replies)))))
613 (setq replies (cdr replies)))
615 (gnus-soup-reply-prefix (car replies))
616 (setq nnsoup-replies-list
617 (cons (vector (gnus-soup-unique-prefix nnsoup-replies-directory)
620 nnsoup-replies-format-type
621 nnsoup-replies-index-type
622 (if (string= kind "news")
624 nnsoup-replies-list))
625 (gnus-soup-reply-prefix (car nnsoup-replies-list)))))
627 (defun nnsoup-make-active ()
628 (let ((files (sort (directory-files nnsoup-directory t "IDX$")
630 (< (progn (string-match "/\\([0-9]+\\)\\." f1)
631 (string-to-int (substring
632 f1 (match-beginning 1)
634 (progn (string-match "/\\([0-9]+\\)\\." f2)
635 (string-to-int (substring
636 f2 (match-beginning 1)
638 active group lines ident elem min)
639 (set-buffer (get-buffer-create " *nnsoup work*"))
640 (buffer-disable-undo (current-buffer))
642 (message "Doing %s..." (car files))
644 (insert-file-contents (car files))
645 (goto-char (point-min))
647 (re-search-backward "[ \t]\\([^ ]+\\):[0-9]")
648 (setq group (buffer-substring (match-beginning 1) (match-end 1)))
649 (setq lines (count-lines (point-min) (point-max)))
650 (setq ident (progn (string-match
651 "/\\([0-9]+\\)\\." (car files))
653 (car files) (match-beginning 1)
655 (if (not (setq elem (assoc group active)))
656 (push (list group (list (cons 1 lines)
657 (vector ident group "ncm" "" lines)))
659 (setcdr elem (cons (list (cons (setq min (1+ (cdr (car (car
662 (vector ident group "ncm" "" lines))
664 (setq files (cdr files)))
665 (setq nnsoup-group-alist active)
667 (setcdr (car active) (nreverse (cdr (car active))))
668 (setq active (cdr active)))))
672 ;;; nnsoup.el ends here