1 ;;; nnsoup.el --- SOUP access for Gnus
2 ;; Copyright (C) 1995,96 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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
33 (eval-when-compile (require 'cl))
35 (defvar nnsoup-directory "~/SOUP/"
36 "*SOUP packet directory.")
38 (defvar nnsoup-tmp-directory "/tmp/"
39 "*Where nnsoup will store temporary files.")
41 (defvar nnsoup-replies-directory (concat nnsoup-directory "replies/")
42 "*Directory where outgoing packets will be composed.")
44 (defvar nnsoup-replies-format-type ?n
45 "*Format of the replies packages.")
47 (defvar nnsoup-replies-index-type ?n
48 "*Index type of the replies packages.")
50 (defvar nnsoup-active-file (concat nnsoup-directory "active")
53 (defvar nnsoup-packer "tar cf - %s | gzip > $HOME/Soupin%d.tgz"
54 "Format string command for packing a SOUP packet.
55 The SOUP files will be inserted where the %s is in the string.
56 This string MUST contain both %s and %d. The file number will be
57 inserted where %d appears.")
59 (defvar nnsoup-unpacker "gunzip -c %s | tar xvf -"
60 "*Format string command for unpacking a SOUP packet.
61 The SOUP packet file name will be inserted at the %s.")
63 (defvar nnsoup-packet-directory "~/"
64 "*Where nnsoup will look for incoming packets.")
66 (defvar nnsoup-packet-regexp "Soupout"
67 "*Regular expression matching SOUP packets in `nnsoup-packet-directory'.")
71 (defconst nnsoup-version "nnsoup 0.0"
74 (defvar nnsoup-status-string "")
75 (defvar nnsoup-group-alist nil)
76 (defvar nnsoup-current-prefix 0)
77 (defvar nnsoup-replies-list nil)
78 (defvar nnsoup-buffers nil)
79 (defvar nnsoup-current-group nil)
85 (defvar nnsoup-current-server nil)
86 (defvar nnsoup-server-alist nil)
87 (defvar nnsoup-server-variables
88 `((nnsoup-directory ,nnsoup-directory)
89 (nnsoup-active-file ,nnsoup-active-file)
90 (nnsoup-status-string "")
91 (nnsoup-current-prefix 0)
92 (nnsoup-group-alist nil)))
96 ;;; Interface functions.
98 (defun nnsoup-retrieve-headers (sequence &optional group server fetch-old)
99 (nnsoup-possibly-change-group group)
101 (set-buffer nntp-server-buffer)
103 (let ((areas (cddr (assoc nnsoup-current-group nnsoup-group-alist)))
106 useful-areas this-area-seq)
107 (if (stringp (car sequence))
108 ;; We don't support fetching by Message-ID.
110 ;; We go through all the areas and find which files the
111 ;; articles in SEQUENCE come from.
112 (while (and areas sequence)
113 ;; Peel off areas that are below sequence.
114 (while (and areas (< (cdr (car (car areas))) (car sequence)))
115 (setq areas (cdr areas)))
117 ;; This is a useful area.
118 (push (car areas) useful-areas)
119 (setq this-area-seq nil)
120 ;; We take note whether this MSG has a corresponding IDX
122 (when (or (= (gnus-soup-encoding-index
123 (gnus-soup-area-encoding (nth 1 (car areas)))) ?n)
126 (gnus-soup-area-prefix (nth 1 (car areas)))))))
128 ;; We assign the portion of `sequence' that is relevant to
129 ;; this MSG packet to this packet.
130 (while (and sequence (<= (car sequence) (cdr (car (car areas)))))
131 (push (car sequence) this-area-seq)
132 (setq sequence (cdr sequence)))
133 (setcar useful-areas (cons (nreverse this-area-seq)
134 (car useful-areas)))))
136 ;; We now have a list of article numbers and corresponding
138 (setq useful-areas (nreverse useful-areas))
140 ;; Two different approaches depending on whether all the MSG
141 ;; files have corresponding IDX files. If they all do, we
142 ;; simply return the relevant IDX files and let Gnus sort out
143 ;; what lines are relevant. If some of the IDX files are
144 ;; missing, we must return HEADs for all the articles.
146 ;; We have IDX files for all areas.
149 (goto-char (point-max))
151 (number (car (nth 1 (car useful-areas)))))
152 (insert-buffer-substring
154 (gnus-soup-area-prefix
155 (nth 2 (car useful-areas)))))
157 ;; We have to remove the index number entires and
158 ;; insert article numbers instead.
159 (while (looking-at "[0-9]+")
160 (replace-match (int-to-string number) t t)
163 (setq useful-areas (cdr useful-areas)))
167 (setq articles (car (car useful-areas))
168 useful-areas (cdr useful-areas))
170 (goto-char (point-max))
171 (insert (format "221 %d Article retrieved.\n" (car articles)))
172 (insert-buffer-substring
173 (nnsoup-narrow-to-article
174 (car articles) (cdr (car useful-areas)) 'head))
175 (goto-char (point-max))
177 (setq articles (cdr articles))))
179 ;; Fold continuation lines.
180 (goto-char (point-min))
181 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
182 (replace-match " " t t))
185 (defun nnsoup-open-server (server &optional defs)
186 (nnheader-init-server-buffer)
187 (if (equal server nnsoup-current-server)
189 (if nnsoup-current-server
190 (setq nnsoup-server-alist
191 (cons (list nnsoup-current-server
192 (nnheader-save-variables nnsoup-server-variables))
193 nnsoup-server-alist)))
194 (let ((state (assoc server nnsoup-server-alist)))
197 (nnheader-restore-variables (nth 1 state))
198 (setq nnsoup-server-alist (delq state nnsoup-server-alist)))
199 (nnheader-set-init-variables nnsoup-server-variables defs)))
200 (setq nnsoup-current-server server))
201 (nnsoup-read-active-file))
203 (defun nnsoup-request-close ()
204 (nnsoup-write-active-file)
205 (nnsoup-write-replies)
206 (gnus-soup-save-areas)
207 ;; Kill all nnsoup buffers.
209 (while nnsoup-buffers
210 (setq buffer (cdr (pop nnsoup-buffers)))
213 (kill-buffer buffer))))
214 (setq nnsoup-group-alist nil
215 nnsoup-current-group nil
216 nnsoup-current-server nil
217 nnsoup-server-alist nil
218 nnsoup-replies-list nil)
221 (defun nnsoup-close-server (&optional server)
224 (defun nnsoup-server-opened (&optional server)
225 (and (equal server nnsoup-current-server)
227 (buffer-name nntp-server-buffer)))
229 (defun nnsoup-status-message (&optional server)
230 nnsoup-status-string)
232 (defun nnsoup-request-article (id &optional newsgroup server buffer)
233 (nnsoup-possibly-change-group newsgroup)
234 (let ((buffer (or buffer nntp-server-buffer)))
240 (insert-buffer-substring
241 (nnsoup-narrow-to-article id))
244 (defun nnsoup-request-group (group &optional server dont-check)
245 (nnsoup-possibly-change-group group)
248 (let ((active (cadr (assoc group nnsoup-group-alist))))
250 (nnheader-report 'nnsoup "No such group: %s" group)
253 (max (1+ (- (cdr active) (car active))) 0)
254 (car active) (cdr active) 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)
285 (while (setq entry (pop alist))
286 (insert (format "%s %d %d y\n" (car entry)
287 (cdadr entry) (caadr entry))))
290 (defun nnsoup-request-scan (group &optional server)
291 (nnsoup-unpack-packets))
293 (defun nnsoup-request-newgroups (date &optional server)
294 (nnsoup-request-list))
296 (defun nnsoup-request-list-newsgroups (&optional server)
299 (defun nnsoup-request-post (&optional server)
300 (nnsoup-store-reply "news")
303 (defun nnsoup-request-mail ()
304 (nnsoup-store-reply "mail")
307 (defun nnsoup-request-expire-articles (articles group &optional server force)
308 (nnsoup-possibly-change-group group)
309 (let* ((total-infolist (assoc group nnsoup-group-alist))
310 (active (cadr total-infolist))
311 (infolist (cddr total-infolist))
312 info range-list mod-time prefix)
314 (setq info (pop infolist)
315 range-list (gnus-uncompress-range (car info))
316 prefix (gnus-soup-area-prefix (nth 1 info)))
317 (when ;; All the articles in this file are marked for expiry.
318 (and (or (setq mod-time (nth 5 (file-attributes
319 (nnsoup-file prefix))))
320 (setq mod-time (nth 5 (file-attributes
321 (nnsoup-file prefix t)))))
322 (gnus-sublist-p articles range-list)
323 ;; This file is old enough.
324 (nnmail-expired-article-p group mod-time force))
325 ;; Ok, we delete this file.
326 (when (condition-case nil
329 5 "Deleting %s..." (nnsoup-file prefix))
330 (when (file-exists-p (nnsoup-file prefix))
331 (delete-file (nnsoup-file prefix)))
333 5 "Deleting %s..." (nnsoup-file prefix t))
334 (when (file-exists-p (nnsoup-file prefix t))
335 (delete-file (nnsoup-file prefix t)))
338 (setcdr (cdr total-infolist) (delq info (cddr total-infolist)))
339 (setq articles (gnus-sorted-complement articles range-list))))
341 (setcdr (cdr total-infolist) (delq info (cddr total-infolist)))))
342 (if (cddr total-infolist)
343 (setcar active (car (car (car (cdr (cdr total-infolist))))))
344 (setcar active (1+ (cdr active))))
345 (nnsoup-write-active-file)
346 ;; Return the articles that weren't expired.
350 ;;; Internal functions
352 (defun nnsoup-possibly-change-group (group &optional force)
354 (setq nnsoup-current-group group)
357 (defun nnsoup-read-active-file ()
358 (setq nnsoup-group-alist)
359 (when (file-exists-p nnsoup-active-file)
361 (load nnsoup-active-file)
363 ;; Be backwards compatible.
364 (when (and nnsoup-group-alist
365 (not (atom (caadar nnsoup-group-alist))))
366 (let ((alist nnsoup-group-alist)
368 (while (setq e (cdr (setq entry (pop alist))))
373 (setcdr entry (cons (cons min max) (cdr entry))))))
376 (defun nnsoup-write-active-file ()
377 (when nnsoup-group-alist
379 (set-buffer (get-buffer-create " *nnsoup work*"))
380 (buffer-disable-undo (current-buffer))
382 (insert (format "(setq nnsoup-group-alist '%S)\n" nnsoup-group-alist))
383 (insert (format "(setq nnsoup-current-prefix %d)\n"
384 nnsoup-current-prefix))
385 (write-region (point-min) (point-max) nnsoup-active-file
387 (kill-buffer (current-buffer)))))
389 (defun nnsoup-next-prefix ()
390 "Return the next free prefix."
392 (while (or (file-exists-p
393 (nnsoup-file (setq prefix (int-to-string
394 nnsoup-current-prefix))))
395 (file-exists-p (nnsoup-file prefix t)))
396 (incf nnsoup-current-prefix))
397 (incf nnsoup-current-prefix)
400 (defun nnsoup-read-areas ()
402 (set-buffer nntp-server-buffer)
403 (let ((areas (gnus-soup-parse-areas (concat nnsoup-tmp-directory "AREAS")))
404 entry number area lnum cur-prefix file)
405 ;; Go through all areas in the new AREAS file.
406 (while (setq area (pop areas))
407 ;; Change the name to the permanent name and move the files.
408 (setq cur-prefix (nnsoup-next-prefix))
410 (setq file (concat nnsoup-tmp-directory
411 (gnus-soup-area-prefix area) ".IDX")))
412 (rename-file file (nnsoup-file cur-prefix)))
414 (setq file (concat nnsoup-tmp-directory
415 (gnus-soup-area-prefix area) ".MSG")))
416 (rename-file file (nnsoup-file cur-prefix t)))
417 (gnus-soup-set-area-prefix area cur-prefix)
418 ;; Find the number of new articles in this area.
419 (setq number (nnsoup-number-of-articles area))
420 (if (not (setq entry (assoc (gnus-soup-area-name area)
421 nnsoup-group-alist)))
422 ;; If this is a new area (group), we just add this info to
424 (push (list (gnus-soup-area-name area)
426 (list (cons 1 number) area))
428 ;; There are already articles in this group, so we add this
429 ;; info to the end of the entry.
430 (let ((e (cddr entry)))
433 (setcdr e (list (list (cons (setq lnum (1+ (cdr (car (car e)))))
436 (setcdr (cadr entry) (+ lnum number)))))
437 (nnsoup-write-active-file)))
439 (defun nnsoup-number-of-articles (area)
442 ;; If the number is in the area info, we just return it.
443 ((gnus-soup-area-number area)
444 (gnus-soup-area-number area))
445 ;; If there is an index file, we just count the lines.
446 ((/= (gnus-soup-encoding-index (gnus-soup-area-encoding area)) ?n)
447 (set-buffer (nnsoup-index-buffer (gnus-soup-area-prefix area)))
448 (count-lines (point-min) (point-max)))
449 ;; We do it the hard way - re-searching through the message
452 (set-buffer (nnsoup-message-buffer (gnus-soup-area-prefix area)))
453 (goto-char (point-min))
454 (let ((regexp (nnsoup-header (gnus-soup-encoding-format
455 (gnus-soup-area-encoding area))))
457 (while (re-search-forward regexp nil t)
461 (defun nnsoup-index-buffer (prefix &optional message)
462 (let* ((file (concat prefix (if message ".MSG" ".IDX")))
463 (buffer-name (concat " *nnsoup " file "*")))
464 (or (get-buffer buffer-name) ; File aready loaded.
465 (save-excursion ; Load the file.
466 (set-buffer (get-buffer-create buffer-name))
467 (buffer-disable-undo (current-buffer))
468 (push (cons nnsoup-current-group (current-buffer)) nnsoup-buffers)
469 (insert-file-contents (concat nnsoup-directory file))
472 (defun nnsoup-file (prefix &optional message)
474 (concat nnsoup-directory prefix (if message ".MSG" ".IDX"))))
476 (defun nnsoup-message-buffer (prefix)
477 (nnsoup-index-buffer prefix 'msg))
479 (defun nnsoup-unpack-packets ()
480 "Unpack all packets in `nnsoup-packet-directory'."
481 (let ((packets (directory-files
482 nnsoup-packet-directory t nnsoup-packet-regexp))
484 (while (setq packet (pop packets))
485 (message (setq msg (format "nnsoup: unpacking %s..." packet)))
486 (if (not (gnus-soup-unpack-packet
487 nnsoup-tmp-directory nnsoup-unpacker packet))
488 (message "Couldn't unpack %s" packet)
491 (message "%sdone" msg)))))
493 (defun nnsoup-narrow-to-article (article &optional area head)
494 (let* ((area (or area (nnsoup-article-to-area article nnsoup-current-group)))
495 (prefix (gnus-soup-area-prefix (nth 1 area)))
497 (setq msg-buf (nnsoup-index-buffer prefix 'msg))
500 ;; We use the index file to find out where the article begins and ends.
501 ((and (= (gnus-soup-encoding-index
502 (gnus-soup-area-encoding (nth 1 area)))
504 (file-exists-p (nnsoup-file prefix)))
505 (set-buffer (nnsoup-index-buffer prefix))
507 (goto-char (point-min))
508 (forward-line (- article (car (car area))))
509 (setq beg (read (current-buffer)))
511 (if (looking-at "[0-9]+")
513 (setq end (read (current-buffer)))
516 (let ((format (gnus-soup-encoding-format
517 (gnus-soup-area-encoding (nth 1 area)))))
519 (if (or (= format ?n) (= format ?m))
520 (setq end (progn (forward-line -1) (point))))))
521 (set-buffer msg-buf))
523 (narrow-to-region beg (or end (point-max))))
527 (goto-char (point-min))
528 (let ((header (nnsoup-header
529 (gnus-soup-encoding-format
530 (gnus-soup-area-encoding (nth 1 area))))))
531 (re-search-forward header nil t (- article (car (car area))))
534 (if (re-search-forward header nil t)
537 (goto-char (point-min))
542 (if (search-forward "\n\n" nil t)
547 (defun nnsoup-header (format)
550 "^#! *rnews +[0-9]+ *$")
552 (concat "^" rmail-unix-mail-delimiter))
556 (error "Unknown format: %c" format))))
559 (defun nnsoup-pack-replies ()
560 "Make an outbound package of SOUP replies."
562 ;; Write all data buffers.
563 (gnus-soup-save-areas)
564 ;; Write the active file.
565 (nnsoup-write-active-file)
566 ;; Write the REPLIES file.
567 (nnsoup-write-replies)
568 ;; Pack all these files into a SOUP packet.
569 (gnus-soup-pack nnsoup-replies-directory nnsoup-packer))
571 (defun nnsoup-write-replies ()
572 "Write the REPLIES file."
573 (when nnsoup-replies-list
574 (gnus-soup-write-replies nnsoup-replies-directory nnsoup-replies-list)
575 (setq nnsoup-replies-list nil)))
577 (defun nnsoup-article-to-area (article group)
578 "Return the area that ARTICLE in GROUP is located in."
579 (let ((areas (cddr (assoc group nnsoup-group-alist))))
580 (while (and areas (< (cdr (car (car areas))) article))
581 (setq areas (cdr areas)))
582 (and areas (car areas))))
585 (defun nnsoup-set-variables ()
586 "Use the SOUP methods for posting news and mailing mail."
588 (setq gnus-inews-article-function 'nnsoup-request-post)
589 (setq send-mail-function 'nnsoup-request-mail))
591 (defun nnsoup-store-reply (kind)
592 ;; Mostly stolen from `sendmail.el'.
593 (let ((tembuf (generate-new-buffer " sendmail temp"))
594 (case-fold-search nil)
595 (mailbuf (current-buffer))
600 (insert-buffer-substring mailbuf)
601 (goto-char (point-max))
602 ;; require one newline at the end.
603 (or (= (preceding-char) ?\n)
605 ;; Change header-delimiter to be what sendmail expects.
606 (goto-char (point-min))
607 (if (re-search-forward
608 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
610 (search-forward "\n\n" nil t))
612 (setq delimline (point-marker))
613 (if mail-aliases (expand-mail-aliases (point-min) delimline))
614 (goto-char (point-min))
615 ;; ignore any blank lines in the header
616 (while (and (re-search-forward "\n\n\n*" delimline t)
617 (< (point) delimline))
618 (replace-match "\n"))
619 (let ((case-fold-search t))
620 (goto-char (point-min))
621 ;; Find and handle any FCC fields.
622 (goto-char (point-min))
623 (if (re-search-forward "^FCC:" delimline t)
624 (mail-do-fcc delimline))
625 (goto-char (point-min))
626 ;; "S:" is an abbreviation for "Subject:".
627 (goto-char (point-min))
628 (if (re-search-forward "^S:" delimline t)
629 (replace-match "Subject:"))
630 ;; Don't send out a blank subject line
631 (goto-char (point-min))
632 (if (re-search-forward "^Subject:[ \t]*\n" delimline t)
634 ;; Insert an extra newline if we need it to work around
635 ;; Sun's bug that swallows newlines.
636 (goto-char (1+ delimline))
637 (if (eval mail-mailer-swallows-blank-line)
641 nnsoup-replies-directory
642 (nnsoup-kind-to-prefix kind) nil nnsoup-replies-format-type
643 nnsoup-replies-index-type))
645 (when (and msg-buf (bufferp msg-buf))
648 (goto-char (point-min))
649 (while (re-search-forward "^#! *rnews" nil t)
651 (message "Stored %d messages" num)))
652 (kill-buffer tembuf))))
654 (defun nnsoup-kind-to-prefix (kind)
655 (unless nnsoup-replies-list
656 (setq nnsoup-replies-list
657 (gnus-soup-parse-replies
658 (concat nnsoup-replies-directory "REPLIES"))))
659 (let ((replies nnsoup-replies-list))
661 (not (string= kind (gnus-soup-reply-kind (car replies)))))
662 (setq replies (cdr replies)))
664 (gnus-soup-reply-prefix (car replies))
665 (setq nnsoup-replies-list
666 (cons (vector (gnus-soup-unique-prefix nnsoup-replies-directory)
669 nnsoup-replies-format-type
670 nnsoup-replies-index-type
671 (if (string= kind "news")
673 nnsoup-replies-list))
674 (gnus-soup-reply-prefix (car nnsoup-replies-list)))))
676 (defun nnsoup-make-active ()
677 "(Re-)create the SOUP active file."
679 (let ((files (sort (directory-files nnsoup-directory t "IDX$")
681 (< (progn (string-match "/\\([0-9]+\\)\\." f1)
682 (string-to-int (match-string 1 f1)))
683 (progn (string-match "/\\([0-9]+\\)\\." f2)
684 (string-to-int (match-string 1 f2)))))))
685 active group lines ident elem min)
686 (set-buffer (get-buffer-create " *nnsoup work*"))
687 (buffer-disable-undo (current-buffer))
689 (message "Doing %s..." (car files))
691 (insert-file-contents (car files))
692 (goto-char (point-min))
694 (re-search-backward "[ \t]\\([^ ]+\\):[0-9]")
695 (setq group (buffer-substring (match-beginning 1) (match-end 1)))
696 (setq lines (count-lines (point-min) (point-max)))
697 (setq ident (progn (string-match
698 "/\\([0-9]+\\)\\." (car files))
700 (car files) (match-beginning 1)
702 (if (not (setq elem (assoc group active)))
703 (push (list group (cons 1 lines)
705 (vector ident group "ncm" "" lines)))
709 (list (cons (setq min (1+ (cdr (car (car
712 (vector ident group "ncm" "" lines))))
713 (setcdr (cadr elem) (+ min lines)))
714 (setq files (cdr files)))
716 (setq nnsoup-group-alist active)
718 (setcdr (car active) (nreverse (cdr (car active))))
719 (setq active (cdr active)))
720 (nnsoup-write-active-file)))
722 (defun nnsoup-delete-unreferenced-message-files ()
723 "Delete any *.MSG and *.IDX files that aren't known by nnsoup."
725 (let* ((known (apply 'nconc (mapcar
729 (gnus-soup-area-prefix (cadr area)))
731 nnsoup-group-alist)))
732 (regexp "\\.MSG$\\|\\.IDX$")
733 (files (directory-files nnsoup-directory nil regexp))
735 ;; Find all files that aren't known by nnsoup.
736 (while (setq file (pop files))
737 (string-match regexp file)
738 (unless (member (substring file 0 (match-beginning 0)) known)
739 (push file non-files)))
740 ;; Sort and delete the files.
741 (setq non-files (sort non-files 'string<))
742 (map-y-or-n-p "Delete file %s? "
743 (lambda (file) (delete-file (concat nnsoup-directory file)))
748 ;;; nnsoup.el ends here