1 ;;; nnsoup.el --- SOUP access for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
4 ;; Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
8 ;; Keywords: news, mail
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
36 (eval-when-compile (require 'cl))
40 (defvoo nnsoup-directory "~/SOUP/"
41 "*SOUP packet directory.")
43 (defvoo nnsoup-tmp-directory
44 (cond ((fboundp 'temp-directory) (temp-directory))
45 ((boundp 'temporary-file-directory) temporary-file-directory)
47 "*Where nnsoup will store temporary files.")
49 (defvoo nnsoup-replies-directory (expand-file-name "replies/" nnsoup-directory)
50 "*Directory where outgoing packets will be composed.")
52 (defvoo nnsoup-replies-format-type ?u ;; u is USENET news format.
53 "*Format of the replies packages.")
55 (defvoo nnsoup-replies-index-type ?n
56 "*Index type of the replies packages.")
58 (defvoo nnsoup-active-file (expand-file-name "active" nnsoup-directory)
61 (defvoo nnsoup-packer "tar cf - %s | gzip > $HOME/Soupin%d.tgz"
62 "Format string command for packing a SOUP packet.
63 The SOUP files will be inserted where the %s is in the string.
64 This string MUST contain both %s and %d. The file number will be
65 inserted where %d appears.")
67 (defvoo nnsoup-unpacker "gunzip -c %s | tar xvf -"
68 "*Format string command for unpacking a SOUP packet.
69 The SOUP packet file name will be inserted at the %s.")
71 (defvoo nnsoup-packet-directory "~/"
72 "*Where nnsoup will look for incoming packets.")
74 (defvoo nnsoup-packet-regexp "Soupout"
75 "*Regular expression matching SOUP packets in `nnsoup-packet-directory'.")
77 (defvoo nnsoup-always-save t
78 "If non nil commit the reply buffer on each message send.
79 This is necessary if using message mode outside Gnus with nnsoup as a
80 backend for the messages.")
84 (defconst nnsoup-version "nnsoup 0.0"
87 (defvoo nnsoup-status-string "")
88 (defvoo nnsoup-group-alist nil)
89 (defvoo nnsoup-current-prefix 0)
90 (defvoo nnsoup-replies-list nil)
91 (defvoo nnsoup-buffers nil)
92 (defvoo nnsoup-current-group nil)
93 (defvoo nnsoup-group-alist-touched nil)
94 (defvoo nnsoup-article-alist nil)
97 ;;; Interface functions.
99 (nnoo-define-basics nnsoup)
101 (deffoo nnsoup-retrieve-headers (sequence &optional group server fetch-old)
102 (nnsoup-possibly-change-group group)
104 (set-buffer nntp-server-buffer)
106 (let ((areas (cddr (assoc nnsoup-current-group nnsoup-group-alist)))
109 useful-areas this-area-seq msg-buf)
110 (if (stringp (car sequence))
111 ;; We don't support fetching by Message-ID.
113 ;; We go through all the areas and find which files the
114 ;; articles in SEQUENCE come from.
115 (while (and areas sequence)
116 ;; Peel off areas that are below sequence.
117 (while (and areas (< (cdaar areas) (car sequence)))
118 (setq areas (cdr areas)))
120 ;; This is a useful area.
121 (push (car areas) useful-areas)
122 (setq this-area-seq nil)
123 ;; We take note whether this MSG has a corresponding IDX
125 (when (or (= (gnus-soup-encoding-index
126 (gnus-soup-area-encoding (nth 1 (car areas)))) ?n)
129 (gnus-soup-area-prefix (nth 1 (car areas)))))))
131 ;; We assign the portion of `sequence' that is relevant to
132 ;; this MSG packet to this packet.
133 (while (and sequence (<= (car sequence) (cdaar areas)))
134 (push (car sequence) this-area-seq)
135 (setq sequence (cdr sequence)))
136 (setcar useful-areas (cons (nreverse this-area-seq)
137 (car useful-areas)))))
139 ;; We now have a list of article numbers and corresponding
141 (setq useful-areas (nreverse useful-areas))
143 ;; Two different approaches depending on whether all the MSG
144 ;; files have corresponding IDX files. If they all do, we
145 ;; simply return the relevant IDX files and let Gnus sort out
146 ;; what lines are relevant. If some of the IDX files are
147 ;; missing, we must return HEADs for all the articles.
149 ;; We have IDX files for all areas.
152 (goto-char (point-max))
154 (number (car (nth 1 (car useful-areas))))
155 (index-buffer (nnsoup-index-buffer
156 (gnus-soup-area-prefix
157 (nth 2 (car useful-areas))))))
159 (insert-buffer-substring index-buffer)
161 ;; We have to remove the index number entires and
162 ;; insert article numbers instead.
163 (while (looking-at "[0-9]+")
164 (replace-match (int-to-string number) t t)
167 (setq useful-areas (cdr useful-areas)))
171 (setq articles (caar useful-areas)
172 useful-areas (cdr useful-areas))
175 (nnsoup-narrow-to-article
176 (car articles) (cdar useful-areas) 'head))
177 (goto-char (point-max))
178 (insert (format "221 %d Article retrieved.\n" (car articles)))
179 (insert-buffer-substring msg-buf)
180 (goto-char (point-max))
182 (setq articles (cdr articles))))
184 (nnheader-fold-continuation-lines)
187 (deffoo nnsoup-open-server (server &optional defs)
188 (nnoo-change-server 'nnsoup server defs)
189 (when (not (file-exists-p nnsoup-directory))
191 (make-directory nnsoup-directory t)
194 ((not (file-exists-p nnsoup-directory))
195 (nnsoup-close-server)
196 (nnheader-report 'nnsoup "Couldn't create directory: %s" nnsoup-directory))
197 ((not (file-directory-p (file-truename nnsoup-directory)))
198 (nnsoup-close-server)
199 (nnheader-report 'nnsoup "Not a directory: %s" nnsoup-directory))
201 (nnsoup-read-active-file)
202 (nnheader-report 'nnsoup "Opened server %s using directory %s"
203 server nnsoup-directory)
206 (deffoo nnsoup-request-close ()
207 (nnsoup-write-active-file)
208 (nnsoup-write-replies)
209 (gnus-soup-save-areas)
210 ;; Kill all nnsoup buffers.
212 (while nnsoup-buffers
213 (setq buffer (cdr (pop nnsoup-buffers)))
216 (kill-buffer buffer))))
217 (setq nnsoup-group-alist nil
218 nnsoup-group-alist-touched nil
219 nnsoup-current-group nil
220 nnsoup-replies-list nil)
221 (nnoo-close-server 'nnoo)
224 (deffoo nnsoup-request-article (id &optional newsgroup server buffer)
225 (nnsoup-possibly-change-group newsgroup)
228 (set-buffer (or buffer nntp-server-buffer))
230 (when (and (not (stringp id))
231 (setq buf (nnsoup-narrow-to-article id)))
232 (insert-buffer-substring buf)
235 (deffoo nnsoup-request-group (group &optional server dont-check)
236 (nnsoup-possibly-change-group group)
239 (let ((active (cadr (assoc group nnsoup-group-alist))))
241 (nnheader-report 'nnsoup "No such group: %s" group)
244 (max (1+ (- (cdr active) (car active))) 0)
245 (car active) (cdr active) group)))))
247 (deffoo nnsoup-request-type (group &optional article)
248 (nnsoup-possibly-change-group group)
249 ;; Try to guess the type based on the first article in the group.
252 (cdaar (cddr (assoc group nnsoup-group-alist)))))
255 (let ((kind (gnus-soup-encoding-kind
256 (gnus-soup-area-encoding
257 (nth 1 (nnsoup-article-to-area
258 article nnsoup-current-group))))))
259 (cond ((= kind ?m) 'mail)
263 (deffoo nnsoup-close-group (group &optional server)
264 ;; Kill all nnsoup buffers.
265 (let ((buffers nnsoup-buffers)
268 (when (equal (car (setq elem (pop buffers))) group)
269 (setq nnsoup-buffers (delq elem nnsoup-buffers))
270 (and (cdr elem) (buffer-name (cdr elem))
271 (kill-buffer (cdr elem))))))
274 (deffoo nnsoup-request-list (&optional server)
276 (set-buffer nntp-server-buffer)
278 (unless nnsoup-group-alist
279 (nnsoup-read-active-file))
280 (let ((alist nnsoup-group-alist)
281 (standard-output (current-buffer))
283 (while (setq entry (pop alist))
284 (insert (car entry) " ")
285 (princ (cdadr entry))
287 (princ (caadr entry))
291 (deffoo nnsoup-request-scan (group &optional server)
292 (nnsoup-unpack-packets))
294 (deffoo nnsoup-request-newgroups (date &optional server)
295 (nnsoup-request-list))
297 (deffoo nnsoup-request-list-newsgroups (&optional server)
300 (deffoo nnsoup-request-post (&optional server)
301 (nnsoup-store-reply "news")
304 (deffoo nnsoup-request-mail (&optional server)
305 (nnsoup-store-reply "mail")
308 (deffoo nnsoup-request-expire-articles (articles group &optional server force)
309 (nnsoup-possibly-change-group group)
310 (let* ((total-infolist (assoc group nnsoup-group-alist))
311 (active (cadr total-infolist))
312 (infolist (cddr total-infolist))
313 info range-list mod-time prefix)
315 (setq info (pop infolist)
316 range-list (gnus-uncompress-range (car info))
317 prefix (gnus-soup-area-prefix (nth 1 info)))
318 (when;; All the articles in this file are marked for expiry.
319 (and (or (setq mod-time (nth 5 (file-attributes
320 (nnsoup-file prefix))))
321 (setq mod-time (nth 5 (file-attributes
322 (nnsoup-file prefix t)))))
323 (gnus-sublist-p articles range-list)
324 ;; This file is old enough.
325 (nnmail-expired-article-p group mod-time force))
326 ;; Ok, we delete this file.
329 5 "Deleting %s in group %s..." (nnsoup-file prefix)
331 (when (file-exists-p (nnsoup-file prefix))
332 (delete-file (nnsoup-file prefix)))
334 5 "Deleting %s in group %s..." (nnsoup-file prefix t)
336 (when (file-exists-p (nnsoup-file prefix t))
337 (delete-file (nnsoup-file prefix t)))
339 (setcdr (cdr total-infolist) (delq info (cddr total-infolist)))
340 (setq articles (gnus-sorted-complement articles range-list))))
342 (setcdr (cdr total-infolist) (delq info (cddr total-infolist)))))
343 (if (cddr total-infolist)
344 (setcar active (caaadr (cdr total-infolist)))
345 (setcar active (1+ (cdr active))))
346 (nnsoup-write-active-file t)
347 ;; Return the articles that weren't expired.
351 ;;; Internal functions
353 (defun nnsoup-possibly-change-group (group &optional force)
355 (not (equal nnsoup-current-group group)))
356 (setq nnsoup-article-alist nil)
357 (setq nnsoup-current-group group))
360 (defun nnsoup-read-active-file ()
361 (setq nnsoup-group-alist nil)
362 (when (file-exists-p nnsoup-active-file)
364 (load nnsoup-active-file t t t))
365 ;; Be backwards compatible.
366 (when (and nnsoup-group-alist
367 (not (atom (caadar nnsoup-group-alist))))
368 (let ((alist nnsoup-group-alist)
370 (while (setq e (cdr (setq entry (pop alist))))
375 (setcdr entry (cons (cons min max) (cdr entry)))))
376 (setq nnsoup-group-alist-touched t))
379 (defun nnsoup-write-active-file (&optional force)
380 (when (and nnsoup-group-alist
382 nnsoup-group-alist-touched))
383 (setq nnsoup-group-alist-touched nil)
384 (with-temp-file nnsoup-active-file
385 (gnus-prin1 `(setq nnsoup-group-alist ',nnsoup-group-alist))
387 (gnus-prin1 `(setq nnsoup-current-prefix ,nnsoup-current-prefix))
390 (defun nnsoup-next-prefix ()
391 "Return the next free prefix."
393 (while (or (file-exists-p
394 (nnsoup-file (setq prefix (int-to-string
395 nnsoup-current-prefix))))
396 (file-exists-p (nnsoup-file prefix t)))
397 (incf nnsoup-current-prefix))
398 (incf nnsoup-current-prefix)
401 (defun nnsoup-file-name (dir file)
402 "Return the full path of FILE (in any case) in DIR."
403 (let* ((case-fold-search t)
404 (files (directory-files dir t))
405 (regexp (concat (regexp-quote file) "$")))
409 (if (string-match regexp file)
414 (defun nnsoup-read-areas ()
415 (let ((areas-file (nnsoup-file-name nnsoup-tmp-directory "areas")))
418 (set-buffer nntp-server-buffer)
419 (let ((areas (gnus-soup-parse-areas areas-file))
420 entry number area lnum cur-prefix file)
421 ;; Go through all areas in the new AREAS file.
422 (while (setq area (pop areas))
423 ;; Change the name to the permanent name and move the files.
424 (setq cur-prefix (nnsoup-next-prefix))
425 (nnheader-message 5 "Incorporating file %s..." cur-prefix)
429 (concat (gnus-soup-area-prefix area) ".IDX")
430 nnsoup-tmp-directory)))
431 (rename-file file (nnsoup-file cur-prefix)))
433 (setq file (expand-file-name
434 (concat (gnus-soup-area-prefix area) ".MSG")
435 nnsoup-tmp-directory)))
436 (rename-file file (nnsoup-file cur-prefix t))
437 (gnus-soup-set-area-prefix area cur-prefix)
438 ;; Find the number of new articles in this area.
439 (setq number (nnsoup-number-of-articles area))
440 (if (not (setq entry (assoc (gnus-soup-area-name area)
441 nnsoup-group-alist)))
442 ;; If this is a new area (group), we just add this info to
444 (push (list (gnus-soup-area-name area)
446 (list (cons 1 number) area))
448 ;; There are already articles in this group, so we add this
449 ;; info to the end of the entry.
450 (nconc entry (list (list (cons (1+ (setq lnum (cdadr entry)))
453 (setcdr (cadr entry) (+ lnum number))))))
454 (nnsoup-write-active-file t)
455 (delete-file areas-file)))))
457 (defun nnsoup-number-of-articles (area)
460 ;; If the number is in the area info, we just return it.
461 ((gnus-soup-area-number area)
462 (gnus-soup-area-number area))
463 ;; If there is an index file, we just count the lines.
464 ((/= (gnus-soup-encoding-index (gnus-soup-area-encoding area)) ?n)
465 (set-buffer (nnsoup-index-buffer (gnus-soup-area-prefix area)))
466 (count-lines (point-min) (point-max)))
467 ;; We do it the hard way - re-searching through the message
470 (set-buffer (nnsoup-message-buffer (gnus-soup-area-prefix area)))
471 (unless (assoc (gnus-soup-area-prefix area) nnsoup-article-alist)
472 (nnsoup-dissect-buffer area))
473 (length (cdr (assoc (gnus-soup-area-prefix area)
474 nnsoup-article-alist)))))))
476 (defun nnsoup-dissect-buffer (area)
477 (let ((mbox-delim (concat "^" message-unix-mail-delimiter))
478 (format (gnus-soup-encoding-format (gnus-soup-area-encoding area)))
481 (goto-char (point-min))
483 ;; rnews batch format
485 (= format ?n)) ;; Gnus back compatibility.
486 (while (looking-at "^#! *rnews \\(+[0-9]+\\) *$")