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.
33 (defvar gnus-soup-directory "~/SoupBrew/"
34 "*Directory containing an unpacked SOUP packet.")
36 (defvar gnus-soup-replies-directory (concat gnus-soup-directory "SoupReplies/")
37 "*Directory where Gnus will do processing of replies.")
39 (defvar gnus-soup-prefix-file "gnus-prefix"
40 "*Name of the file where Gnus stores the last used prefix.")
42 (defvar gnus-soup-packer "tar cf - %s | gzip > $HOME/Soupout%d.tgz"
43 "Format string command for packing a SOUP packet.
44 The SOUP files will be inserted where the %s is in the string.
45 This string MUST contain both %s and %d. The file number will be
46 inserted where %d appears.")
48 (defvar gnus-soup-unpacker "gunzip -c %s | tar xvf -"
49 "*Format string command for unpacking a SOUP packet.
50 The SOUP packet file name will be inserted at the %s.")
52 (defvar gnus-soup-packet-directory "~/"
53 "*Where gnus-soup will look for REPLIES packets.")
55 (defvar gnus-soup-packet-regexp "Soupin"
56 "*Regular expression matching SOUP REPLIES packets in `gnus-soup-packet-directory'.")
58 ;;; Internal Variables:
60 (defvar gnus-soup-encoding-type ?n
62 `n' is news format, `m' is Unix mbox format, and `M' is MMDF mailbox
65 (defvar gnus-soup-index-type ?c
67 `n' means no index file and `c' means standard Cnews overview
70 (defvar gnus-soup-areas nil)
71 (defvar gnus-soup-last-prefix nil)
72 (defvar gnus-soup-prev-prefix nil)
73 (defvar gnus-soup-buffers nil)
77 (defmacro gnus-soup-area-prefix (area)
78 (` (aref (, area) 0)))
79 (defmacro gnus-soup-area-name (area)
80 (` (aref (, area) 1)))
81 (defmacro gnus-soup-area-encoding (area)
82 (` (aref (, area) 2)))
83 (defmacro gnus-soup-area-description (area)
84 (` (aref (, area) 3)))
85 (defmacro gnus-soup-area-number (area)
86 (` (aref (, area) 4)))
87 (defmacro gnus-soup-area-set-number (area value)
88 (` (aset (, area) 4 (, value))))
90 (defmacro gnus-soup-encoding-format (encoding)
91 (` (aref (, encoding) 0)))
92 (defmacro gnus-soup-encoding-index (encoding)
93 (` (aref (, encoding) 1)))
94 (defmacro gnus-soup-encoding-kind (encoding)
95 (` (aref (, encoding) 2)))
97 (defmacro gnus-soup-reply-prefix (reply)
98 (` (aref (, reply) 0)))
99 (defmacro gnus-soup-reply-kind (reply)
100 (` (aref (, reply) 1)))
101 (defmacro gnus-soup-reply-encoding (reply)
102 (` (aref (, reply) 2)))
106 (defun gnus-soup-send-replies ()
107 "Unpack and send all replies in the reply packet."
109 (let ((packets (directory-files
110 gnus-soup-packet-directory t gnus-soup-packet-regexp)))
112 (and (gnus-soup-send-packet (car packets))
113 (delete-file (car packets)))
114 (setq packets (cdr packets)))))
116 (defun gnus-soup-add-article (n)
117 "Add the current article to SOUP packet.
118 If N is a positive number, add the N next articles.
119 If N is a negative number, add the N previous articles.
120 If N is nil and any articles have been marked with the process mark,
121 move those articles instead."
123 (gnus-set-global-variables)
124 (let* ((articles (gnus-summary-work-articles n))
125 (tmp-buf (get-buffer-create "*soup work*"))
126 (area (gnus-soup-area gnus-newsgroup-name))
127 (prefix (gnus-soup-area-prefix area))
129 (buffer-disable-undo tmp-buf)
132 ;; Find the header of the article.
133 (set-buffer gnus-summary-buffer)
134 (setq headers (gnus-summary-article-header (car articles)))
135 ;; Put the article in a buffer.
137 (gnus-request-article-this-buffer
138 (car articles) gnus-newsgroup-name)
139 (gnus-soup-store gnus-soup-directory prefix headers
140 gnus-soup-encoding-type
141 gnus-soup-index-type)
142 (gnus-soup-area-set-number area
143 (1+ (or (gnus-soup-area-number area) 0)))
144 ;; Mark article as read.
145 (set-buffer gnus-summary-buffer)
146 (gnus-summary-remove-process-mark (car articles))
147 (gnus-summary-mark-as-read (car articles) gnus-souped-mark)
148 (setq articles (cdr articles)))
149 (kill-buffer tmp-buf))
150 (gnus-soup-save-areas)))
152 (defun gnus-soup-pack-packet ()
153 "Make a SOUP packet from the SOUP areas."
155 (gnus-soup-read-areas)
156 (gnus-soup-pack gnus-soup-directory gnus-soup-packer))
158 (defun gnus-group-brew-soup (n)
159 "Make a soup packet from the current group.
160 Uses the process/prefix convention."
162 (let ((groups (gnus-group-process-prefix n)))
164 (gnus-group-remove-mark (car groups))
165 (gnus-soup-group-brew (car groups))
166 (setq groups (cdr groups)))
167 (gnus-soup-save-areas)))
169 (defun gnus-brew-soup (&optional level)
170 "Go through all groups on LEVEL or less and make a soup packet."
172 (let ((level (or level gnus-level-subscribed))
173 (newsrc (cdr gnus-newsrc-alist)))
175 (and (<= (nth 1 (car newsrc)) level)
176 (gnus-soup-group-brew (car (car newsrc))))
177 (setq newsrc (cdr newsrc)))
178 (gnus-soup-save-areas)))
181 (defun gnus-batch-brew-soup ()
182 "Brew a SOUP packet from groups mention on the command line.
183 Will use the remaining command line arguments as regular expressions
184 for matching on group names.
186 For instance, if you want to brew on all the nnml groups, as well as
187 groups with \"emacs\" in the name, you could say something like:
189 $ emacs -batch -f gnus-batch-brew-soup ^nnml \".*emacs.*\""
193 ;;; Internal Functions:
195 ;; Store the current buffer.
196 (defun gnus-soup-store (directory prefix headers format index)
197 ;; Create the directory, if needed.
198 (or (file-directory-p directory)
199 (gnus-make-directory directory))
200 (let* ((msg-buf (find-file-noselect
201 (concat directory prefix ".MSG")))
202 (idx-buf (if (= index ?n)
205 (concat directory prefix ".IDX"))))
206 (article-buf (current-buffer))
207 from head-line beg type)
208 (setq gnus-soup-buffers (cons msg-buf (delq msg-buf gnus-soup-buffers)))
209 (buffer-disable-undo msg-buf)
212 (setq gnus-soup-buffers (cons idx-buf gnus-soup-buffers))
213 (buffer-disable-undo idx-buf)))
215 ;; Make sure the last char in the buffer is a newline.
216 (goto-char (point-max))
217 (or (= (current-column) 0)
220 (goto-char (point-min))
222 (mail-strip-quoted-names
223 (or (mail-fetch-field "from")
224 (mail-fetch-field "really-from")
225 (mail-fetch-field "sender"))))
226 (goto-char (point-min))
227 ;; Depending on what encoding is supposed to be used, we make
231 ((= gnus-soup-encoding-type ?n)
232 (format "#! rnews %d\n" (buffer-size)))
233 ((= gnus-soup-encoding-type ?m)
234 (while (search-forward "\nFrom " nil t)
235 (replace-match "\n>From " t t))
236 (concat "From " (or from "unknown")
237 " " (current-time-string) "\n"))
238 ((= gnus-soup-encoding-type ?M)
240 (t (error "Unsupported type: %c" gnus-soup-encoding-type))))
241 ;; Insert the soup header and the article in the MSG buf.
243 (goto-char (point-max))
246 (insert-buffer-substring article-buf)
247 ;; Insert the index in the IDX buf.
250 (gnus-soup-insert-idx beg headers))
252 (error "Unknown index type: %c" type)))
253 ;; Return the MSG buf.
256 (defun gnus-soup-group-brew (group)
257 (let ((gnus-expert-user t)
258 (gnus-large-newsgroup nil))
259 (and (gnus-summary-read-group group)
260 (let ((gnus-newsgroup-processable
261 (gnus-sorted-complement
262 gnus-newsgroup-unreads
263 (append gnus-newsgroup-dormant gnus-newsgroup-marked))))
264 (gnus-soup-add-article nil)))
265 (gnus-summary-exit)))
267 (defun gnus-soup-insert-idx (offset header)
268 ;; [number subject from date id references chars lines xref]
269 (goto-char (point-max))
271 (format "%d\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t\n"
273 (or (mail-header-subject header) "(none)")
274 (or (mail-header-from header) "(nobody)")
275 (or (mail-header-date header) "")
276 (or (mail-header-id header)
277 (concat "soup-dummy-id-"
279 (lambda (time) (int-to-string time))
280 (current-time) "-")))
281 (or (mail-header-references header) "")
282 (or (mail-header-chars header) 0)
283 (or (mail-header-lines header) "0")
284 (or (mail-header-xref header) ""))))
286 (defun gnus-soup-save-areas ()
287 (gnus-soup-write-areas)
290 (while gnus-soup-buffers
291 (setq buf (car gnus-soup-buffers)
292 gnus-soup-buffers (cdr gnus-soup-buffers))
293 (if (not (buffer-name buf))
296 (and (buffer-modified-p) (save-buffer))
297 (kill-buffer (current-buffer)))))
298 (gnus-soup-write-prefixes)))
300 (defun gnus-soup-write-prefixes ()
301 (let ((prefix gnus-soup-last-prefix))
304 (gnus-set-work-buffer)
305 (insert (format "(setq gnus-soup-prev-prefix %d)\n"
307 (write-region (point-min) (point-max)
308 (concat (car (car prefix))
309 gnus-soup-prefix-file)
311 (setq prefix (cdr prefix))))))
313 (defun gnus-soup-pack (dir packer)
314 (let* ((files (mapconcat 'identity
315 '("AREAS" "*.MSG" "*.IDX" "INFO"
316 "LIST" "REPLIES" "COMMANDS" "ERRORS")
318 (packer (if (< (string-match "%s" packer)
319 (string-match "%d" packer))
321 (string-to-int (gnus-soup-unique-prefix dir)))
323 (string-to-int (gnus-soup-unique-prefix dir))
325 (dir (expand-file-name dir)))
326 (or (file-directory-p dir)
327 (gnus-make-directory dir))
328 (setq gnus-soup-areas nil)
329 (message "Packing %s..." packer)
330 (if (zerop (call-process "sh" nil nil nil "-c"
331 (concat "cd " dir " ; " packer)))
333 (call-process "sh" nil nil nil "-c"
334 (concat "cd " dir " ; rm " files))
335 (message "Packing...done" packer))
336 (error "Couldn't pack packet."))))
338 (defun gnus-soup-parse-areas (file)
339 "Parse soup area file FILE.
340 The result is a of vectors, each containing one entry from the AREA file.
341 The vector contain five strings,
342 [prefix name encoding description number]
343 though the two last may be nil if they are missing."
346 (set-buffer (find-file-noselect file 'force))
347 (buffer-disable-undo (current-buffer))
348 (goto-char (point-min))
351 (cons (vector (gnus-soup-field)
354 (and (eq (preceding-char) ?\t)
356 (and (eq (preceding-char) ?\t)
357 (string-to-int (gnus-soup-field))))
359 (if (eq (preceding-char) ?\t)
360 (beginning-of-line 2))))
363 (defun gnus-soup-parse-replies (file)
364 "Parse soup REPLIES file FILE.
365 The result is a of vectors, each containing one entry from the REPLIES
366 file. The vector contain three strings, [prefix name encoding]."
369 (set-buffer (find-file-noselect file))
370 (buffer-disable-undo (current-buffer))
371 (goto-char (point-min))
374 (cons (vector (gnus-soup-field) (gnus-soup-field)
377 (if (eq (preceding-char) ?\t)
378 (beginning-of-line 2)))
379 (kill-buffer (current-buffer)))
382 (defun gnus-soup-field ()
384 (buffer-substring (point) (progn (skip-chars-forward "^\t\n") (point)))
387 (defun gnus-soup-read-areas ()
389 (setq gnus-soup-areas
390 (gnus-soup-parse-areas (concat gnus-soup-directory "AREAS")))))
392 (defun gnus-soup-write-areas ()
393 "Write all areas to disk."
395 (if (not gnus-soup-areas)
398 (set-buffer (find-file-noselect
399 (concat gnus-soup-directory "AREAS")))
400 (buffer-disable-undo (current-buffer))
402 (let ((areas gnus-soup-areas)
405 (setq area (car areas)
407 (insert (format "%s\t%s\t%s%s\n"
408 (gnus-soup-area-prefix area)
409 (gnus-soup-area-name area)
410 (gnus-soup-area-encoding area)
411 (if (or (gnus-soup-area-description area)
412 (gnus-soup-area-number area))
413 (concat "\t" (or (gnus-soup-area-description
416 (if (gnus-soup-area-number area)
419 (gnus-soup-area-number
422 (write-region (point-min) (point-max)
423 (concat gnus-soup-directory "AREAS"))
424 (set-buffer-modified-p nil)
425 (kill-buffer (current-buffer)))))
427 (defun gnus-soup-write-replies (dir areas)
428 (or (file-directory-p dir)
429 (gnus-make-directory dir))
431 (set-buffer (find-file-noselect (concat dir "REPLIES")))
432 (buffer-disable-undo (current-buffer))
436 (setq area (car areas)
438 (insert (format "%s\t%s\t%s\n"
439 (gnus-soup-reply-prefix area)
440 (gnus-soup-reply-kind area)
441 (gnus-soup-reply-encoding area)))))
442 (write-region (point-min) (point-max) (concat dir "REPLIES"))
443 (set-buffer-modified-p nil)
444 (kill-buffer (current-buffer))))
446 (defun gnus-soup-area (group)
447 (gnus-soup-read-areas)
448 (let ((areas gnus-soup-areas)
449 (real-group (gnus-group-real-name group))
452 (setq area (car areas)
454 (if (equal (gnus-soup-area-name area) real-group)
458 (vector (gnus-soup-unique-prefix)
461 gnus-soup-encoding-type
463 (if (gnus-member-of-valid 'mail group) ?m ?n))
465 gnus-soup-areas (cons result gnus-soup-areas)))
468 (defun gnus-soup-unique-prefix (&optional dir)
469 (let* ((dir (file-name-as-directory (or dir gnus-soup-directory)))
470 (entry (assoc dir gnus-soup-last-prefix))
471 gnus-soup-prev-prefix)
474 (and (file-exists-p (concat dir gnus-soup-prefix-file))
476 (load-file (concat dir gnus-soup-prefix-file))
478 (setq gnus-soup-last-prefix
479 (cons (setq entry (cons dir (or gnus-soup-prev-prefix 0)))
480 gnus-soup-last-prefix)))
481 (setcdr entry (1+ (cdr entry)))
482 (gnus-soup-write-prefixes)
483 (int-to-string (cdr entry))))
485 (defun gnus-soup-unpack-packet (dir unpacker packet)
486 (gnus-make-directory dir)
487 (message "Unpacking: %s" (format unpacker packet))
489 "sh" nil nil nil "-c"
490 (format "cd %s ; %s" (expand-file-name dir) (format unpacker packet)))
491 (message "Unpacking...done"))
493 (defun gnus-soup-send-packet (packet)
494 (gnus-soup-unpack-packet
495 gnus-soup-replies-directory gnus-soup-unpacker packet)
496 (let ((replies (gnus-soup-parse-replies
497 (concat gnus-soup-replies-directory "REPLIES"))))
500 (let* ((msg-file (concat gnus-soup-replies-directory
501 (gnus-soup-reply-prefix (car replies))
503 (msg-buf (and (file-exists-p msg-file)
504 (find-file-noselect msg-file)))
505 (tmp-buf (get-buffer-create " *soup send*"))
508 ((/= (gnus-soup-encoding-format
509 (gnus-soup-reply-encoding (car replies))) ?n)
510 (error "Unsupported encoding"))
514 (buffer-disable-undo msg-buf)
515 (buffer-disable-undo tmp-buf)
517 (goto-char (point-min))
519 (or (looking-at "#! *rnews +\\([0-9]+\\)")
520 (error "Bad header."))
523 end (+ (point) (string-to-int
525 (match-beginning 1) (match-end 1)))))
526 (switch-to-buffer tmp-buf)
528 (insert-buffer-substring msg-buf beg end)
529 (goto-char (point-min))
530 (search-forward "\n\n")
532 (insert mail-header-separator)
534 ((string= (gnus-soup-reply-kind (car replies)) "news")
535 (message "Sending news message to %s..."
536 (mail-fetch-field "newsgroups"))
538 (gnus-inews-article))
539 ((string= (gnus-soup-reply-kind (car replies)) "mail")
540 (message "Sending mail to %s..."
541 (mail-fetch-field "to"))
543 (gnus-mail-send-and-exit))
545 (error "Unknown reply kind")))
548 (delete-file (buffer-file-name))
549 (kill-buffer msg-buf)
550 (kill-buffer tmp-buf)
551 (message "Sent packet"))))
552 (setq replies (cdr replies)))
557 ;;; gnus-soup.el ends here