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 ;; Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
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.
26 ;; This file contain support for storing articles in SOUP format from
27 ;; within Gnus. Support for reading SOUP packets is provided in
30 ;; SOUP is a format for offline reading of news and mail. See the
31 ;; file `soup12.zip' in one of the Simtel mirrors
32 ;; (e.g. `ftp.funet.fi') for the specification of SOUP.
34 ;; Only a subset of the SOUP protocol is supported, and the minimal
35 ;; conformance requirements in the SOUP document is *not* meet.
36 ;; Most annoyingly, replying and posting are not supported.
39 ;; (require 'gnus-soup)
40 ;; in your `.emacs' file to enable the SOUP support.
42 ;; Type `V o s' to add articles to the SOUP packet.
43 ;; Use a prefix argument or the process mark to add multiple articles.
45 ;; The variable `gnus-soup-directory' should point to the directory
46 ;; where you want to store the SOUP component files. You must
47 ;; manually `zip' the directory to generate a conforming SOUP packet.
49 ;; Add `nnsoup' to `gnus-secondary-select-methods' in order to read a
50 ;; SOUP packet. The variable `nnmail-directory' should point to the
51 ;; directory containing the unziped SOUP packet.
53 ;; Check out `uqwk' or `yarn' for two alterative solutions to
54 ;; generating or reading SOUP packages respectively, they should both
55 ;; be available at a Simtel mirror near you. There are plenty of
56 ;; other SOUP-aware programs available as well, look in the group
57 ;; `alt.usenet.offline-reader' and its FAQ for more information.
59 ;; Since `gnus-soup.el' does not fulfill the minimal conformance
60 ;; requirements, expect some problems when using other SOUP packeges.
61 ;; More importantly, the author haven't tested any of them.
70 (defvar gnus-soup-directory "~/SoupBrew/"
71 "*Directory containing an unpacked SOUP packet.")
73 (defvar gnus-soup-replies-directory (concat gnus-soup-directory "SoupReplies/")
74 "*Directory where Gnus will do processing of replies.")
76 (defvar gnus-soup-prefix-file "gnus-prefix"
77 "*Name of the file where Gnus stores the last used prefix.")
79 (defvar gnus-soup-packer "tar cf - %s | gzip > $HOME/Soupout%d.tgz"
80 "Format string command for packing a SOUP packet.
81 The SOUP files will be inserted where the %s is in the string.
82 This string MUST contain both %s and %d. The file number will be
83 inserted where %d appears.")
85 (defvar gnus-soup-unpacker "gunzip -c %s | tar xvf -"
86 "*Format string command for unpacking a SOUP packet.
87 The SOUP packet file name will be inserted at the %s.")
89 (defvar gnus-soup-packet-directory "~/"
90 "*Where gnus-soup will look for REPLIES packets.")
92 (defvar gnus-soup-packet-regexp "Soupin"
93 "*Regular expression matching SOUP REPLIES packets in `gnus-soup-packet-directory'.")
95 ;;; Internal Variables:
97 (defvar gnus-soup-encoding-type ?n
99 `n' is news format, `m' is Unix mbox format, and `M' is MMDF mailbox
102 (defvar gnus-soup-index-type ?c
104 `n' means no index file and `c' means standard Cnews overview
107 (defvar gnus-soup-group-type ?u
108 "*Soup message area type.
109 `u' is unknown, `m' is private mail, and `n' is news.
110 Gnus will determine by itself what type to use in what group, so
111 setting this variable won't do much.")
113 (defvar gnus-soup-areas nil)
114 (defvar gnus-soup-last-prefix nil)
115 (defvar gnus-soup-prev-prefix nil)
116 (defvar gnus-soup-buffers nil)
120 (defmacro gnus-soup-area-prefix (area)
121 (` (aref (, area) 0)))
122 (defmacro gnus-soup-area-name (area)
123 (` (aref (, area) 1)))
124 (defmacro gnus-soup-area-encoding (area)
125 (` (aref (, area) 2)))
126 (defmacro gnus-soup-area-description (area)
127 (` (aref (, area) 3)))
128 (defmacro gnus-soup-area-number (area)
129 (` (aref (, area) 4)))
130 (defmacro gnus-soup-area-set-number (area value)
131 (` (aset (, area) 4 (, value))))
133 (defmacro gnus-soup-encoding-format (encoding)
134 (` (aref (, encoding) 0)))
135 (defmacro gnus-soup-encoding-index (encoding)
136 (` (aref (, encoding) 1)))
137 (defmacro gnus-soup-encoding-kind (encoding)
138 (` (aref (, encoding) 2)))
140 (defmacro gnus-soup-reply-prefix (reply)
141 (` (aref (, reply) 0)))
142 (defmacro gnus-soup-reply-kind (reply)
143 (` (aref (, reply) 1)))
144 (defmacro gnus-soup-reply-encoding (reply)
145 (` (aref (, reply) 2)))
149 (defun gnus-soup-send-replies ()
150 "Unpack and send all replies in the reply packet."
152 (let ((packets (directory-files
153 gnus-soup-packet-directory t gnus-soup-packet-regexp)))
155 (and (gnus-soup-send-packet (car packets))
156 (delete-file (car packets)))
157 (setq packets (cdr packets)))))
159 (defun gnus-soup-add-article (n)
160 "Add the current article to SOUP packet.
161 If N is a positive number, add the N next articles.
162 If N is a negative number, add the N previous articles.
163 If N is nil and any articles have been marked with the process mark,
164 move those articles instead."
166 (gnus-set-global-variables)
167 (let* ((articles (gnus-summary-work-articles n))
168 (tmp-buf (get-buffer-create "*soup work*"))
169 (area (gnus-soup-area gnus-newsgroup-name))
170 (prefix (gnus-soup-area-prefix area))
172 (buffer-disable-undo tmp-buf)
175 ;; Find the header of the article.
176 (set-buffer gnus-summary-buffer)
177 (setq headers (gnus-get-header-by-number (car articles)))
178 ;; Put the article in a buffer.
180 (gnus-request-article-this-buffer
181 (car articles) gnus-newsgroup-name)
182 (gnus-soup-store gnus-soup-directory prefix headers
183 gnus-soup-encoding-type
184 gnus-soup-index-type)
185 (gnus-soup-area-set-number area
186 (1+ (or (gnus-soup-area-number area) 0)))
187 ;; Mark article as read.
188 (set-buffer gnus-summary-buffer)
189 (gnus-summary-remove-process-mark (car articles))
190 (gnus-summary-mark-as-read (car articles) "F")
191 (setq articles (cdr articles)))
192 (kill-buffer tmp-buf))))
194 (defun gnus-soup-pack-packet ()
195 "Make a SOUP packet from the SOUP areas."
197 (gnus-soup-read-areas)
198 (gnus-soup-pack gnus-soup-directory gnus-soup-packer))
200 (defun gnus-group-brew-soup (n)
201 "Make a soup packet from the current group.
202 Uses the process/prefix convention."
204 (let ((groups (gnus-group-process-prefix n)))
206 (gnus-group-remove-mark (car groups))
207 (gnus-soup-group-brew (car groups))
208 (setq groups (cdr groups)))
209 (gnus-soup-save-areas)))
211 (defun gnus-brew-soup (&optional level)
212 "Go through all groups on LEVEL or less and make a soup packet."
214 (let ((level (or level gnus-level-subscribed))
215 (newsrc (cdr gnus-newsrc-alist)))
217 (and (<= (nth 1 (car newsrc)) level)
218 (gnus-soup-group-brew (car (car newsrc))))
219 (setq newsrc (cdr newsrc)))
220 (gnus-soup-save-areas)))
223 (defun gnus-batch-brew-soup ()
224 "Brew a SOUP packet from groups mention on the command line.
225 Will use the remaining command line arguments as regular expressions
226 for matching on group names.
228 For instance, if you want to brew on all the nnml groups, as well as
229 groups with \"emacs\" in the name, you could say something like:
231 $ emacs -batch -f gnus-batch-brew-soup ^nnml \".*emacs.*\""
235 ;;; Internal Functions:
237 ;; Store the current buffer.
238 (defun gnus-soup-store (directory prefix headers format index)
239 (add-hook 'gnus-exit-gnus-hook 'gnus-soup-save-areas)
240 ;; Create the directory, if needed.
241 (or (file-directory-p directory)
242 (gnus-make-directory directory))
243 (let* ((msg-buf (gnus-find-file-noselect
244 (concat directory prefix ".MSG")))
245 (idx-buf (if (= index ?n)
247 (gnus-find-file-noselect
248 (concat directory prefix ".IDX"))))
249 (article-buf (current-buffer))
250 from head-line beg type)
251 (setq gnus-soup-buffers (cons msg-buf gnus-soup-buffers))
252 (buffer-disable-undo msg-buf)
255 (setq gnus-soup-buffers (cons idx-buf gnus-soup-buffers))
256 (buffer-disable-undo idx-buf)))
258 ;; Make sure the last char in the buffer is a newline.
259 (goto-char (point-max))
260 (or (= (current-column) 0)
263 (goto-char (point-min))
265 (mail-strip-quoted-names
266 (or (mail-fetch-field "from")
267 (mail-fetch-field "really-from")
268 (mail-fetch-field "sender"))))
269 (goto-char (point-min))
270 ;; Depending on what encoding is supposed to be used, we make
274 ((= gnus-soup-encoding-type ?n)
275 (format "#! rnews %d\n" (buffer-size)))
276 ((= gnus-soup-encoding-type ?m)
277 (while (search-forward "\nFrom " nil t)
278 (replace-match "\n>From " t t))
279 (concat "From " (or from "unknown")
280 " " (current-time-string) "\n"))
281 ((= gnus-soup-encoding-type ?M)
283 (t (error "Unsupported type: %c" gnus-soup-encoding-type))))
284 ;; Insert the soup header and the article in the MSG buf.
286 (goto-char (point-max))
289 (insert-buffer-substring article-buf)
290 ;; Insert the index in the IDX buf.
293 (gnus-soup-insert-idx beg headers))
295 (error "Unknown index type: %c" type))))))
297 (defun gnus-soup-group-brew (group)
298 (let ((gnus-expert-user t)
299 (gnus-large-newsgroup nil))
300 (and (gnus-summary-read-group group)
301 (let ((gnus-newsgroup-processable
302 (gnus-sorted-complement
303 gnus-newsgroup-unreads
304 (append gnus-newsgroup-dormant gnus-newsgroup-marked))))
305 (gnus-soup-add-article nil)))
306 (gnus-summary-exit)))
308 (defun gnus-soup-insert-idx (offset header)
309 ;; [number subject from date id references chars lines xref]
310 (goto-char (point-max))
312 (format "%d\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t\n"
314 (or (header-subject header) "(none)")
315 (or (header-from header) "(nobody)")
316 (or (header-date header) "")
317 (or (header-id header)
318 (concat "soup-dummy-id-"
320 (lambda (time) (int-to-string time))
321 (current-time) "-")))
322 (or (header-references header) "")
323 (or (header-chars header) 0)
324 (or (header-lines header) "0")
325 (or (header-xref header) ""))))
327 (defun gnus-soup-save-areas ()
328 (gnus-soup-write-areas)
331 (while gnus-soup-buffers
332 (setq buf (car gnus-soup-buffers)
333 gnus-soup-buffers (cdr gnus-soup-buffers))
334 (if (not (buffer-name buf))
337 (and (buffer-modified-p) (save-buffer))
338 (kill-buffer (current-buffer)))))
339 (let ((prefix gnus-soup-last-prefix))
341 (gnus-set-work-buffer)
342 (insert (format "(setq gnus-soup-prev-prefix %d)\n"
344 (write-region (point-min) (point-max)
345 (concat (car (car prefix))
346 gnus-soup-prefix-file)
348 (setq prefix (cdr prefix))))))
350 (defun gnus-soup-pack (dir packer)
351 (let* ((files (mapconcat 'identity
352 '("AREAS" "*.MSG" "*.IDX" "INFO"
353 "LIST" "REPLIES" "COMMANDS" "ERRORS")
355 (packer (if (< (string-match "%s" packer)
356 (string-match "%d" packer))
358 (string-to-int (gnus-soup-unique-prefix dir)))
360 (string-to-int (gnus-soup-unique-prefix dir))
362 (dir (expand-file-name dir)))
363 (setq gnus-soup-areas nil)
364 (message "Packing %s..." packer)
365 (if (zerop (call-process "sh" nil nil nil "-c"
366 (concat "cd " dir " ; " packer)))
368 (call-process "sh" nil nil nil "-c"
369 (concat "cd " dir " ; rm " files))
370 (message "Packing...done" packer))
371 (error "Couldn't pack packet."))))
373 (defun gnus-soup-parse-areas (file)
374 "Parse soup area file FILE.
375 The result is a of vectors, each containing one entry from the AREA file.
376 The vector contain five strings,
377 [prefix name encoding description number]
378 though the two last may be nil if they are missing."
381 (set-buffer (gnus-find-file-noselect file 'force))
382 (buffer-disable-undo (current-buffer))
383 (goto-char (point-min))
386 (cons (vector (gnus-soup-field)
389 (and (eq (preceding-char) ?\t)
391 (and (eq (preceding-char) ?\t)
392 (string-to-int (gnus-soup-field))))
394 (if (eq (preceding-char) ?\t)
395 (beginning-of-line 2))))
398 (defun gnus-soup-parse-replies (file)
399 "Parse soup REPLIES file FILE.
400 The result is a of vectors, each containing one entry from the REPLIES
401 file. The vector contain three strings, [prefix name encoding]."
404 (set-buffer (gnus-find-file-noselect file))
405 (buffer-disable-undo (current-buffer))
406 (goto-char (point-min))
409 (cons (vector (gnus-soup-field) (gnus-soup-field)
412 (if (eq (preceding-char) ?\t)
413 (beginning-of-line 2))))
416 (defun gnus-soup-field ()
418 (buffer-substring (point) (progn (skip-chars-forward "^\t\n") (point)))
421 (defun gnus-soup-read-areas ()
423 (setq gnus-soup-areas
424 (gnus-soup-parse-areas (concat gnus-soup-directory "AREAS")))))
426 (defun gnus-soup-write-areas ()
427 "Write all areas to disk."
429 (if (not gnus-soup-areas)
432 (set-buffer (gnus-find-file-noselect
433 (concat gnus-soup-directory "AREAS")))
435 (let ((areas gnus-soup-areas)
438 (setq area (car areas)
440 (insert (format "%s\t%s\t%s%s\n"
441 (gnus-soup-area-prefix area)
442 (gnus-soup-area-name area)
443 (gnus-soup-area-encoding area)
444 (if (or (gnus-soup-area-description area)
445 (gnus-soup-area-number area))
446 (concat "\t" (or (gnus-soup-area-description
449 (if (gnus-soup-area-number area)
452 (gnus-soup-area-number
455 (write-region (point-min) (point-max)
456 (concat gnus-soup-directory "AREAS"))
457 (set-buffer-modified-p nil)
458 (kill-buffer (current-buffer)))))
460 (defun gnus-soup-write-replies (dir areas)
462 (set-buffer (gnus-find-file-noselect (concat dir "REPLIES")))
466 (setq area (car areas)
468 (insert (format "%s\t%s\t%s\n"
469 (gnus-soup-reply-prefix area)
470 (gnus-soup-reply-kind area)
471 (gnus-soup-reply-encoding area)))))
472 (write-region (point-min) (point-max) (concat dir "REPLIES"))
473 (set-buffer-modified-p nil)
474 (kill-buffer (current-buffer))))
476 (defun gnus-soup-area (group)
477 (gnus-soup-read-areas)
478 (let ((areas gnus-soup-areas)
479 (real-group (gnus-group-real-name group))
482 (setq area (car areas)
484 (if (equal (gnus-soup-area-name area) real-group)
488 (vector (gnus-soup-unique-prefix)
491 gnus-soup-encoding-type
493 (if (gnus-member-of-valid 'mail group) ?m ?n))
495 gnus-soup-areas (cons result gnus-soup-areas)))
498 (defun gnus-soup-unique-prefix (&optional dir)
499 (let* ((dir (file-name-as-directory (or dir gnus-soup-directory)))
500 (entry (assoc dir gnus-soup-last-prefix))
501 gnus-soup-prev-prefix)
504 (and (file-exists-p (concat dir gnus-soup-prefix-file))
506 (load-file (concat dir gnus-soup-prefix-file))
508 (setq gnus-soup-last-prefix
509 (cons (setq entry (cons dir (or gnus-soup-prev-prefix 0)))
510 gnus-soup-last-prefix)))
511 (setcdr entry (1+ (cdr entry)))
512 (int-to-string (cdr entry))))
514 (defun gnus-soup-unpack-packet (dir unpacker packet)
515 (gnus-make-directory dir)
516 (message "Unpacking: %s" (format unpacker packet))
518 "sh" nil nil nil "-c"
519 (format "cd %s ; %s" (expand-file-name dir) (format unpacker packet)))
520 (message "Unpacking...done"))
522 (defun gnus-soup-send-packet (packet)
523 (gnus-soup-unpack-packet
524 gnus-soup-replies-directory gnus-soup-unpacker packet)
525 (let ((replies (gnus-soup-parse-replies
526 (concat gnus-soup-replies-directory "REPLIES"))))
529 (let* ((msg-file (concat gnus-soup-replies-directory
530 (gnus-soup-reply-prefix (car replies))
532 (msg-buf (and (file-exists-p msg-file)
533 (gnus-find-file-noselect msg-file)))
534 (tmp-buf (get-buffer-create " *soup send*"))
537 ((/= (gnus-soup-encoding-format
538 (gnus-soup-reply-encoding (car replies))) ?n)
539 (error "Unsupported encoding"))
543 (buffer-disable-undo msg-buf)
544 (buffer-disable-undo tmp-buf)
546 (goto-char (point-min))
548 (or (looking-at "#! *rnews +\\([0-9]+\\)")
549 (error "Bad header."))
552 end (+ (point) (string-to-int
554 (match-beginning 1) (match-end 1)))))
555 (switch-to-buffer tmp-buf)
557 (insert-buffer-substring msg-buf beg end)
558 (goto-char (point-min))
559 (search-forward "\n\n")
561 (insert mail-header-separator)
563 ((string= (gnus-soup-reply-kind (car replies)) "news")
564 (message "Sending news message to %s..."
565 (mail-fetch-field "newsgroups"))
567 (gnus-inews-article))
568 ((string= (gnus-soup-reply-kind (car replies)) "mail")
569 (message "Sending mail to %s..."
570 (mail-fetch-field "to"))
572 (gnus-mail-send-and-exit))
574 (error "Unknown reply kind")))
577 (delete-file (buffer-file-name))
578 (kill-buffer msg-buf)
579 (kill-buffer tmp-buf)
580 (message "Sent packet"))))
581 (setq replies (cdr replies)))
586 ;;; gnus-soup.el ends here