1 ;;; gnus-soup.el --- SOUP packet writing support for Gnus
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4 ;; Author: Per Abrahamsen <abraham@iesd.auc.dk>
5 ;; Keywords: news, mail
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 ;; This file contain support for storing articles in SOUP format from
26 ;; within Gnus. Support for reading SOUP packets is provided in
29 ;; SOUP is a format for offline reading of news and mail. See the
30 ;; file `soup12.zip' in one of the Simtel mirrors
31 ;; (e.g. `ftp.funet.fi') for the specification of SOUP.
33 ;; Only a subset of the SOUP protocol is supported, and the minimal
34 ;; conformance requirements in the SOUP document is *not* meet.
35 ;; Most annoyingly, replying and posting are not supported.
38 ;; (require 'gnus-soup)
39 ;; in your `.emacs' file to enable the SOUP support.
41 ;; Type `V o s' to add articles to the SOUP packet.
42 ;; Use a prefix argument or the process mark to add multiple articles.
44 ;; The variable `gnus-soup-directory' should point to the directory
45 ;; where you want to store the SOUP component files. You must
46 ;; manually `zip' the directory to generate a conforming SOUP packet.
48 ;; Add `nnsoup' to `gnus-secondary-select-methods' in order to read a
49 ;; SOUP packet. The variable `nnmail-directory' should point to the
50 ;; directory containing the unziped SOUP packet.
52 ;; Check out `uqwk' or `yarn' for two alterative solutions to
53 ;; generating or reading SOUP packages respectively, they should both
54 ;; be available at a Simtel mirror near you. There are plenty of
55 ;; other SOUP-aware programs available as well, look in the group
56 ;; `alt.usenet.offline-reader' and its FAQ for more information.
58 ;; Since `gnus-soup.el' does not fulfill the minimal conformance
59 ;; requirements, expect some problems when using other SOUP packeges.
60 ;; More importantly, the author haven't tested any of them.
69 (defvar gnus-soup-directory "~/SoupBrew/"
70 "*Directory containing an unpacked SOUP packet.")
72 (defvar gnus-soup-replies-directory (concat gnus-soup-directory "SoupReplies/")
73 "*Directory where Gnus will do processing of replies.")
75 (defvar gnus-soup-prefix-file "gnus-prefix"
76 "*Name of the file where Gnus stores the last used prefix.")
78 (defvar gnus-soup-packer "tar cf - %s | gzip > $HOME/Soupout%d.tgz"
79 "Format string command for packing a SOUP packet.
80 The SOUP files will be inserted where the %s is in the string.
81 This string MUST contain both %s and %d. The file number will be
82 inserted where %d appears.")
84 (defvar gnus-soup-unpacker "gunzip -c %s | tar xvf -"
85 "*Format string command for unpacking a SOUP packet.
86 The SOUP packet file name will be inserted at the %s.")
88 (defvar gnus-soup-packet-directory "~/"
89 "*Where gnus-soup will look for REPLIES packets.")
91 (defvar gnus-soup-packet-regexp "Soupin"
92 "*Regular expression matching SOUP REPLIES packets in `gnus-soup-packet-directory'.")
94 ;;; Internal Variables:
96 (defvar gnus-soup-encoding-type ?n
98 `n' is news format, `m' is Unix mbox format, and `M' is MMDF mailbox
101 (defvar gnus-soup-index-type ?c
103 `n' means no index file and `c' means standard Cnews overview
106 (defvar gnus-soup-group-type ?u
107 "*Soup message area type.
108 `u' is unknown, `m' is private mail, and `n' is news.
109 Gnus will determine by itself what type to use in what group, so
110 setting this variable won't do much.")
112 (defvar gnus-soup-areas nil)
113 (defvar gnus-soup-last-prefix nil)
114 (defvar gnus-soup-prev-prefix nil)
115 (defvar gnus-soup-buffers nil)
119 (defmacro gnus-soup-area-prefix (area)
120 (` (aref (, area) 0)))
121 (defmacro gnus-soup-area-name (area)
122 (` (aref (, area) 1)))
123 (defmacro gnus-soup-area-encoding (area)
124 (` (aref (, area) 2)))
125 (defmacro gnus-soup-area-description (area)
126 (` (aref (, area) 3)))
127 (defmacro gnus-soup-area-number (area)
128 (` (aref (, area) 4)))
129 (defmacro gnus-soup-area-set-number (area value)
130 (` (aset (, area) 4 (, value))))
132 (defmacro gnus-soup-encoding-format (encoding)
133 (` (aref (, encoding) 0)))
134 (defmacro gnus-soup-encoding-index (encoding)
135 (` (aref (, encoding) 1)))
136 (defmacro gnus-soup-encoding-kind (encoding)
137 (` (aref (, encoding) 2)))
139 (defmacro gnus-soup-reply-prefix (reply)
140 (` (aref (, reply) 0)))
141 (defmacro gnus-soup-reply-kind (reply)
142 (` (aref (, reply) 1)))
143 (defmacro gnus-soup-reply-encoding (reply)
144 (` (aref (, reply) 2)))
148 (defun gnus-soup-send-replies ()
149 "Unpack and send all replies in the reply packet."
151 (let ((packets (directory-files
152 gnus-soup-packet-directory t gnus-soup-packet-regexp)))
154 (and (gnus-soup-send-packet (car packets))
155 (delete-file (car packets)))
156 (setq packets (cdr packets)))))
158 (defun gnus-soup-add-article (n)
159 "Add the current article to SOUP packet.
160 If N is a positive number, add the N next articles.
161 If N is a negative number, add the N previous articles.
162 If N is nil and any articles have been marked with the process mark,
163 move those articles instead."
165 (gnus-set-global-variables)
166 (let* ((articles (gnus-summary-work-articles n))
167 (tmp-buf (get-buffer-create "*soup work*"))
168 (area (gnus-soup-area gnus-newsgroup-name))
169 (prefix (gnus-soup-area-prefix area))
171 (buffer-disable-undo tmp-buf)
174 ;; Find the header of the article.
175 (set-buffer gnus-summary-buffer)
176 (setq headers (gnus-get-header-by-number (car articles)))
177 ;; Put the article in a buffer.
179 (gnus-request-article-this-buffer
180 (car articles) gnus-newsgroup-name)
181 (gnus-soup-store gnus-soup-directory prefix headers
182 gnus-soup-encoding-type
183 gnus-soup-index-type)
184 (gnus-soup-area-set-number area
185 (1+ (or (gnus-soup-area-number area) 0)))
186 ;; Mark article as read.
187 (set-buffer gnus-summary-buffer)
188 (gnus-summary-remove-process-mark (car articles))
189 (gnus-summary-mark-as-read (car articles) "F")
190 (setq articles (cdr articles)))
191 (kill-buffer tmp-buf))))
193 (defun gnus-soup-pack-packet ()
194 "Make a SOUP packet from the SOUP areas."
196 (gnus-soup-read-areas)
197 (gnus-soup-pack gnus-soup-directory gnus-soup-packer))
199 (defun gnus-group-brew-soup (n)
200 "Make a soup packet from the current group.
201 Uses the process/prefix convention."
203 (let ((groups (gnus-group-process-prefix n)))
205 (gnus-group-remove-mark (car groups))
206 (gnus-soup-group-brew (car groups))
207 (setq groups (cdr groups)))
208 (gnus-soup-save-areas)))
210 (defun gnus-brew-soup (&optional level)
211 "Go through all groups on LEVEL or less and make a soup packet."
213 (let ((level (or level gnus-level-subscribed))
214 (newsrc (cdr gnus-newsrc-alist)))
216 (and (<= (nth 1 (car newsrc)) level)
217 (gnus-soup-group-brew (car (car newsrc))))
218 (setq newsrc (cdr newsrc)))
219 (gnus-soup-save-areas)))
222 (defun gnus-batch-brew-soup ()
223 "Brew a SOUP packet from groups mention on the command line.
224 Will use the remaining command line arguments as regular expressions
225 for matching on group names.
227 For instance, if you want to brew on all the nnml groups, as well as
228 groups with \"emacs\" in the name, you could say something like:
230 $ emacs -batch -f gnus-batch-brew-soup ^nnml \".*emacs.*\""
234 ;;; Internal Functions:
236 ;; Store the current buffer.
237 (defun gnus-soup-store (directory prefix headers format index)
238 (add-hook 'gnus-exit-gnus-hook 'gnus-soup-save-areas)
239 ;; Create the directory, if needed.
240 (or (file-directory-p directory)
241 (gnus-make-directory directory))
242 (let* ((msg-buf (gnus-find-file-noselect
243 (concat directory prefix ".MSG")))
244 (idx-buf (if (= index ?n)
246 (gnus-find-file-noselect
247 (concat directory prefix ".IDX"))))
248 (article-buf (current-buffer))
249 from head-line beg type)
250 (setq gnus-soup-buffers (cons msg-buf gnus-soup-buffers))
251 (buffer-disable-undo msg-buf)
254 (setq gnus-soup-buffers (cons idx-buf gnus-soup-buffers))
255 (buffer-disable-undo idx-buf)))
257 ;; Make sure the last char in the buffer is a newline.
258 (goto-char (point-max))
259 (or (= (current-column) 0)
262 (goto-char (point-min))
264 (mail-strip-quoted-names
265 (or (mail-fetch-field "from")
266 (mail-fetch-field "really-from")
267 (mail-fetch-field "sender"))))
268 (goto-char (point-min))
269 ;; Depending on what encoding is supposed to be used, we make
273 ((= gnus-soup-encoding-type ?n)
274 (format "#! rnews %d\n" (buffer-size)))
275 ((= gnus-soup-encoding-type ?m)
276 (while (search-forward "\nFrom " nil t)
277 (replace-match "\n>From " t t))
278 (concat "From " (or from "unknown")
279 " " (current-time-string) "\n"))
280 ((= gnus-soup-encoding-type ?M)
282 (t (error "Unsupported type: %c" gnus-soup-encoding-type))))
283 ;; Insert the soup header and the article in the MSG buf.
285 (goto-char (point-max))
288 (insert-buffer-substring article-buf)
289 ;; Insert the index in the IDX buf.
292 (gnus-soup-insert-idx beg headers))
294 (error "Unknown index type: %c" type))))))
296 (defun gnus-soup-group-brew (group)
297 (let ((gnus-expert-user t)
298 (gnus-large-newsgroup nil))
299 (and (gnus-summary-read-group group)
300 (let ((gnus-newsgroup-processable
301 (gnus-sorted-complement
302 gnus-newsgroup-unreads
303 (append gnus-newsgroup-dormant gnus-newsgroup-marked))))
304 (gnus-soup-add-article nil)))
305 (gnus-summary-exit)))
307 (defun gnus-soup-insert-idx (offset header)
308 ;; [number subject from date id references chars lines xref]
309 (goto-char (point-max))
311 (format "%d\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t\n"
313 (or (header-subject header) "(none)")
314 (or (header-from header) "(nobody)")
315 (or (header-date header) "")
316 (or (header-id header)
317 (concat "soup-dummy-id-"
319 (lambda (time) (int-to-string time))
320 (current-time) "-")))
321 (or (header-references header) "")
322 (or (header-chars header) 0)
323 (or (header-lines header) "0")
324 (or (header-xref header) ""))))
326 (defun gnus-soup-save-areas ()
327 (gnus-soup-write-areas)
330 (while gnus-soup-buffers
331 (setq buf (car gnus-soup-buffers)
332 gnus-soup-buffers (cdr gnus-soup-buffers))
333 (if (not (buffer-name buf))
336 (and (buffer-modified-p) (save-buffer))
337 (kill-buffer (current-buffer)))))
338 (let ((prefix gnus-soup-last-prefix))
340 (gnus-set-work-buffer)
341 (insert (format "(setq gnus-soup-prev-prefix %d)\n"
343 (write-region (point-min) (point-max)
344 (concat (car (car prefix))
345 gnus-soup-prefix-file)
347 (setq prefix (cdr prefix))))))
349 (defun gnus-soup-pack (dir packer)
350 (let* ((files (mapconcat 'identity
351 '("AREAS" "*.MSG" "*.IDX" "INFO"
352 "LIST" "REPLIES" "COMMANDS" "ERRORS")
354 (packer (if (< (string-match "%s" packer)
355 (string-match "%d" packer))
357 (string-to-int (gnus-soup-unique-prefix dir)))
359 (string-to-int (gnus-soup-unique-prefix dir))
361 (dir (expand-file-name dir)))
362 (message "Packing %s..." packer)
363 (if (zerop (call-process "sh" nil nil nil "-c"
364 (concat "cd " dir " ; " packer)))
366 (call-process "sh" nil nil nil "-c"
367 (concat "cd " dir " ; rm " files))
368 (message "Packing...done" packer))
369 (error "Couldn't pack packet."))))
371 (defun gnus-soup-parse-areas (file)
372 "Parse soup area file FILE.
373 The result is a of vectors, each containing one entry from the AREA file.
374 The vector contain five strings,
375 [prefix name encoding description number]
376 though the two last may be nil if they are missing."
379 (set-buffer (gnus-find-file-noselect file 'force))
380 (buffer-disable-undo (current-buffer))
381 (goto-char (point-min))
384 (cons (vector (gnus-soup-field)
387 (and (eq (preceding-char) ?\t)
389 (and (eq (preceding-char) ?\t)
390 (string-to-int (gnus-soup-field))))
392 (if (eq (preceding-char) ?\t)
393 (beginning-of-line 2))))
396 (defun gnus-soup-parse-replies (file)
397 "Parse soup REPLIES file FILE.
398 The result is a of vectors, each containing one entry from the REPLIES
399 file. The vector contain three strings, [prefix name encoding]."
402 (set-buffer (gnus-find-file-noselect file))
403 (buffer-disable-undo (current-buffer))
404 (goto-char (point-min))
407 (cons (vector (gnus-soup-field) (gnus-soup-field)
410 (if (eq (preceding-char) ?\t)
411 (beginning-of-line 2))))
414 (defun gnus-soup-field ()
416 (buffer-substring (point) (progn (skip-chars-forward "^\t\n") (point)))
419 (defun gnus-soup-read-areas ()
421 (setq gnus-soup-areas
422 (gnus-soup-parse-areas (concat gnus-soup-directory "AREAS")))))
424 (defun gnus-soup-write-areas ()
425 (if (not gnus-soup-areas)
428 (set-buffer (gnus-find-file-noselect
429 (concat gnus-soup-directory "AREAS")))
431 (let ((areas gnus-soup-areas)
434 (setq area (car areas)
436 (insert (format "%s\t%s\t%s%s\n"
437 (gnus-soup-area-prefix area)
438 (gnus-soup-area-name area)
439 (gnus-soup-area-encoding area)
440 (if (or (gnus-soup-area-description area)
441 (gnus-soup-area-number area))
442 (concat "\t" (or (gnus-soup-area-description
445 (if (gnus-soup-area-number area)
448 (gnus-soup-area-number
451 (write-region (point-min) (point-max)
452 (concat gnus-soup-directory "AREAS"))
453 (set-buffer-modified-p nil)
454 (kill-buffer (current-buffer)))))
456 (defun gnus-soup-write-replies (dir areas)
458 (set-buffer (gnus-find-file-noselect (concat dir "REPLIES")))
462 (setq area (car areas)
464 (insert (format "%s\t%s\t%s\n"
465 (gnus-soup-reply-prefix area)
466 (gnus-soup-reply-kind area)
467 (gnus-soup-reply-encoding area)))))
468 (write-region (point-min) (point-max) (concat dir "REPLIES"))
469 (set-buffer-modified-p nil)
470 (kill-buffer (current-buffer))))
472 (defun gnus-soup-area (group)
473 (gnus-soup-read-areas)
474 (let ((areas gnus-soup-areas)
475 (real-group (gnus-group-real-name group))
478 (setq area (car areas)
480 (if (equal (gnus-soup-area-name area) real-group)
484 (vector (gnus-soup-unique-prefix)
487 gnus-soup-encoding-type
489 (if (gnus-member-of-valid 'mail group) ?m ?n))
491 gnus-soup-areas (cons result gnus-soup-areas)))
494 (defun gnus-soup-unique-prefix (&optional dir)
495 (let* ((dir (file-name-as-directory (or dir gnus-soup-directory)))
496 (entry (assoc dir gnus-soup-last-prefix))
497 gnus-soup-prev-prefix)
500 (and (file-exists-p (concat dir gnus-soup-prefix-file))
502 (load-file (concat dir gnus-soup-prefix-file))
504 (setq gnus-soup-last-prefix
505 (cons (setq entry (cons dir (or gnus-soup-prev-prefix 0)))
506 gnus-soup-last-prefix)))
507 (setcdr entry (1+ (cdr entry)))
508 (int-to-string (cdr entry))))
510 (defun gnus-soup-unpack-packet (dir unpacker packet)
511 (gnus-make-directory dir)
512 (message "Unpacking: %s" (format unpacker packet))
514 "sh" nil nil nil "-c"
515 (format "cd %s ; %s" (expand-file-name dir) (format unpacker packet)))
516 (message "Unpacking...done"))
518 (defun gnus-soup-send-packet (packet)
519 (gnus-soup-unpack-packet
520 gnus-soup-replies-directory gnus-soup-unpacker packet)
521 (let ((replies (gnus-soup-parse-replies
522 (concat gnus-soup-replies-directory "REPLIES"))))
525 (let* ((msg-file (concat gnus-soup-replies-directory
526 (gnus-soup-reply-prefix (car replies))
528 (msg-buf (and (file-exists-p msg-file)
529 (gnus-find-file-noselect msg-file)))
530 (tmp-buf (get-buffer-create " *soup send*"))
533 ((/= (gnus-soup-encoding-format
534 (gnus-soup-reply-encoding (car replies))) ?n)
535 (error "Unsupported encoding"))
539 (buffer-disable-undo msg-buf)
540 (buffer-disable-undo tmp-buf)
542 (goto-char (point-min))
544 (or (looking-at "#! *rnews +\\([0-9]+\\)")
545 (error "Bad header."))
548 end (+ (point) (string-to-int
550 (match-beginning 1) (match-end 1)))))
551 (switch-to-buffer tmp-buf)
553 (insert-buffer-substring msg-buf beg end)
554 (goto-char (point-min))
555 (search-forward "\n\n")
557 (insert mail-header-separator)
559 ((string= (gnus-soup-reply-kind (car replies)) "news")
560 (message "Sending news message to %s..."
561 (mail-fetch-field "newsgroups"))
563 (gnus-inews-article))
564 ((string= (gnus-soup-reply-kind (car replies)) "mail")
565 (message "Sending mail to %s..."
566 (mail-fetch-field "to"))
568 (gnus-mail-send-and-exit))
570 (error "Unknown reply kind")))
573 (delete-file (buffer-file-name))
574 (kill-buffer msg-buf)
575 (kill-buffer tmp-buf)
576 (message "Sent packet"))))
577 (setq replies (cdr replies)))
582 ;;; gnus-soup.el ends here