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.
31 (eval-when-compile (require 'cl))
35 (defvar gnus-soup-directory "~/SoupBrew/"
36 "*Directory containing an unpacked SOUP packet.")
38 (defvar gnus-soup-replies-directory (concat gnus-soup-directory "SoupReplies/")
39 "*Directory where Gnus will do processing of replies.")
41 (defvar gnus-soup-prefix-file "gnus-prefix"
42 "*Name of the file where Gnus stores the last used prefix.")
44 (defvar gnus-soup-packer "tar cf - %s | gzip > $HOME/Soupout%d.tgz"
45 "Format string command for packing a SOUP packet.
46 The SOUP files will be inserted where the %s is in the string.
47 This string MUST contain both %s and %d. The file number will be
48 inserted where %d appears.")
50 (defvar gnus-soup-unpacker "gunzip -c %s | tar xvf -"
51 "*Format string command for unpacking a SOUP packet.
52 The SOUP packet file name will be inserted at the %s.")
54 (defvar gnus-soup-packet-directory "~/"
55 "*Where gnus-soup will look for REPLIES packets.")
57 (defvar gnus-soup-packet-regexp "Soupin"
58 "*Regular expression matching SOUP REPLIES packets in `gnus-soup-packet-directory'.")
60 (defvar gnus-soup-ignored-headers "^Xref:"
61 "*Regexp to match headers to be removed when brewing SOUP packets.")
63 ;;; Internal Variables:
65 (defvar gnus-soup-encoding-type ?n
67 `n' is news format, `m' is Unix mbox format, and `M' is MMDF mailbox
70 (defvar gnus-soup-index-type ?c
72 `n' means no index file and `c' means standard Cnews overview
75 (defvar gnus-soup-areas nil)
76 (defvar gnus-soup-last-prefix nil)
77 (defvar gnus-soup-prev-prefix nil)
78 (defvar gnus-soup-buffers nil)
82 (defmacro gnus-soup-area-prefix (area)
84 (defmacro gnus-soup-set-area-prefix (area prefix)
85 `(aset ,area 0 ,prefix))
86 (defmacro gnus-soup-area-name (area)
88 (defmacro gnus-soup-area-encoding (area)
90 (defmacro gnus-soup-area-description (area)
92 (defmacro gnus-soup-area-number (area)
94 (defmacro gnus-soup-area-set-number (area value)
95 `(aset ,area 4 ,value))
97 (defmacro gnus-soup-encoding-format (encoding)
99 (defmacro gnus-soup-encoding-index (encoding)
101 (defmacro gnus-soup-encoding-kind (encoding)
104 (defmacro gnus-soup-reply-prefix (reply)
106 (defmacro gnus-soup-reply-kind (reply)
108 (defmacro gnus-soup-reply-encoding (reply)
113 (defun gnus-soup-send-replies ()
114 "Unpack and send all replies in the reply packet."
116 (let ((packets (directory-files
117 gnus-soup-packet-directory t gnus-soup-packet-regexp)))
119 (and (gnus-soup-send-packet (car packets))
120 (delete-file (car packets)))
121 (setq packets (cdr packets)))))
123 (defun gnus-soup-add-article (n)
124 "Add the current article to SOUP packet.
125 If N is a positive number, add the N next articles.
126 If N is a negative number, add the N previous articles.
127 If N is nil and any articles have been marked with the process mark,
128 move those articles instead."
130 (gnus-set-global-variables)
131 (let* ((articles (gnus-summary-work-articles n))
132 (tmp-buf (get-buffer-create "*soup work*"))
133 (area (gnus-soup-area gnus-newsgroup-name))
134 (prefix (gnus-soup-area-prefix area))
136 (buffer-disable-undo tmp-buf)
139 ;; Find the header of the article.
140 (set-buffer gnus-summary-buffer)
141 (when (setq headers (gnus-summary-article-header (car articles)))
142 ;; Put the article in a buffer.
144 (when (gnus-request-article-this-buffer
145 (car articles) gnus-newsgroup-name)
147 (message-narrow-to-headers)
148 (message-remove-header gnus-soup-ignored-headers t))
149 (gnus-soup-store gnus-soup-directory prefix headers
150 gnus-soup-encoding-type
151 gnus-soup-index-type)
152 (gnus-soup-area-set-number
153 area (1+ (or (gnus-soup-area-number area) 0)))))
154 ;; Mark article as read.
155 (set-buffer gnus-summary-buffer)
156 (gnus-summary-remove-process-mark (car articles))
157 (gnus-summary-mark-as-read (car articles) gnus-souped-mark)
158 (setq articles (cdr articles)))
159 (kill-buffer tmp-buf))
160 (gnus-soup-save-areas)))
162 (defun gnus-soup-pack-packet ()
163 "Make a SOUP packet from the SOUP areas."
165 (gnus-soup-read-areas)
166 (gnus-soup-pack gnus-soup-directory gnus-soup-packer))
168 (defun gnus-group-brew-soup (n)
169 "Make a soup packet from the current group.
170 Uses the process/prefix convention."
172 (let ((groups (gnus-group-process-prefix n)))
174 (gnus-group-remove-mark (car groups))
175 (gnus-soup-group-brew (car groups) t)
176 (setq groups (cdr groups)))
177 (gnus-soup-save-areas)))
179 (defun gnus-brew-soup (&optional level)
180 "Go through all groups on LEVEL or less and make a soup packet."
182 (let ((level (or level gnus-level-subscribed))
183 (newsrc (cdr gnus-newsrc-alist)))
185 (and (<= (nth 1 (car newsrc)) level)
186 (gnus-soup-group-brew (caar newsrc) t))
187 (setq newsrc (cdr newsrc)))
188 (gnus-soup-save-areas)))
191 (defun gnus-batch-brew-soup ()
192 "Brew a SOUP packet from groups mention on the command line.
193 Will use the remaining command line arguments as regular expressions
194 for matching on group names.
196 For instance, if you want to brew on all the nnml groups, as well as
197 groups with \"emacs\" in the name, you could say something like:
199 $ emacs -batch -f gnus-batch-brew-soup ^nnml \".*emacs.*\""
203 ;;; Internal Functions:
205 ;; Store the current buffer.
206 (defun gnus-soup-store (directory prefix headers format index)
207 ;; Create the directory, if needed.
208 (or (file-directory-p directory)
209 (gnus-make-directory directory))
210 (let* ((msg-buf (find-file-noselect
211 (concat directory prefix ".MSG")))
212 (idx-buf (if (= index ?n)
215 (concat directory prefix ".IDX"))))
216 (article-buf (current-buffer))
217 from head-line beg type)
218 (setq gnus-soup-buffers (cons msg-buf (delq msg-buf gnus-soup-buffers)))
219 (buffer-disable-undo msg-buf)
222 (setq gnus-soup-buffers (cons idx-buf gnus-soup-buffers))
223 (buffer-disable-undo idx-buf)))
225 ;; Make sure the last char in the buffer is a newline.
226 (goto-char (point-max))
227 (or (= (current-column) 0)
230 (goto-char (point-min))
232 (gnus-mail-strip-quoted-names
233 (or (mail-fetch-field "from")
234 (mail-fetch-field "really-from")
235 (mail-fetch-field "sender"))))
236 (goto-char (point-min))
237 ;; Depending on what encoding is supposed to be used, we make
241 ((= gnus-soup-encoding-type ?n)
242 (format "#! rnews %d\n" (buffer-size)))
243 ((= gnus-soup-encoding-type ?m)
244 (while (search-forward "\nFrom " nil t)
245 (replace-match "\n>From " t t))
246 (concat "From " (or from "unknown")
247 " " (current-time-string) "\n"))
248 ((= gnus-soup-encoding-type ?M)
250 (t (error "Unsupported type: %c" gnus-soup-encoding-type))))
251 ;; Insert the soup header and the article in the MSG buf.
253 (goto-char (point-max))
256 (insert-buffer-substring article-buf)
257 ;; Insert the index in the IDX buf.
260 (gnus-soup-insert-idx beg headers))
262 (error "Unknown index type: %c" type)))
263 ;; Return the MSG buf.
266 (defun gnus-soup-group-brew (group &optional not-all)
267 "Enter GROUP and add all articles to a SOUP package.
268 If NOT-ALL, don't pack ticked articles."
269 (let ((gnus-expert-user t)
270 (gnus-large-newsgroup nil)
271 (entry (gnus-gethash group gnus-newsrc-hashtb)))
272 (when (or (null entry)
277 (gnus-range-length (cdr (assq 'tick (gnus-info-marks
279 (when (gnus-summary-read-group group nil t)
280 (setq gnus-newsgroup-processable
283 (append gnus-newsgroup-marked gnus-newsgroup-unreads)
284 gnus-newsgroup-unreads)))
285 (gnus-soup-add-article nil)
286 (gnus-summary-exit)))))
288 (defun gnus-soup-insert-idx (offset header)
289 ;; [number subject from date id references chars lines xref]
290 (goto-char (point-max))
292 (format "%d\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t\t\n"
294 (or (mail-header-subject header) "(none)")
295 (or (mail-header-from header) "(nobody)")
296 (or (mail-header-date header) "")
297 (or (mail-header-id header)
298 (concat "soup-dummy-id-"
300 (lambda (time) (int-to-string time))
301 (current-time) "-")))
302 (or (mail-header-references header) "")
303 (or (mail-header-chars header) 0)
304 (or (mail-header-lines header) "0"))))
306 (defun gnus-soup-save-areas ()
307 (gnus-soup-write-areas)
310 (while gnus-soup-buffers
311 (setq buf (car gnus-soup-buffers)
312 gnus-soup-buffers (cdr gnus-soup-buffers))
313 (if (not (buffer-name buf))
316 (and (buffer-modified-p) (save-buffer))
317 (kill-buffer (current-buffer)))))
318 (gnus-soup-write-prefixes)))
320 (defun gnus-soup-write-prefixes ()
321 (let ((prefix gnus-soup-last-prefix))
324 (gnus-set-work-buffer)
325 (insert (format "(setq gnus-soup-prev-prefix %d)\n" (cdar prefix)))
326 (gnus-make-directory (caar prefix))
327 (write-region (point-min) (point-max)
328 (concat (caar prefix) gnus-soup-prefix-file)
330 (setq prefix (cdr prefix))))))
332 (defun gnus-soup-pack (dir packer)
333 (let* ((files (mapconcat 'identity
334 '("AREAS" "*.MSG" "*.IDX" "INFO"
335 "LIST" "REPLIES" "COMMANDS" "ERRORS")
337 (packer (if (< (string-match "%s" packer)
338 (string-match "%d" packer))
340 (string-to-int (gnus-soup-unique-prefix dir)))
342 (string-to-int (gnus-soup-unique-prefix dir))
344 (dir (expand-file-name dir)))
345 (or (file-directory-p dir)
346 (gnus-make-directory dir))
347 (setq gnus-soup-areas nil)
348 (message "Packing %s..." packer)
349 (if (zerop (call-process "sh" nil nil nil "-c"
350 (concat "cd " dir " ; " packer)))
352 (call-process "sh" nil nil nil "-c"
353 (concat "cd " dir " ; rm " files))
354 (message "Packing...done" packer))
355 (error "Couldn't pack packet."))))
357 (defun gnus-soup-parse-areas (file)
358 "Parse soup area file FILE.
359 The result is a of vectors, each containing one entry from the AREA file.
360 The vector contain five strings,
361 [prefix name encoding description number]
362 though the two last may be nil if they are missing."
365 (set-buffer (find-file-noselect file 'force))
366 (buffer-disable-undo (current-buffer))
367 (goto-char (point-min))
370 (cons (vector (gnus-soup-field)
373 (and (eq (preceding-char) ?\t)
375 (and (eq (preceding-char) ?\t)
376 (string-to-int (gnus-soup-field))))
378 (if (eq (preceding-char) ?\t)
379 (beginning-of-line 2)))
380 (kill-buffer (current-buffer)))
383 (defun gnus-soup-parse-replies (file)
384 "Parse soup REPLIES file FILE.
385 The result is a of vectors, each containing one entry from the REPLIES
386 file. The vector contain three strings, [prefix name encoding]."
389 (set-buffer (find-file-noselect file))
390 (buffer-disable-undo (current-buffer))
391 (goto-char (point-min))
394 (cons (vector (gnus-soup-field) (gnus-soup-field)
397 (if (eq (preceding-char) ?\t)
398 (beginning-of-line 2)))
399 (kill-buffer (current-buffer)))
402 (defun gnus-soup-field ()
404 (buffer-substring (point) (progn (skip-chars-forward "^\t\n") (point)))
407 (defun gnus-soup-read-areas ()
409 (setq gnus-soup-areas
410 (gnus-soup-parse-areas (concat gnus-soup-directory "AREAS")))))
412 (defun gnus-soup-write-areas ()
413 "Write the AREAS file."
415 (when gnus-soup-areas
416 (nnheader-temp-write (concat gnus-soup-directory "AREAS")
417 (let ((areas gnus-soup-areas)
419 (while (setq area (pop areas))
423 (gnus-soup-area-prefix area)
424 (gnus-soup-area-name area)
425 (gnus-soup-area-encoding area)
426 (if (or (gnus-soup-area-description area)
427 (gnus-soup-area-number area))
428 (concat "\t" (or (gnus-soup-area-description
430 (if (gnus-soup-area-number area)
431 (concat "\t" (int-to-string
432 (gnus-soup-area-number area)))
435 (defun gnus-soup-write-replies (dir areas)
436 "Write a REPLIES file in DIR containing AREAS."
437 (nnheader-temp-write (concat dir "REPLIES")
439 (while (setq area (pop areas))
440 (insert (format "%s\t%s\t%s\n"
441 (gnus-soup-reply-prefix area)
442 (gnus-soup-reply-kind area)
443 (gnus-soup-reply-encoding area)))))))
445 (defun gnus-soup-area (group)
446 (gnus-soup-read-areas)
447 (let ((areas gnus-soup-areas)
448 (real-group (gnus-group-real-name group))
451 (setq area (car areas)
453 (if (equal (gnus-soup-area-name area) real-group)
457 (vector (gnus-soup-unique-prefix)
460 gnus-soup-encoding-type
462 (if (gnus-member-of-valid 'mail group) ?m ?n))
464 gnus-soup-areas (cons result gnus-soup-areas)))
467 (defun gnus-soup-unique-prefix (&optional dir)
468 (let* ((dir (file-name-as-directory (or dir gnus-soup-directory)))
469 (entry (assoc dir gnus-soup-last-prefix))
470 gnus-soup-prev-prefix)
473 (and (file-exists-p (concat dir gnus-soup-prefix-file))
475 (load (concat dir gnus-soup-prefix-file) nil t t)
477 (setq gnus-soup-last-prefix
478 (cons (setq entry (cons dir (or gnus-soup-prev-prefix 0)))
479 gnus-soup-last-prefix)))
480 (setcdr entry (1+ (cdr entry)))
481 (gnus-soup-write-prefixes)
482 (int-to-string (cdr entry))))
484 (defun gnus-soup-unpack-packet (dir unpacker packet)
485 "Unpack PACKET into DIR using UNPACKER.
486 Return whether the unpacking was successful."
487 (gnus-make-directory dir)
488 (message "Unpacking: %s" (format unpacker packet))
491 "sh" nil nil nil "-c"
492 (format "cd %s ; %s" (expand-file-name dir)
493 (format unpacker packet))))
494 (message "Unpacking...done")))
496 (defun gnus-soup-send-packet (packet)
497 (gnus-soup-unpack-packet
498 gnus-soup-replies-directory gnus-soup-unpacker packet)
499 (let ((replies (gnus-soup-parse-replies
500 (concat gnus-soup-replies-directory "REPLIES"))))
503 (let* ((msg-file (concat gnus-soup-replies-directory
504 (gnus-soup-reply-prefix (car replies))
506 (msg-buf (and (file-exists-p msg-file)
507 (find-file-noselect msg-file)))
508 (tmp-buf (get-buffer-create " *soup send*"))
511 ((/= (gnus-soup-encoding-format
512 (gnus-soup-reply-encoding (car replies))) ?n)
513 (error "Unsupported encoding"))
517 (buffer-disable-undo msg-buf)
518 (buffer-disable-undo tmp-buf)
520 (goto-char (point-min))
522 (or (looking-at "#! *rnews +\\([0-9]+\\)")
523 (error "Bad header."))
526 end (+ (point) (string-to-int
528 (match-beginning 1) (match-end 1)))))
529 (switch-to-buffer tmp-buf)
531 (insert-buffer-substring msg-buf beg end)
532 (goto-char (point-min))
533 (search-forward "\n\n")
535 (insert mail-header-separator)
537 ((string= (gnus-soup-reply-kind (car replies)) "news")
538 (message "Sending news message to %s..."
539 (mail-fetch-field "newsgroups"))
541 (funcall message-send-news-function))
542 ((string= (gnus-soup-reply-kind (car replies)) "mail")
543 (message "Sending mail to %s..."
544 (mail-fetch-field "to"))
546 (funcall message-send-mail-function))
548 (error "Unknown reply kind")))
551 (delete-file (buffer-file-name))
552 (kill-buffer msg-buf)
553 (kill-buffer tmp-buf)
554 (message "Sent packet"))))
555 (setq replies (cdr replies)))
560 ;;; gnus-soup.el ends here