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-close-group (group &optional server)
257 ;; Kill all nnsoup buffers.
258 (let ((buffers nnsoup-buffers)
261 (when (equal (car (setq elem (pop buffers))) group)
262 (setq nnsoup-buffers (delq elem nnsoup-buffers))
263 (and (cdr elem) (buffer-name (cdr elem))
264 (kill-buffer (cdr elem))))))
267 (defun nnsoup-request-list (&optional server)
269 (set-buffer nntp-server-buffer)
271 (let ((alist nnsoup-group-alist)
274 (setq min (car (car (nth 1 (car alist)))))
275 (insert (format "%s %d %d y\n" (car (car alist))
276 (let ((areas (car alist)))
278 (setq areas (cdr areas)))
279 (cdr (car (car areas)))) min))
280 (setq alist (cdr alist)))
283 (defun nnsoup-request-scan (group &optional server)
284 (or nnsoup-group-alist (nnsoup-read-areas))
285 (nnsoup-unpack-packets))
287 (defun nnsoup-request-newgroups (date &optional server)
288 (nnsoup-request-list))
290 (defun nnsoup-request-list-newsgroups (&optional server)
293 (defun nnsoup-request-post (&optional server)
294 (nnsoup-store-reply "news")
297 (defun nnsoup-request-mail ()
298 (nnsoup-store-reply "mail")
301 (defun nnsoup-request-expire-articles (articles group &optional server force)
302 (nnsoup-possibly-change-group group)
303 (let* ((days (or (and nnmail-expiry-wait-function
304 (funcall nnmail-expiry-wait-function group))
306 (total-infolist (assoc group nnsoup-group-alist))
307 (infolist (cdr total-infolist))
308 info range-list mod-time prefix)
310 (setq info (pop infolist)
311 range-list (gnus-uncompress-range (car info))
312 prefix (gnus-soup-area-prefix (nth 1 info)))
313 (when ;; All the articles in this file are marked for expiry.
314 (and (gnus-sublist-p articles range-list)
315 ;; This file is old enough. We have to check for
316 ;; `(0 0)', since that's what ange-ftp files reply with.
319 (setq mod-time (nth 5 (nnsoup-file prefix)))
321 (> (nnmail-days-between
322 (current-time-string)
323 (current-time-string mod-time))
325 ;; Ok, we delete this file.
326 (when (condition-case nil
328 (delete-file (nnsoup-file prefix))
329 (delete-file (nnsoup-file prefix) t)
332 (setcdr total-infolist (delq info total-infolist))
333 (setq articles (gnus-sorted-complement articles range-list)))))
334 (nnsoup-write-active-file)
335 ;; Return the articles that weren't expired.
339 ;;; Internal functions
341 (defun nnsoup-possibly-change-group (group &optional force)
343 (setq nnsoup-current-group group)
346 (defun nnsoup-read-active-file ()
347 (if (file-exists-p nnsoup-active-file)
349 (load nnsoup-active-file)
352 (defun nnsoup-write-active-file ()
353 (when nnsoup-group-alist
355 (set-buffer (get-buffer-create " *nnsoup work*"))
356 (buffer-disable-undo (current-buffer))
358 (insert (format "(setq nnsoup-group-alist '%S)\n" nnsoup-group-alist))
359 (write-region (point-min) (point-max) nnsoup-active-file
361 (kill-buffer (current-buffer)))))
363 (defun nnsoup-read-areas ()
365 (set-buffer nntp-server-buffer)
366 (let ((areas (gnus-soup-parse-areas (concat nnsoup-directory "AREAS")))
367 entry number area lnum)
368 ;; Go through all areas in the new AREAS file.
370 (setq area (car areas)
372 ;; Find the number of new articles in this area.
373 (setq number (nnsoup-number-of-articles area))
374 (if (not (setq entry (assoc (gnus-soup-area-name area)
375 nnsoup-group-alist)))
376 ;; If this is a new area (group), we just add this info to
378 (setq nnsoup-group-alist
379 (cons (list (gnus-soup-area-name area)
380 (list (cons 1 number) area))
382 ;; There are already articles in this group, so we add this
383 ;; info to the end of the entry.
384 (let ((e (cdr entry)))
387 (setcdr e (list (list (cons (setq lnum (1+ (cdr (car (car e)))))
390 (nnsoup-write-active-file)))
392 (defun nnsoup-number-of-articles (area)
395 ;; If the number is in the area info, we just return it.
396 ((gnus-soup-area-number area)
397 (gnus-soup-area-number area))
398 ;; If there is an index file, we just count the lines.
399 ((/= (gnus-soup-encoding-index (gnus-soup-area-encoding area)) ?n)
400 (set-buffer (nnsoup-index-buffer (gnus-soup-area-prefix area)))
401 (count-lines (point-min) (point-max)))
402 ;; We do it the hard way - re-searching through the message
405 (set-buffer (nnsoup-message-buffer (gnus-soup-area-prefix area)))
406 (goto-char (point-min))
407 (let ((regexp (nnsoup-header (gnus-soup-encoding-format
408 (gnus-soup-area-encoding area))))
410 (while (re-search-forward regexp nil t)
414 (defun nnsoup-index-buffer (prefix &optional message)
415 (let* ((file (concat prefix (if message ".MSG" ".IDX")))
416 (buffer-name (concat " *nnsoup " file "*")))
417 (or (get-buffer buffer-name) ; File aready loaded.
418 (save-excursion ; Load the file.
419 (set-buffer (get-buffer-create buffer-name))
420 (buffer-disable-undo (current-buffer))
421 (push (cons nnsoup-current-group (current-buffer)) nnsoup-buffers)
422 (insert-file-contents (concat nnsoup-directory file))
425 (defun nnsoup-file (prefix &optional message)
426 (concat nnsoup-directory prefix (if message ".MSG" ".IDX")))
428 (defun nnsoup-message-buffer (prefix)
429 (nnsoup-index-buffer prefix 'msg))
431 (defun nnsoup-unpack-packets ()
432 (let ((packets (directory-files
433 nnsoup-packet-directory t nnsoup-packet-regexp))
436 (message (setq msg (format "nnsoup: unpacking %s..." (car packets))))
437 (gnus-soup-unpack-packet nnsoup-directory nnsoup-unpacker (car packets))
438 (delete-file (car packets))
440 (message "%sdone" msg)
441 (setq packets (cdr packets)))))
443 (defun nnsoup-narrow-to-article (article &optional area head)
444 (let* ((area (or area (nnsoup-article-to-area article nnsoup-current-group)))
445 (prefix (gnus-soup-area-prefix (nth 1 area)))
447 (setq msg-buf (nnsoup-index-buffer prefix 'msg))
450 ;; We use the index file to find out where the article begins and ends.
451 ((and (= (gnus-soup-encoding-index
452 (gnus-soup-area-encoding (nth 1 area)))
454 (file-exists-p (nnsoup-file prefix)))
455 (set-buffer (nnsoup-index-buffer prefix))
457 (goto-char (point-min))
458 (forward-line (- article (car (car area))))
459 (setq beg (read (current-buffer)))
461 (if (looking-at "[0-9]+")
463 (setq end (read (current-buffer)))
466 (let ((format (gnus-soup-encoding-format
467 (gnus-soup-area-encoding (nth 1 area)))))
469 (if (or (= format ?n) (= format ?m))
470 (setq end (progn (forward-line -1) (point))))))
471 (set-buffer msg-buf))
473 (narrow-to-region beg (or end (point-max))))
477 (goto-char (point-min))
478 (let ((header (nnsoup-header
479 (gnus-soup-encoding-format
480 (gnus-soup-area-encoding (nth 1 area))))))
481 (re-search-forward header nil t (- article (car (car area))))
484 (if (re-search-forward header nil t)
487 (goto-char (point-min))
492 (if (search-forward "\n\n" nil t)
497 (defun nnsoup-header (format)
500 "^#! *rnews +[0-9]+ *$")
502 (concat "^" rmail-unix-mail-delimiter))
506 (error "Unknown format: %c" format))))
509 (defun nnsoup-pack-replies ()
510 "Make an outbound package of SOUP replies."
512 ;; Write all data buffers.
513 (gnus-soup-save-areas)
514 ;; Write the active file.
515 (nnsoup-write-active-file)
516 ;; Write the REPLIES file.
517 (nnsoup-write-replies)
518 ;; Pack all these files into a SOUP packet.
519 (gnus-soup-pack nnsoup-replies-directory nnsoup-packer))
521 (defun nnsoup-write-replies ()
522 "Write the REPLIES file."
523 (when nnsoup-replies-list
524 (gnus-soup-write-replies nnsoup-replies-directory nnsoup-replies-list)
525 (setq nnsoup-replies-list nil)))
527 (defun nnsoup-article-to-area (article group)
528 "Return the area that ARTICLE in GROUP is located in."
529 (let ((areas (cdr (assoc group nnsoup-group-alist))))
530 (while (and areas (< (cdr (car (car areas))) article))
531 (setq areas (cdr areas)))
532 (and areas (car areas))))
535 (defun nnsoup-set-variables ()
536 "Use the SOUP methods for posting news and mailing mail."
538 (setq gnus-inews-article-function 'nnsoup-request-post)
539 (setq send-mail-function 'nnsoup-request-mail))
541 (defun nnsoup-store-reply (kind)
542 ;; Mostly stolen from `sendmail.el'.
543 (let ((tembuf (generate-new-buffer " sendmail temp"))
544 (case-fold-search nil)
545 (mailbuf (current-buffer))
550 (insert-buffer-substring mailbuf)
551 (goto-char (point-max))
552 ;; require one newline at the end.
553 (or (= (preceding-char) ?\n)
555 ;; Change header-delimiter to be what sendmail expects.
556 (goto-char (point-min))
558 (concat "^" (regexp-quote mail-header-separator) "\n"))
561 (setq delimline (point-marker))
562 (if mail-aliases (expand-mail-aliases (point-min) delimline))
563 (goto-char (point-min))
564 ;; ignore any blank lines in the header
565 (while (and (re-search-forward "\n\n\n*" delimline t)
566 (< (point) delimline))
567 (replace-match "\n"))
568 (let ((case-fold-search t))
569 (goto-char (point-min))
570 ;; Find and handle any FCC fields.
571 (goto-char (point-min))
572 (if (re-search-forward "^FCC:" delimline t)
573 (mail-do-fcc delimline))
574 (goto-char (point-min))
575 ;; "S:" is an abbreviation for "Subject:".
576 (goto-char (point-min))
577 (if (re-search-forward "^S:" delimline t)
578 (replace-match "Subject:"))
579 ;; Don't send out a blank subject line
580 (goto-char (point-min))
581 (if (re-search-forward "^Subject:[ \t]*\n" delimline t)
583 ;; Insert an extra newline if we need it to work around
584 ;; Sun's bug that swallows newlines.
585 (goto-char (1+ delimline))
586 (if (eval mail-mailer-swallows-blank-line)
589 nnsoup-replies-directory
590 (nnsoup-kind-to-prefix kind) nil nnsoup-replies-format-type
591 nnsoup-replies-index-type)
592 (kill-buffer tembuf))))
594 (defun nnsoup-kind-to-prefix (kind)
595 (unless nnsoup-replies-list
596 (setq nnsoup-replies-list
597 (gnus-soup-parse-replies
598 (concat nnsoup-replies-directory "REPLIES"))))
599 (let ((replies nnsoup-replies-list))
601 (not (string= kind (gnus-soup-reply-kind (car replies)))))
602 (setq replies (cdr replies)))
604 (gnus-soup-reply-prefix (car replies))
605 (setq nnsoup-replies-list
606 (cons (vector (gnus-soup-unique-prefix nnsoup-replies-directory)
609 nnsoup-replies-format-type
610 nnsoup-replies-index-type
611 (if (string= kind "news")
613 nnsoup-replies-list))
614 (gnus-soup-reply-prefix (car nnsoup-replies-list)))))
616 (defun nnsoup-make-active ()
617 (let ((files (sort (directory-files nnsoup-directory t "IDX$")
619 (< (progn (string-match "/\\([0-9]+\\)\\." f1)
620 (string-to-int (substring
621 f1 (match-beginning 1)
623 (progn (string-match "/\\([0-9]+\\)\\." f2)
624 (string-to-int (substring
625 f2 (match-beginning 1)
627 active group lines ident elem min)
628 (set-buffer (get-buffer-create " *nnsoup work*"))
629 (buffer-disable-undo (current-buffer))
631 (message "Doing %s..." (car files))
633 (insert-file-contents (car files))
634 (goto-char (point-min))
636 (re-search-backward "[ \t]\\([^ ]+\\):[0-9]")
637 (setq group (buffer-substring (match-beginning 1) (match-end 1)))
638 (setq lines (count-lines (point-min) (point-max)))
639 (setq ident (progn (string-match
640 "/\\([0-9]+\\)\\." (car files))
642 (car files) (match-beginning 1)
644 (if (not (setq elem (assoc group active)))
645 (push (list group (list (cons 1 lines)
646 (vector ident group "ncm" "" lines)))
648 (setcdr elem (cons (list (cons (setq min (1+ (cdr (car (car
651 (vector ident group "ncm" "" lines))
653 (setq files (cdr files)))
654 (setq nnsoup-group-alist active)
656 (setcdr (car active) (nreverse (cdr (car active))))
657 (setq active (cdr active)))))
661 ;;; nnsoup.el ends here