1 ;;; gnus-soup.el --- SOUP packet writing support for Gnus
2 ;; Copyright (C) 1995,96 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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
38 (defvar gnus-soup-directory "~/SoupBrew/"
39 "*Directory containing an unpacked SOUP packet.")
41 (defvar gnus-soup-replies-directory (concat gnus-soup-directory "SoupReplies/")
42 "*Directory where Gnus will do processing of replies.")
44 (defvar gnus-soup-prefix-file "gnus-prefix"
45 "*Name of the file where Gnus stores the last used prefix.")
47 (defvar gnus-soup-packer "tar cf - %s | gzip > $HOME/Soupout%d.tgz"
48 "Format string command for packing a SOUP packet.
49 The SOUP files will be inserted where the %s is in the string.
50 This string MUST contain both %s and %d. The file number will be
51 inserted where %d appears.")
53 (defvar gnus-soup-unpacker "gunzip -c %s | tar xvf -"
54 "*Format string command for unpacking a SOUP packet.
55 The SOUP packet file name will be inserted at the %s.")
57 (defvar gnus-soup-packet-directory "~/"
58 "*Where gnus-soup will look for REPLIES packets.")
60 (defvar gnus-soup-packet-regexp "Soupin"
61 "*Regular expression matching SOUP REPLIES packets in `gnus-soup-packet-directory'.")
63 (defvar gnus-soup-ignored-headers "^Xref:"
64 "*Regexp to match headers to be removed when brewing SOUP packets.")
66 ;;; Internal Variables:
68 (defvar gnus-soup-encoding-type ?n
70 `n' is news format, `m' is Unix mbox format, and `M' is MMDF mailbox
73 (defvar gnus-soup-index-type ?c
75 `n' means no index file and `c' means standard Cnews overview
78 (defvar gnus-soup-areas nil)
79 (defvar gnus-soup-last-prefix nil)
80 (defvar gnus-soup-prev-prefix nil)
81 (defvar gnus-soup-buffers nil)
85 (defmacro gnus-soup-area-prefix (area)
87 (defmacro gnus-soup-set-area-prefix (area prefix)
88 `(aset ,area 0 ,prefix))
89 (defmacro gnus-soup-area-name (area)
91 (defmacro gnus-soup-area-encoding (area)
93 (defmacro gnus-soup-area-description (area)
95 (defmacro gnus-soup-area-number (area)
97 (defmacro gnus-soup-area-set-number (area value)
98 `(aset ,area 4 ,value))
100 (defmacro gnus-soup-encoding-format (encoding)
102 (defmacro gnus-soup-encoding-index (encoding)
104 (defmacro gnus-soup-encoding-kind (encoding)
107 (defmacro gnus-soup-reply-prefix (reply)
109 (defmacro gnus-soup-reply-kind (reply)
111 (defmacro gnus-soup-reply-encoding (reply)
116 (defun gnus-soup-send-replies ()
117 "Unpack and send all replies in the reply packet."
119 (let ((packets (directory-files
120 gnus-soup-packet-directory t gnus-soup-packet-regexp)))
122 (and (gnus-soup-send-packet (car packets))
123 (delete-file (car packets)))
124 (setq packets (cdr packets)))))
126 (defun gnus-soup-add-article (n)
127 "Add the current article to SOUP packet.
128 If N is a positive number, add the N next articles.
129 If N is a negative number, add the N previous articles.
130 If N is nil and any articles have been marked with the process mark,
131 move those articles instead."
133 (gnus-set-global-variables)
134 (let* ((articles (gnus-summary-work-articles n))
135 (tmp-buf (get-buffer-create "*soup work*"))
136 (area (gnus-soup-area gnus-newsgroup-name))
137 (prefix (gnus-soup-area-prefix area))
139 (buffer-disable-undo tmp-buf)
142 ;; Find the header of the article.
143 (set-buffer gnus-summary-buffer)
144 (when (setq headers (gnus-summary-article-header (car articles)))
145 ;; Put the article in a buffer.
147 (when (gnus-request-article-this-buffer
148 (car articles) gnus-newsgroup-name)
150 (message-narrow-to-head)
151 (message-remove-header gnus-soup-ignored-headers t))
152 (gnus-soup-store gnus-soup-directory prefix headers
153 gnus-soup-encoding-type
154 gnus-soup-index-type)
155 (gnus-soup-area-set-number
156 area (1+ (or (gnus-soup-area-number area) 0)))))
157 ;; Mark article as read.
158 (set-buffer gnus-summary-buffer)
159 (gnus-summary-remove-process-mark (car articles))
160 (gnus-summary-mark-as-read (car articles) gnus-souped-mark)
161 (setq articles (cdr articles)))
162 (kill-buffer tmp-buf))
163 (gnus-soup-save-areas)))
165 (defun gnus-soup-pack-packet ()
166 "Make a SOUP packet from the SOUP areas."
168 (gnus-soup-read-areas)
169 (gnus-soup-pack gnus-soup-directory gnus-soup-packer))
171 (defun gnus-group-brew-soup (n)
172 "Make a soup packet from the current group.
173 Uses the process/prefix convention."
175 (let ((groups (gnus-group-process-prefix n)))
177 (gnus-group-remove-mark (car groups))
178 (gnus-soup-group-brew (car groups) t)
179 (setq groups (cdr groups)))
180 (gnus-soup-save-areas)))
182 (defun gnus-brew-soup (&optional level)
183 "Go through all groups on LEVEL or less and make a soup packet."
185 (let ((level (or level gnus-level-subscribed))
186 (newsrc (cdr gnus-newsrc-alist)))
188 (and (<= (nth 1 (car newsrc)) level)
189 (gnus-soup-group-brew (caar newsrc) t))
190 (setq newsrc (cdr newsrc)))
191 (gnus-soup-save-areas)))
194 (defun gnus-batch-brew-soup ()
195 "Brew a SOUP packet from groups mention on the command line.
196 Will use the remaining command line arguments as regular expressions
197 for matching on group names.
199 For instance, if you want to brew on all the nnml groups, as well as
200 groups with \"emacs\" in the name, you could say something like:
202 $ emacs -batch -f gnus-batch-brew-soup ^nnml \".*emacs.*\""
206 ;;; Internal Functions:
208 ;; Store the current buffer.
209 (defun gnus-soup-store (directory prefix headers format index)
210 ;; Create the directory, if needed.
211 (or (file-directory-p directory)
212 (make-directory directory) t)
213 (let* ((msg-buf (find-file-noselect
214 (concat directory prefix ".MSG")))
215 (idx-buf (if (= index ?n)
218 (concat directory prefix ".IDX"))))
219 (article-buf (current-buffer))
220 from head-line beg type)
221 (setq gnus-soup-buffers (cons msg-buf (delq msg-buf gnus-soup-buffers)))
222 (buffer-disable-undo msg-buf)
225 (setq gnus-soup-buffers (cons idx-buf gnus-soup-buffers))
226 (buffer-disable-undo idx-buf)))
228 ;; Make sure the last char in the buffer is a newline.
229 (goto-char (point-max))
230 (or (= (current-column) 0)
233 (goto-char (point-min))
235 (gnus-mail-strip-quoted-names
236 (or (mail-fetch-field "from")
237 (mail-fetch-field "really-from")
238 (mail-fetch-field "sender"))))
239 (goto-char (point-min))
240 ;; Depending on what encoding is supposed to be used, we make
244 ((= gnus-soup-encoding-type ?n)
245 (format "#! rnews %d\n" (buffer-size)))
246 ((= gnus-soup-encoding-type ?m)
247 (while (search-forward "\nFrom " nil t)
248 (replace-match "\n>From " t t))
249 (concat "From " (or from "unknown")
250 " " (current-time-string) "\n"))
251 ((= gnus-soup-encoding-type ?M)
253 (t (error "Unsupported type: %c" gnus-soup-encoding-type))))
254 ;; Insert the soup header and the article in the MSG buf.
256 (goto-char (point-max))
259 (insert-buffer-substring article-buf)
260 ;; Insert the index in the IDX buf.
263 (gnus-soup-insert-idx beg headers))
265 (error "Unknown index type: %c" type)))
266 ;; Return the MSG buf.
269 (defun gnus-soup-group-brew (group &optional not-all)
270 "Enter GROUP and add all articles to a SOUP package.
271 If NOT-ALL, don't pack ticked articles."
272 (let ((gnus-expert-user t)
273 (gnus-large-newsgroup nil)
274 (entry (gnus-gethash group gnus-newsrc-hashtb)))
275 (when (or (null entry)
280 (gnus-range-length (cdr (assq 'tick (gnus-info-marks
282 (when (gnus-summary-read-group group nil t)
283 (setq gnus-newsgroup-processable
286 (append gnus-newsgroup-marked gnus-newsgroup-unreads)
287 gnus-newsgroup-unreads)))
288 (gnus-soup-add-article nil)
289 (gnus-summary-exit)))))
291 (defun gnus-soup-insert-idx (offset header)
292 ;; [number subject from date id references chars lines xref]
293 (goto-char (point-max))
295 (format "%d\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t\t\n"
297 (or (mail-header-subject header) "(none)")
298 (or (mail-header-from header) "(nobody)")
299 (or (mail-header-date header) "")
300 (or (mail-header-id header)
301 (concat "soup-dummy-id-"
303 (lambda (time) (int-to-string time))
304 (current-time) "-")))
305 (or (mail-header-references header) "")
306 (or (mail-header-chars header) 0)
307 (or (mail-header-lines header) "0"))))
309 (defun gnus-soup-save-areas ()
310 (gnus-soup-write-areas)
313 (while gnus-soup-buffers
314 (setq buf (car gnus-soup-buffers)
315 gnus-soup-buffers (cdr gnus-soup-buffers))
316 (if (not (buffer-name buf))
319 (and (buffer-modified-p) (save-buffer))
320 (kill-buffer (current-buffer)))))
321 (gnus-soup-write-prefixes)))
323 (defun gnus-soup-write-prefixes ()
324 (let ((prefix gnus-soup-last-prefix))
327 (gnus-set-work-buffer)
328 (insert (format "(setq gnus-soup-prev-prefix %d)\n" (cdar prefix)))
329 (unless (file-exists-p (caar prefix))
330 (make-directory (caar prefix) t))
331 (write-region (point-min) (point-max)
332 (concat (caar prefix) gnus-soup-prefix-file)
334 (setq prefix (cdr prefix))))))
336 (defun gnus-soup-pack (dir packer)
337 (let* ((files (mapconcat 'identity
338 '("AREAS" "*.MSG" "*.IDX" "INFO"
339 "LIST" "REPLIES" "COMMANDS" "ERRORS")
341 (packer (if (< (string-match "%s" packer)
342 (string-match "%d" packer))
344 (string-to-int (gnus-soup-unique-prefix dir)))
346 (string-to-int (gnus-soup-unique-prefix dir))
348 (dir (expand-file-name dir)))
349 (or (file-directory-p dir)
350 (make-directory dir t))
351 (setq gnus-soup-areas nil)
352 (gnus-message 4 "Packing %s..." packer)
353 (if (zerop (call-process shell-file-name
354 nil nil nil shell-command-switch
355 (concat "cd " dir " ; " packer)))
357 (call-process shell-file-name nil nil nil shell-command-switch
358 (concat "cd " dir " ; rm " files))
359 (gnus-message 4 "Packing...done" packer))
360 (error "Couldn't pack packet."))))
362 (defun gnus-soup-parse-areas (file)
363 "Parse soup area file FILE.
364 The result is a of vectors, each containing one entry from the AREA file.
365 The vector contain five strings,
366 [prefix name encoding description number]
367 though the two last may be nil if they are missing."
370 (set-buffer (find-file-noselect file 'force))
371 (buffer-disable-undo (current-buffer))
372 (goto-char (point-min))
375 (cons (vector (gnus-soup-field)
378 (and (eq (preceding-char) ?\t)
380 (and (eq (preceding-char) ?\t)
381 (string-to-int (gnus-soup-field))))
383 (if (eq (preceding-char) ?\t)
384 (beginning-of-line 2)))
385 (kill-buffer (current-buffer)))
388 (defun gnus-soup-parse-replies (file)
389 "Parse soup REPLIES file FILE.
390 The result is a of vectors, each containing one entry from the REPLIES
391 file. The vector contain three strings, [prefix name encoding]."
394 (set-buffer (find-file-noselect file))
395 (buffer-disable-undo (current-buffer))
396 (goto-char (point-min))
399 (cons (vector (gnus-soup-field) (gnus-soup-field)
402 (if (eq (preceding-char) ?\t)
403 (beginning-of-line 2)))
404 (kill-buffer (current-buffer)))
407 (defun gnus-soup-field ()
409 (buffer-substring (point) (progn (skip-chars-forward "^\t\n") (point)))
412 (defun gnus-soup-read-areas ()
414 (setq gnus-soup-areas
415 (gnus-soup-parse-areas (concat gnus-soup-directory "AREAS")))))
417 (defun gnus-soup-write-areas ()
418 "Write the AREAS file."
420 (when gnus-soup-areas
421 (nnheader-temp-write (concat gnus-soup-directory "AREAS")
422 (let ((areas gnus-soup-areas)
424 (while (setq area (pop areas))
428 (gnus-soup-area-prefix area)
429 (gnus-soup-area-name area)
430 (gnus-soup-area-encoding area)
431 (if (or (gnus-soup-area-description area)
432 (gnus-soup-area-number area))
433 (concat "\t" (or (gnus-soup-area-description
435 (if (gnus-soup-area-number area)
436 (concat "\t" (int-to-string
437 (gnus-soup-area-number area)))
440 (defun gnus-soup-write-replies (dir areas)
441 "Write a REPLIES file in DIR containing AREAS."
442 (nnheader-temp-write (concat dir "REPLIES")
444 (while (setq area (pop areas))
445 (insert (format "%s\t%s\t%s\n"
446 (gnus-soup-reply-prefix area)
447 (gnus-soup-reply-kind area)
448 (gnus-soup-reply-encoding area)))))))
450 (defun gnus-soup-area (group)
451 (gnus-soup-read-areas)
452 (let ((areas gnus-soup-areas)
453 (real-group (gnus-group-real-name group))
456 (setq area (car areas)
458 (if (equal (gnus-soup-area-name area) real-group)
462 (vector (gnus-soup-unique-prefix)
465 gnus-soup-encoding-type
467 (if (gnus-member-of-valid 'mail group) ?m ?n))
469 gnus-soup-areas (cons result gnus-soup-areas)))
472 (defun gnus-soup-unique-prefix (&optional dir)
473 (let* ((dir (file-name-as-directory (or dir gnus-soup-directory)))
474 (entry (assoc dir gnus-soup-last-prefix))
475 gnus-soup-prev-prefix)
478 (and (file-exists-p (concat dir gnus-soup-prefix-file))
480 (load (concat dir gnus-soup-prefix-file) nil t t)
482 (setq gnus-soup-last-prefix
483 (cons (setq entry (cons dir (or gnus-soup-prev-prefix 0)))
484 gnus-soup-last-prefix)))
485 (setcdr entry (1+ (cdr entry)))
486 (gnus-soup-write-prefixes)
487 (int-to-string (cdr entry))))
489 (defun gnus-soup-unpack-packet (dir unpacker packet)
490 "Unpack PACKET into DIR using UNPACKER.
491 Return whether the unpacking was successful."
492 (make-directory dir t)
493 (gnus-message 4 "Unpacking: %s" (format unpacker packet))
496 shell-file-name nil nil nil shell-command-switch
497 (format "cd %s ; %s" (expand-file-name dir)
498 (format unpacker packet))))
499 (gnus-message 4 "Unpacking...done")))
501 (defun gnus-soup-send-packet (packet)
502 (gnus-soup-unpack-packet
503 gnus-soup-replies-directory gnus-soup-unpacker packet)
504 (let ((replies (gnus-soup-parse-replies
505 (concat gnus-soup-replies-directory "REPLIES"))))
508 (let* ((msg-file (concat gnus-soup-replies-directory
509 (gnus-soup-reply-prefix (car replies))
511 (msg-buf (and (file-exists-p msg-file)
512 (find-file-noselect msg-file)))
513 (tmp-buf (get-buffer-create " *soup send*"))
516 ((/= (gnus-soup-encoding-format
517 (gnus-soup-reply-encoding (car replies))) ?n)
518 (error "Unsupported encoding"))
522 (buffer-disable-undo msg-buf)
523 (buffer-disable-undo tmp-buf)
525 (goto-char (point-min))
527 (or (looking-at "#! *rnews +\\([0-9]+\\)")
528 (error "Bad header."))
531 end (+ (point) (string-to-int
533 (match-beginning 1) (match-end 1)))))
534 (switch-to-buffer tmp-buf)
536 (insert-buffer-substring msg-buf beg end)
537 (goto-char (point-min))
538 (search-forward "\n\n")
540 (insert mail-header-separator)
541 (setq message-newsreader (setq message-mailer
542 (gnus-extended-version)))
544 ((string= (gnus-soup-reply-kind (car replies)) "news")
545 (gnus-message 5 "Sending news message to %s..."
546 (mail-fetch-field "newsgroups"))
548 (funcall message-send-news-function))
549 ((string= (gnus-soup-reply-kind (car replies)) "mail")
550 (gnus-message 5 "Sending mail to %s..."
551 (mail-fetch-field "to"))
555 (error "Unknown reply kind")))
558 (delete-file (buffer-file-name))
559 (kill-buffer msg-buf)
560 (kill-buffer tmp-buf)
561 (gnus-message 4 "Sent packet"))))
562 (setq replies (cdr replies)))
567 ;;; gnus-soup.el ends here