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
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-tmp-directory "/tmp/"
38 "*Where nnsoup will store temporary files.")
40 (defvar nnsoup-replies-directory (concat nnsoup-directory "replies/")
41 "*Directory where outgoing packets will be composed.")
43 (defvar nnsoup-replies-format-type ?n
44 "*Format of the replies packages.")
46 (defvar nnsoup-replies-index-type ?n
47 "*Index type of the replies packages.")
49 (defvar nnsoup-active-file (concat nnsoup-directory "active")
52 (defvar nnsoup-packer "tar cf - %s | gzip > $HOME/Soupin%d.tgz"
53 "Format string command for packing a SOUP packet.
54 The SOUP files will be inserted where the %s is in the string.
55 This string MUST contain both %s and %d. The file number will be
56 inserted where %d appears.")
58 (defvar nnsoup-unpacker "gunzip -c %s | tar xvf -"
59 "*Format string command for unpacking a SOUP packet.
60 The SOUP packet file name will be inserted at the %s.")
62 (defvar nnsoup-packet-directory "~/"
63 "*Where nnsoup will look for incoming packets.")
65 (defvar nnsoup-packet-regexp "Soupout"
66 "*Regular expression matching SOUP packets in `nnsoup-packet-directory'.")
70 (defconst nnsoup-version "nnsoup 0.0"
73 (defvar nnsoup-status-string "")
74 (defvar nnsoup-group-alist nil)
75 (defvar nnsoup-current-prefix 0)
76 (defvar nnsoup-replies-list nil)
77 (defvar nnsoup-buffers nil)
78 (defvar nnsoup-current-group nil)
84 (defvar nnsoup-current-server nil)
85 (defvar nnsoup-server-alist nil)
86 (defvar nnsoup-server-variables
87 `((nnsoup-directory ,nnsoup-directory)
88 (nnsoup-active-file ,nnsoup-active-file)
89 (nnsoup-status-string "")
90 (nnsoup-current-prefix 0)
91 (nnsoup-group-alist nil)))
95 ;;; Interface functions.
97 (defun nnsoup-retrieve-headers (sequence &optional group server fetch-old)
98 (nnsoup-possibly-change-group group)
100 (set-buffer nntp-server-buffer)
102 (let ((areas (cddr (assoc nnsoup-current-group nnsoup-group-alist)))
105 useful-areas this-area-seq)
106 (if (stringp (car sequence))
107 ;; We don't support fetching by Message-ID.
109 ;; We go through all the areas and find which files the
110 ;; articles in SEQUENCE come from.
111 (while (and areas sequence)
112 ;; Peel off areas that are below sequence.
113 (while (and areas (< (cdr (car (car areas))) (car sequence)))
114 (setq areas (cdr areas)))
116 ;; This is a useful area.
117 (push (car areas) useful-areas)
118 (setq this-area-seq nil)
119 ;; We take note whether this MSG has a corresponding IDX
121 (when (or (= (gnus-soup-encoding-index
122 (gnus-soup-area-encoding (nth 1 (car areas)))) ?n)
125 (gnus-soup-area-prefix (nth 1 (car areas)))))))
127 ;; We assign the portion of `sequence' that is relevant to
128 ;; this MSG packet to this packet.
129 (while (and sequence (<= (car sequence) (cdr (car (car areas)))))
130 (push (car sequence) this-area-seq)
131 (setq sequence (cdr sequence)))
132 (setcar useful-areas (cons (nreverse this-area-seq)
133 (car useful-areas)))))
135 ;; We now have a list of article numbers and corresponding
137 (setq useful-areas (nreverse useful-areas))
139 ;; Two different approaches depending on whether all the MSG
140 ;; files have corresponding IDX files. If they all do, we
141 ;; simply return the relevant IDX files and let Gnus sort out
142 ;; what lines are relevant. If some of the IDX files are
143 ;; missing, we must return HEADs for all the articles.
145 ;; We have IDX files for all areas.
148 (goto-char (point-max))
150 (number (car (nth 1 (car useful-areas)))))
151 (insert-buffer-substring
153 (gnus-soup-area-prefix
154 (nth 2 (car useful-areas)))))
156 ;; We have to remove the index number entires and
157 ;; insert article numbers instead.
158 (while (looking-at "[0-9]+")
159 (replace-match (int-to-string number) t t)
162 (setq useful-areas (cdr useful-areas)))
166 (setq articles (car (car useful-areas))
167 useful-areas (cdr useful-areas))
169 (goto-char (point-max))
170 (insert (format "221 %d Article retrieved.\n" (car articles)))
171 (insert-buffer-substring
172 (nnsoup-narrow-to-article
173 (car articles) (cdr (car useful-areas)) 'head))
174 (goto-char (point-max))
176 (setq articles (cdr articles))))
178 ;; Fold continuation lines.
179 (goto-char (point-min))
180 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
181 (replace-match " " t t))
184 (defun nnsoup-open-server (server &optional defs)
185 (nnheader-init-server-buffer)
186 (if (equal server nnsoup-current-server)
188 (if nnsoup-current-server
189 (setq nnsoup-server-alist
190 (cons (list nnsoup-current-server
191 (nnheader-save-variables nnsoup-server-variables))
192 nnsoup-server-alist)))
193 (let ((state (assoc server nnsoup-server-alist)))
196 (nnheader-restore-variables (nth 1 state))
197 (setq nnsoup-server-alist (delq state nnsoup-server-alist)))
198 (nnheader-set-init-variables nnsoup-server-variables defs)))
199 (setq nnsoup-current-server server))
200 (nnsoup-read-active-file))
202 (defun nnsoup-request-close ()
203 (nnsoup-write-active-file)
204 (nnsoup-write-replies)
205 (gnus-soup-save-areas)
206 ;; Kill all nnsoup buffers.
208 (while nnsoup-buffers
209 (setq buffer (cdr (pop nnsoup-buffers)))
212 (kill-buffer buffer))))
213 (setq nnsoup-group-alist nil
214 nnsoup-current-group nil
215 nnsoup-current-server nil
216 nnsoup-server-alist nil
217 nnsoup-replies-list nil)
220 (defun nnsoup-close-server (&optional server)
223 (defun nnsoup-server-opened (&optional server)
224 (and (equal server nnsoup-current-server)
226 (buffer-name nntp-server-buffer)))
228 (defun nnsoup-status-message (&optional server)
229 nnsoup-status-string)
231 (defun nnsoup-request-article (id &optional newsgroup server buffer)
232 (nnsoup-possibly-change-group newsgroup)
233 (let ((buffer (or buffer nntp-server-buffer)))
239 (insert-buffer-substring
240 (nnsoup-narrow-to-article id))
243 (defun nnsoup-request-group (group &optional server dont-check)
244 (nnsoup-possibly-change-group group)
247 (let ((active (cadr (assoc group nnsoup-group-alist))))
249 (nnheader-report 'nnsoup "No such group: %s" group)
252 (max (1+ (- (cdr active) (car active))) 0)
253 (car active) (cdr active) group)))))
255 (defun nnsoup-request-type (group &optional article)
256 (nnsoup-possibly-change-group group)
259 (let ((kind (gnus-soup-encoding-kind
260 (gnus-soup-area-encoding
261 (nth 1 (nnsoup-article-to-area
262 article nnsoup-current-group))))))
263 (cond ((= kind ?m) 'mail)
267 (defun nnsoup-close-group (group &optional server)
268 ;; Kill all nnsoup buffers.
269 (let ((buffers nnsoup-buffers)
272 (when (equal (car (setq elem (pop buffers))) group)
273 (setq nnsoup-buffers (delq elem nnsoup-buffers))
274 (and (cdr elem) (buffer-name (cdr elem))
275 (kill-buffer (cdr elem))))))
278 (defun nnsoup-request-list (&optional server)
280 (set-buffer nntp-server-buffer)
282 (let ((alist nnsoup-group-alist)
284 (while (setq entry (pop alist))
285 (insert (format "%s %d %d y\n" (car entry)
286 (cdadr entry) (caadr entry))))
289 (defun nnsoup-request-scan (group &optional server)
290 (nnsoup-unpack-packets))
292 (defun nnsoup-request-newgroups (date &optional server)
293 (nnsoup-request-list))
295 (defun nnsoup-request-list-newsgroups (&optional server)
298 (defun nnsoup-request-post (&optional server)
299 (nnsoup-store-reply "news")
302 (defun nnsoup-request-mail ()
303 (nnsoup-store-reply "mail")
306 (defun nnsoup-request-expire-articles (articles group &optional server force)
307 (nnsoup-possibly-change-group group)
308 (let* ((total-infolist (assoc group nnsoup-group-alist))
309 (active (cadr total-infolist))
310 (infolist (cddr total-infolist))
311 info range-list mod-time prefix)
313 (setq info (pop infolist)
314 range-list (gnus-uncompress-range (car info))
315 prefix (gnus-soup-area-prefix (nth 1 info)))
316 (when ;; All the articles in this file are marked for expiry.
317 (and (or (setq mod-time (nth 5 (file-attributes
318 (nnsoup-file prefix))))
319 (setq mod-time (nth 5 (file-attributes
320 (nnsoup-file prefix t)))))
321 (gnus-sublist-p articles range-list)
322 ;; This file is old enough.
323 (nnmail-expired-article-p group mod-time force))
324 ;; Ok, we delete this file.
325 (when (condition-case nil
328 5 "Deleting %s..." (nnsoup-file prefix))
329 (when (file-exists-p (nnsoup-file prefix))
330 (delete-file (nnsoup-file prefix)))
332 5 "Deleting %s..." (nnsoup-file prefix t))
333 (when (file-exists-p (nnsoup-file prefix t))
334 (delete-file (nnsoup-file prefix t)))
337 (setcdr (cdr total-infolist) (delq info (cddr total-infolist)))
338 (setq articles (gnus-sorted-complement articles range-list))))
340 (setcdr (cdr total-infolist) (delq info (cddr total-infolist)))))
341 (if (cddr total-infolist)
342 (setcar active (car (car (cdr (car (cdr (cdr total-infolist)))))))
343 (setcar active (1+ (cdr active))))
344 (nnsoup-write-active-file)
345 ;; Return the articles that weren't expired.
349 ;;; Internal functions
351 (defun nnsoup-possibly-change-group (group &optional force)
353 (setq nnsoup-current-group group)
356 (defun nnsoup-read-active-file ()
357 (setq nnsoup-group-alist)
358 (when (file-exists-p nnsoup-active-file)
360 (load nnsoup-active-file)
362 ;; Be backwards compatible.
363 (when (and nnsoup-group-alist
364 (not (atom (caadar nnsoup-group-alist))))
365 (let ((alist nnsoup-group-alist)
367 (while (setq e (cdr (setq entry (pop alist))))
372 (setcdr entry (cons (cons min max) (cdr entry))))))
375 (defun nnsoup-write-active-file ()
376 (when nnsoup-group-alist
378 (set-buffer (get-buffer-create " *nnsoup work*"))
379 (buffer-disable-undo (current-buffer))
381 (insert (format "(setq nnsoup-group-alist '%S)\n" nnsoup-group-alist))
382 (insert (format "(setq nnsoup-current-prefix %d)\n"
383 nnsoup-current-prefix))
384 (write-region (point-min) (point-max) nnsoup-active-file
386 (kill-buffer (current-buffer)))))
388 (defun nnsoup-next-prefix ()
389 "Return the next free prefix."
391 (while (or (file-exists-p
392 (nnsoup-file (setq prefix (int-to-string
393 nnsoup-current-prefix))))
394 (file-exists-p (nnsoup-file prefix t)))
395 (incf nnsoup-current-prefix))
396 (incf nnsoup-current-prefix)
399 (defun nnsoup-read-areas ()
401 (set-buffer nntp-server-buffer)
402 (let ((areas (gnus-soup-parse-areas (concat nnsoup-tmp-directory "AREAS")))
403 entry number area lnum cur-prefix file)
404 ;; Go through all areas in the new AREAS file.
405 (while (setq area (pop areas))
406 ;; Change the name to the permanent name and move the files.
407 (setq cur-prefix (nnsoup-next-prefix))
409 (setq file (concat nnsoup-tmp-directory
410 (gnus-soup-area-prefix area) ".IDX")))
411 (rename-file file (nnsoup-file cur-prefix)))
413 (setq file (concat nnsoup-tmp-directory
414 (gnus-soup-area-prefix area) ".MSG")))
415 (rename-file file (nnsoup-file cur-prefix t)))
416 (gnus-soup-set-area-prefix area cur-prefix)
417 ;; Find the number of new articles in this area.
418 (setq number (nnsoup-number-of-articles area))
419 (if (not (setq entry (assoc (gnus-soup-area-name area)
420 nnsoup-group-alist)))
421 ;; If this is a new area (group), we just add this info to
423 (push (list (gnus-soup-area-name area)
425 (list (cons 1 number) area))
427 ;; There are already articles in this group, so we add this
428 ;; info to the end of the entry.
429 (let ((e (cddr entry)))
432 (setcdr e (list (list (cons (setq lnum (1+ (cdr (car (car e)))))
435 (setcdr (cadr entry) (+ lnum number)))))
436 (nnsoup-write-active-file)))
438 (defun nnsoup-number-of-articles (area)
441 ;; If the number is in the area info, we just return it.
442 ((gnus-soup-area-number area)
443 (gnus-soup-area-number area))
444 ;; If there is an index file, we just count the lines.
445 ((/= (gnus-soup-encoding-index (gnus-soup-area-encoding area)) ?n)
446 (set-buffer (nnsoup-index-buffer (gnus-soup-area-prefix area)))
447 (count-lines (point-min) (point-max)))
448 ;; We do it the hard way - re-searching through the message
451 (set-buffer (nnsoup-message-buffer (gnus-soup-area-prefix area)))
452 (goto-char (point-min))
453 (let ((regexp (nnsoup-header (gnus-soup-encoding-format
454 (gnus-soup-area-encoding area))))
456 (while (re-search-forward regexp nil t)
460 (defun nnsoup-index-buffer (prefix &optional message)
461 (let* ((file (concat prefix (if message ".MSG" ".IDX")))
462 (buffer-name (concat " *nnsoup " file "*")))
463 (or (get-buffer buffer-name) ; File aready loaded.
464 (save-excursion ; Load the file.
465 (set-buffer (get-buffer-create buffer-name))
466 (buffer-disable-undo (current-buffer))
467 (push (cons nnsoup-current-group (current-buffer)) nnsoup-buffers)
468 (insert-file-contents (concat nnsoup-directory file))
471 (defun nnsoup-file (prefix &optional message)
473 (concat nnsoup-directory prefix (if message ".MSG" ".IDX"))))
475 (defun nnsoup-message-buffer (prefix)
476 (nnsoup-index-buffer prefix 'msg))
478 (defun nnsoup-unpack-packets ()
479 "Unpack all packets in `nnsoup-packet-directory'."
480 (let ((packets (directory-files
481 nnsoup-packet-directory t nnsoup-packet-regexp))
483 (while (setq packet (pop packets))
484 (message (setq msg (format "nnsoup: unpacking %s..." packet)))
485 (if (not (gnus-soup-unpack-packet
486 nnsoup-tmp-directory nnsoup-unpacker packet))
487 (message "Couldn't unpack %s" packet)
490 (message "%sdone" msg)))))
492 (defun nnsoup-narrow-to-article (article &optional area head)
493 (let* ((area (or area (nnsoup-article-to-area article nnsoup-current-group)))
494 (prefix (gnus-soup-area-prefix (nth 1 area)))
496 (setq msg-buf (nnsoup-index-buffer prefix 'msg))
499 ;; We use the index file to find out where the article begins and ends.
500 ((and (= (gnus-soup-encoding-index
501 (gnus-soup-area-encoding (nth 1 area)))
503 (file-exists-p (nnsoup-file prefix)))
504 (set-buffer (nnsoup-index-buffer prefix))
506 (goto-char (point-min))
507 (forward-line (- article (car (car area))))
508 (setq beg (read (current-buffer)))
510 (if (looking-at "[0-9]+")
512 (setq end (read (current-buffer)))
515 (let ((format (gnus-soup-encoding-format
516 (gnus-soup-area-encoding (nth 1 area)))))
518 (if (or (= format ?n) (= format ?m))
519 (setq end (progn (forward-line -1) (point))))))
520 (set-buffer msg-buf))
522 (narrow-to-region beg (or end (point-max))))
526 (goto-char (point-min))
527 (let ((header (nnsoup-header
528 (gnus-soup-encoding-format
529 (gnus-soup-area-encoding (nth 1 area))))))
530 (re-search-forward header nil t (- article (car (car area))))
533 (if (re-search-forward header nil t)
536 (goto-char (point-min))
541 (if (search-forward "\n\n" nil t)
546 (defun nnsoup-header (format)
549 "^#! *rnews +[0-9]+ *$")
551 (concat "^" rmail-unix-mail-delimiter))
555 (error "Unknown format: %c" format))))
558 (defun nnsoup-pack-replies ()
559 "Make an outbound package of SOUP replies."
561 ;; Write all data buffers.
562 (gnus-soup-save-areas)
563 ;; Write the active file.
564 (nnsoup-write-active-file)
565 ;; Write the REPLIES file.
566 (nnsoup-write-replies)
567 ;; Pack all these files into a SOUP packet.
568 (gnus-soup-pack nnsoup-replies-directory nnsoup-packer))
570 (defun nnsoup-write-replies ()
571 "Write the REPLIES file."
572 (when nnsoup-replies-list
573 (gnus-soup-write-replies nnsoup-replies-directory nnsoup-replies-list)
574 (setq nnsoup-replies-list nil)))
576 (defun nnsoup-article-to-area (article group)
577 "Return the area that ARTICLE in GROUP is located in."
578 (let ((areas (cddr (assoc group nnsoup-group-alist))))
579 (while (and areas (< (cdr (car (car areas))) article))
580 (setq areas (cdr areas)))
581 (and areas (car areas))))
584 (defun nnsoup-set-variables ()
585 "Use the SOUP methods for posting news and mailing mail."
587 (setq gnus-inews-article-function 'nnsoup-request-post)
588 (setq send-mail-function 'nnsoup-request-mail))
590 (defun nnsoup-store-reply (kind)
591 ;; Mostly stolen from `sendmail.el'.
592 (let ((tembuf (generate-new-buffer " sendmail temp"))
593 (case-fold-search nil)
594 (mailbuf (current-buffer))
599 (insert-buffer-substring mailbuf)
600 (goto-char (point-max))
601 ;; require one newline at the end.
602 (or (= (preceding-char) ?\n)
604 ;; Change header-delimiter to be what sendmail expects.
605 (goto-char (point-min))
606 (if (re-search-forward
607 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
609 (search-forward "\n\n" nil t))
611 (setq delimline (point-marker))
612 (if mail-aliases (expand-mail-aliases (point-min) delimline))
613 (goto-char (point-min))
614 ;; ignore any blank lines in the header
615 (while (and (re-search-forward "\n\n\n*" delimline t)
616 (< (point) delimline))
617 (replace-match "\n"))
618 (let ((case-fold-search t))
619 (goto-char (point-min))
620 ;; Find and handle any FCC fields.
621 (goto-char (point-min))
622 (if (re-search-forward "^FCC:" delimline t)
623 (mail-do-fcc delimline))
624 (goto-char (point-min))
625 ;; "S:" is an abbreviation for "Subject:".
626 (goto-char (point-min))
627 (if (re-search-forward "^S:" delimline t)
628 (replace-match "Subject:"))
629 ;; Don't send out a blank subject line
630 (goto-char (point-min))
631 (if (re-search-forward "^Subject:[ \t]*\n" delimline t)
633 ;; Insert an extra newline if we need it to work around
634 ;; Sun's bug that swallows newlines.
635 (goto-char (1+ delimline))
636 (if (eval mail-mailer-swallows-blank-line)
640 nnsoup-replies-directory
641 (nnsoup-kind-to-prefix kind) nil nnsoup-replies-format-type
642 nnsoup-replies-index-type))
644 (when (and msg-buf (bufferp msg-buf))
647 (goto-char (point-min))
648 (while (re-search-forward "^#! *rnews" nil t)
650 (message "Stored %d messages" num)))
651 (kill-buffer tembuf))))
653 (defun nnsoup-kind-to-prefix (kind)
654 (unless nnsoup-replies-list
655 (setq nnsoup-replies-list
656 (gnus-soup-parse-replies
657 (concat nnsoup-replies-directory "REPLIES"))))
658 (let ((replies nnsoup-replies-list))
660 (not (string= kind (gnus-soup-reply-kind (car replies)))))
661 (setq replies (cdr replies)))
663 (gnus-soup-reply-prefix (car replies))
664 (setq nnsoup-replies-list
665 (cons (vector (gnus-soup-unique-prefix nnsoup-replies-directory)
668 nnsoup-replies-format-type
669 nnsoup-replies-index-type
670 (if (string= kind "news")
672 nnsoup-replies-list))
673 (gnus-soup-reply-prefix (car nnsoup-replies-list)))))
675 (defun nnsoup-make-active ()
676 "(Re-)create the SOUP active file."
678 (let ((files (sort (directory-files nnsoup-directory t "IDX$")
680 (< (progn (string-match "/\\([0-9]+\\)\\." f1)
681 (string-to-int (match-string 1 f1)))
682 (progn (string-match "/\\([0-9]+\\)\\." f2)
683 (string-to-int (match-string 1 f2)))))))
684 active group lines ident elem min)
685 (set-buffer (get-buffer-create " *nnsoup work*"))
686 (buffer-disable-undo (current-buffer))
688 (message "Doing %s..." (car files))
690 (insert-file-contents (car files))
691 (goto-char (point-min))
693 (re-search-backward "[ \t]\\([^ ]+\\):[0-9]")
694 (setq group (buffer-substring (match-beginning 1) (match-end 1)))
695 (setq lines (count-lines (point-min) (point-max)))
696 (setq ident (progn (string-match
697 "/\\([0-9]+\\)\\." (car files))
699 (car files) (match-beginning 1)
701 (if (not (setq elem (assoc group active)))
702 (push (list group (cons 1 lines)
704 (vector ident group "ncm" "" lines)))
708 (list (cons (setq min (1+ (cdr (car (car
711 (vector ident group "ncm" "" lines))))
712 (setcdr (cadr elem) (+ min lines)))
713 (setq files (cdr files)))
715 (setq nnsoup-group-alist active)
717 (setcdr (car active) (nreverse (cdr (car active))))
718 (setq active (cdr active)))
719 (nnsoup-write-active-file)))
723 ;;; nnsoup.el ends here