1 ;;; gnus-soup.el --- SOUP packet writing support for Gnus
2 ;; Copyright (C) 1995,96,97 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.
37 (defvar gnus-soup-directory "~/SoupBrew/"
38 "*Directory containing an unpacked SOUP packet.")
40 (defvar gnus-soup-replies-directory (concat gnus-soup-directory "SoupReplies/")
41 "*Directory where Gnus will do processing of replies.")
43 (defvar gnus-soup-prefix-file "gnus-prefix"
44 "*Name of the file where Gnus stores the last used prefix.")
46 (defvar gnus-soup-packer "tar cf - %s | gzip > $HOME/Soupout%d.tgz"
47 "Format string command for packing a SOUP packet.
48 The SOUP files will be inserted where the %s is in the string.
49 This string MUST contain both %s and %d. The file number will be
50 inserted where %d appears.")
52 (defvar gnus-soup-unpacker "gunzip -c %s | tar xvf -"
53 "*Format string command for unpacking a SOUP packet.
54 The SOUP packet file name will be inserted at the %s.")
56 (defvar gnus-soup-packet-directory "~/"
57 "*Where gnus-soup will look for REPLIES packets.")
59 (defvar gnus-soup-packet-regexp "Soupin"
60 "*Regular expression matching SOUP REPLIES packets in `gnus-soup-packet-directory'.")
62 (defvar gnus-soup-ignored-headers "^Xref:"
63 "*Regexp to match headers to be removed when brewing SOUP packets.")
65 ;;; Internal Variables:
67 (defvar gnus-soup-encoding-type ?n
69 `n' is news format, `m' is Unix mbox format, and `M' is MMDF mailbox
72 (defvar gnus-soup-index-type ?c
74 `n' means no index file and `c' means standard Cnews overview
77 (defvar gnus-soup-areas nil)
78 (defvar gnus-soup-last-prefix nil)
79 (defvar gnus-soup-prev-prefix nil)
80 (defvar gnus-soup-buffers nil)
84 (defmacro gnus-soup-area-prefix (area)
86 (defmacro gnus-soup-set-area-prefix (area prefix)
87 `(aset ,area 0 ,prefix))
88 (defmacro gnus-soup-area-name (area)
90 (defmacro gnus-soup-area-encoding (area)
92 (defmacro gnus-soup-area-description (area)
94 (defmacro gnus-soup-area-number (area)
96 (defmacro gnus-soup-area-set-number (area value)
97 `(aset ,area 4 ,value))
99 (defmacro gnus-soup-encoding-format (encoding)
101 (defmacro gnus-soup-encoding-index (encoding)
103 (defmacro gnus-soup-encoding-kind (encoding)
106 (defmacro gnus-soup-reply-prefix (reply)
108 (defmacro gnus-soup-reply-kind (reply)
110 (defmacro gnus-soup-reply-encoding (reply)
115 (defun gnus-soup-send-replies ()
116 "Unpack and send all replies in the reply packet."
118 (let ((packets (directory-files
119 gnus-soup-packet-directory t gnus-soup-packet-regexp)))
121 (when (gnus-soup-send-packet (car packets))
122 (delete-file (car packets)))
123 (setq packets (cdr packets)))))
125 (defun gnus-soup-add-article (n)
126 "Add the current article to SOUP packet.
127 If N is a positive number, add the N next articles.
128 If N is a negative number, add the N previous articles.
129 If N is nil and any articles have been marked with the process mark,
130 move those articles instead."
132 (gnus-set-global-variables)
133 (let* ((articles (gnus-summary-work-articles n))
134 (tmp-buf (get-buffer-create "*soup work*"))
135 (area (gnus-soup-area gnus-newsgroup-name))
136 (prefix (gnus-soup-area-prefix area))
138 (buffer-disable-undo tmp-buf)
141 ;; Find the header of the article.
142 (set-buffer gnus-summary-buffer)
143 (when (setq headers (gnus-summary-article-header (car articles)))
144 ;; Put the article in a buffer.
146 (when (gnus-request-article-this-buffer
147 (car articles) gnus-newsgroup-name)
149 (message-narrow-to-head)
150 (message-remove-header gnus-soup-ignored-headers t))
151 (gnus-soup-store gnus-soup-directory prefix headers
152 gnus-soup-encoding-type
153 gnus-soup-index-type)
154 (gnus-soup-area-set-number
155 area (1+ (or (gnus-soup-area-number area) 0)))))
156 ;; Mark article as read.
157 (set-buffer gnus-summary-buffer)
158 (gnus-summary-remove-process-mark (car articles))
159 (gnus-summary-mark-as-read (car articles) gnus-souped-mark)
160 (setq articles (cdr articles)))
161 (kill-buffer tmp-buf))
162 (gnus-soup-save-areas)))
164 (defun gnus-soup-pack-packet ()
165 "Make a SOUP packet from the SOUP areas."
167 (gnus-soup-read-areas)
168 (unless (file-exists-p gnus-soup-directory)
169 (message "No such directory: %s" gnus-soup-directory))
170 (when (null (directory-files gnus-soup-directory nil "\\.MSG$"))
171 (message "No files to pack."))
172 (gnus-soup-pack gnus-soup-directory gnus-soup-packer))
174 (defun gnus-group-brew-soup (n)
175 "Make a soup packet from the current group.
176 Uses the process/prefix convention."
178 (let ((groups (gnus-group-process-prefix n)))
180 (gnus-group-remove-mark (car groups))
181 (gnus-soup-group-brew (car groups) t)
182 (setq groups (cdr groups)))
183 (gnus-soup-save-areas)))
185 (defun gnus-brew-soup (&optional level)
186 "Go through all groups on LEVEL or less and make a soup packet."
188 (let ((level (or level gnus-level-subscribed))
189 (newsrc (cdr gnus-newsrc-alist)))
191 (when (<= (nth 1 (car newsrc)) level)
192 (gnus-soup-group-brew (caar newsrc) t))
193 (setq newsrc (cdr newsrc)))
194 (gnus-soup-save-areas)))
197 (defun gnus-batch-brew-soup ()
198 "Brew a SOUP packet from groups mention on the command line.
199 Will use the remaining command line arguments as regular expressions
200 for matching on group names.
202 For instance, if you want to brew on all the nnml groups, as well as
203 groups with \"emacs\" in the name, you could say something like:
205 $ emacs -batch -f gnus-batch-brew-soup ^nnml \".*emacs.*\""
209 ;;; Internal Functions:
211 ;; Store the current buffer.
212 (defun gnus-soup-store (directory prefix headers format index)
213 ;; Create the directory, if needed.
214 (gnus-make-directory directory)
215 (let* ((msg-buf (nnheader-find-file-noselect
216 (concat directory prefix ".MSG")))
217 (idx-buf (if (= index ?n)
219 (nnheader-find-file-noselect
220 (concat directory prefix ".IDX"))))
221 (article-buf (current-buffer))
222 from head-line beg type)
223 (setq gnus-soup-buffers (cons msg-buf (delq msg-buf gnus-soup-buffers)))
224 (buffer-disable-undo msg-buf)
226 (push idx-buf gnus-soup-buffers)
227 (buffer-disable-undo idx-buf))
229 ;; Make sure the last char in the buffer is a newline.
230 (goto-char (point-max))
231 (unless (= (current-column) 0)
234 (goto-char (point-min))
236 (gnus-mail-strip-quoted-names
237 (or (mail-fetch-field "from")
238 (mail-fetch-field "really-from")
239 (mail-fetch-field "sender"))))
240 (goto-char (point-min))
241 ;; Depending on what encoding is supposed to be used, we make
245 ((= gnus-soup-encoding-type ?n)
246 (format "#! rnews %d\n" (buffer-size)))
247 ((= gnus-soup-encoding-type ?m)
248 (while (search-forward "\nFrom " nil t)
249 (replace-match "\n>From " t t))
250 (concat "From " (or from "unknown")
251 " " (current-time-string) "\n"))
252 ((= gnus-soup-encoding-type ?M)
254 (t (error "Unsupported type: %c" gnus-soup-encoding-type))))
255 ;; Insert the soup header and the article in the MSG buf.
257 (goto-char (point-max))
260 (insert-buffer-substring article-buf)
261 ;; Insert the index in the IDX buf.
264 (gnus-soup-insert-idx beg headers))
266 (error "Unknown index type: %c" type)))
267 ;; Return the MSG buf.
270 (defun gnus-soup-group-brew (group &optional not-all)
271 "Enter GROUP and add all articles to a SOUP package.
272 If NOT-ALL, don't pack ticked articles."
273 (let ((gnus-expert-user t)
274 (gnus-large-newsgroup nil)
275 (entry (gnus-gethash group gnus-newsrc-hashtb)))
276 (when (or (null entry)
281 (gnus-range-length (cdr (assq 'tick (gnus-info-marks
283 (when (gnus-summary-read-group group nil t)
284 (setq gnus-newsgroup-processable
287 (append gnus-newsgroup-marked gnus-newsgroup-unreads)
288 gnus-newsgroup-unreads)))
289 (gnus-soup-add-article nil)
290 (gnus-summary-exit)))))
292 (defun gnus-soup-insert-idx (offset header)
293 ;; [number subject from date id references chars lines xref]
294 (goto-char (point-max))
296 (format "%d\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t\t\n"
298 (or (mail-header-subject header) "(none)")
299 (or (mail-header-from header) "(nobody)")
300 (or (mail-header-date header) "")
301 (or (mail-header-id header)
302 (concat "soup-dummy-id-"
304 (lambda (time) (int-to-string time))
305 (current-time) "-")))
306 (or (mail-header-references header) "")
307 (or (mail-header-chars header) 0)
308 (or (mail-header-lines header) "0"))))
310 (defun gnus-soup-save-areas ()
311 (gnus-soup-write-areas)
314 (while gnus-soup-buffers
315 (setq buf (car gnus-soup-buffers)
316 gnus-soup-buffers (cdr gnus-soup-buffers))
317 (if (not (buffer-name buf))
320 (when (buffer-modified-p)
322 (kill-buffer (current-buffer)))))
323 (gnus-soup-write-prefixes)))
325 (defun gnus-soup-write-prefixes ()
326 (let ((prefixes gnus-soup-last-prefix)
329 (gnus-set-work-buffer)
330 (while (setq prefix (pop prefixes))
332 (insert (format "(setq gnus-soup-prev-prefix %d)\n" (cdr prefix)))
333 (gnus-write-buffer (concat (car prefix) gnus-soup-prefix-file))))))
335 (defun gnus-soup-pack (dir packer)
336 (let* ((files (mapconcat 'identity
337 '("AREAS" "*.MSG" "*.IDX" "INFO"
338 "LIST" "REPLIES" "COMMANDS" "ERRORS")
340 (packer (if (< (string-match "%s" packer)
341 (string-match "%d" packer))
343 (string-to-int (gnus-soup-unique-prefix dir)))
345 (string-to-int (gnus-soup-unique-prefix dir))
347 (dir (expand-file-name dir)))
348 (gnus-make-directory dir)
349 (setq gnus-soup-areas nil)
350 (gnus-message 4 "Packing %s..." packer)
351 (if (zerop (call-process shell-file-name
352 nil nil nil shell-command-switch
353 (concat "cd " dir " ; " packer)))
355 (call-process shell-file-name nil nil nil shell-command-switch
356 (concat "cd " dir " ; rm " files))
357 (gnus-message 4 "Packing...done" packer))
358 (error "Couldn't pack packet."))))
360 (defun gnus-soup-parse-areas (file)
361 "Parse soup area file FILE.
362 The result is a of vectors, each containing one entry from the AREA file.
363 The vector contain five strings,
364 [prefix name encoding description number]
365 though the two last may be nil if they are missing."
368 (set-buffer (nnheader-find-file-noselect file 'force))
369 (buffer-disable-undo (current-buffer))
370 (goto-char (point-min))
372 (push (vector (gnus-soup-field)
375 (and (eq (preceding-char) ?\t)
377 (and (eq (preceding-char) ?\t)
378 (string-to-int (gnus-soup-field))))
380 (when (eq (preceding-char) ?\t)
381 (beginning-of-line 2)))
382 (kill-buffer (current-buffer)))
385 (defun gnus-soup-parse-replies (file)
386 "Parse soup REPLIES file FILE.
387 The result is a of vectors, each containing one entry from the REPLIES
388 file. The vector contain three strings, [prefix name encoding]."
391 (set-buffer (nnheader-find-file-noselect file))
392 (buffer-disable-undo (current-buffer))
393 (goto-char (point-min))
395 (push (vector (gnus-soup-field) (gnus-soup-field)
398 (when (eq (preceding-char) ?\t)
399 (beginning-of-line 2)))
400 (kill-buffer (current-buffer)))
403 (defun gnus-soup-field ()
405 (buffer-substring (point) (progn (skip-chars-forward "^\t\n") (point)))
408 (defun gnus-soup-read-areas ()
410 (setq gnus-soup-areas
411 (gnus-soup-parse-areas (concat gnus-soup-directory "AREAS")))))
413 (defun gnus-soup-write-areas ()
414 "Write the AREAS file."
416 (when gnus-soup-areas
417 (nnheader-temp-write (concat gnus-soup-directory "AREAS")
418 (let ((areas gnus-soup-areas)
420 (while (setq area (pop areas))
424 (gnus-soup-area-prefix area)
425 (gnus-soup-area-name area)
426 (gnus-soup-area-encoding area)
427 (if (or (gnus-soup-area-description area)
428 (gnus-soup-area-number area))
429 (concat "\t" (or (gnus-soup-area-description
431 (if (gnus-soup-area-number area)
432 (concat "\t" (int-to-string
433 (gnus-soup-area-number area)))
436 (defun gnus-soup-write-replies (dir areas)
437 "Write a REPLIES file in DIR containing AREAS."
438 (nnheader-temp-write (concat dir "REPLIES")
440 (while (setq area (pop areas))
441 (insert (format "%s\t%s\t%s\n"
442 (gnus-soup-reply-prefix area)
443 (gnus-soup-reply-kind area)
444 (gnus-soup-reply-encoding area)))))))
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 (when (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 (when (file-exists-p (concat dir gnus-soup-prefix-file))
476 (load (concat dir gnus-soup-prefix-file) nil t t)))
477 (push (setq entry (cons dir (or gnus-soup-prev-prefix 0)))
478 gnus-soup-last-prefix))
479 (setcdr entry (1+ (cdr entry)))
480 (gnus-soup-write-prefixes)
481 (int-to-string (cdr entry))))
483 (defun gnus-soup-unpack-packet (dir unpacker packet)
484 "Unpack PACKET into DIR using UNPACKER.
485 Return whether the unpacking was successful."
486 (gnus-make-directory dir)
487 (gnus-message 4 "Unpacking: %s" (format unpacker packet))
490 shell-file-name nil nil nil shell-command-switch
491 (format "cd %s ; %s" (expand-file-name dir)
492 (format unpacker packet))))
493 (gnus-message 4 "Unpacking...done")))
495 (defun gnus-soup-send-packet (packet)
496 (gnus-soup-unpack-packet
497 gnus-soup-replies-directory gnus-soup-unpacker packet)
498 (let ((replies (gnus-soup-parse-replies
499 (concat gnus-soup-replies-directory "REPLIES"))))
502 (let* ((msg-file (concat gnus-soup-replies-directory
503 (gnus-soup-reply-prefix (car replies))
505 (msg-buf (and (file-exists-p msg-file)
506 (nnheader-find-file-noselect msg-file)))
507 (tmp-buf (get-buffer-create " *soup send*"))
510 ((/= (gnus-soup-encoding-format
511 (gnus-soup-reply-encoding (car replies)))
513 (error "Unsupported encoding"))
517 (buffer-disable-undo msg-buf)
518 (buffer-disable-undo tmp-buf)
520 (goto-char (point-min))
522 (unless (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)
536 (setq message-newsreader (setq message-mailer
537 (gnus-extended-version)))
539 ((string= (gnus-soup-reply-kind (car replies)) "news")
540 (gnus-message 5 "Sending news message to %s..."
541 (mail-fetch-field "newsgroups"))
543 (let ((message-syntax-checks
544 'dont-check-for-anything-just-trust-me))
545 (funcall message-send-news-function)))
546 ((string= (gnus-soup-reply-kind (car replies)) "mail")
547 (gnus-message 5 "Sending mail to %s..."
548 (mail-fetch-field "to"))
552 (error "Unknown reply kind")))
555 (delete-file (buffer-file-name))
556 (kill-buffer msg-buf)
557 (kill-buffer tmp-buf)
558 (gnus-message 4 "Sent packet"))))
559 (setq replies (cdr replies)))
564 ;;; gnus-soup.el ends here