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-group-type ?u
71 "*Soup message area type.
72 `u' is unknown, `m' is private mail, and `n' is news.
73 Gnus will determine by itself what type to use in what group, so
74 setting this variable won't do much.")
76 (defvar gnus-soup-areas nil)
77 (defvar gnus-soup-last-prefix nil)
78 (defvar gnus-soup-prev-prefix nil)
79 (defvar gnus-soup-buffers nil)
83 (defmacro gnus-soup-area-prefix (area)
84 (` (aref (, area) 0)))
85 (defmacro gnus-soup-area-name (area)
86 (` (aref (, area) 1)))
87 (defmacro gnus-soup-area-encoding (area)
88 (` (aref (, area) 2)))
89 (defmacro gnus-soup-area-description (area)
90 (` (aref (, area) 3)))
91 (defmacro gnus-soup-area-number (area)
92 (` (aref (, area) 4)))
93 (defmacro gnus-soup-area-set-number (area value)
94 (` (aset (, area) 4 (, value))))
96 (defmacro gnus-soup-encoding-format (encoding)
97 (` (aref (, encoding) 0)))
98 (defmacro gnus-soup-encoding-index (encoding)
99 (` (aref (, encoding) 1)))
100 (defmacro gnus-soup-encoding-kind (encoding)
101 (` (aref (, encoding) 2)))
103 (defmacro gnus-soup-reply-prefix (reply)
104 (` (aref (, reply) 0)))
105 (defmacro gnus-soup-reply-kind (reply)
106 (` (aref (, reply) 1)))
107 (defmacro gnus-soup-reply-encoding (reply)
108 (` (aref (, reply) 2)))
112 (defun gnus-soup-send-replies ()
113 "Unpack and send all replies in the reply packet."
115 (let ((packets (directory-files
116 gnus-soup-packet-directory t gnus-soup-packet-regexp)))
118 (and (gnus-soup-send-packet (car packets))
119 (delete-file (car packets)))
120 (setq packets (cdr packets)))))
122 (defun gnus-soup-add-article (n)
123 "Add the current article to SOUP packet.
124 If N is a positive number, add the N next articles.
125 If N is a negative number, add the N previous articles.
126 If N is nil and any articles have been marked with the process mark,
127 move those articles instead."
129 (gnus-set-global-variables)
130 (let* ((articles (gnus-summary-work-articles n))
131 (tmp-buf (get-buffer-create "*soup work*"))
132 (area (gnus-soup-area gnus-newsgroup-name))
133 (prefix (gnus-soup-area-prefix area))
135 (buffer-disable-undo tmp-buf)
138 ;; Find the header of the article.
139 (set-buffer gnus-summary-buffer)
140 (setq headers (gnus-summary-article-header (car articles)))
141 ;; Put the article in a buffer.
143 (gnus-request-article-this-buffer
144 (car articles) gnus-newsgroup-name)
145 (gnus-soup-store gnus-soup-directory prefix headers
146 gnus-soup-encoding-type
147 gnus-soup-index-type)
148 (gnus-soup-area-set-number area
149 (1+ (or (gnus-soup-area-number area) 0)))
150 ;; Mark article as read.
151 (set-buffer gnus-summary-buffer)
152 (gnus-summary-remove-process-mark (car articles))
153 (gnus-summary-mark-as-read (car articles) gnus-souped-mark)
154 (setq articles (cdr articles)))
155 (kill-buffer tmp-buf))
156 (gnus-soup-save-areas)))
158 (defun gnus-soup-pack-packet ()
159 "Make a SOUP packet from the SOUP areas."
161 (gnus-soup-read-areas)
162 (gnus-soup-pack gnus-soup-directory gnus-soup-packer))
164 (defun gnus-group-brew-soup (n)
165 "Make a soup packet from the current group.
166 Uses the process/prefix convention."
168 (let ((groups (gnus-group-process-prefix n)))
170 (gnus-group-remove-mark (car groups))
171 (gnus-soup-group-brew (car groups))
172 (setq groups (cdr groups)))
173 (gnus-soup-save-areas)))
175 (defun gnus-brew-soup (&optional level)
176 "Go through all groups on LEVEL or less and make a soup packet."
178 (let ((level (or level gnus-level-subscribed))
179 (newsrc (cdr gnus-newsrc-alist)))
181 (and (<= (nth 1 (car newsrc)) level)
182 (gnus-soup-group-brew (car (car newsrc))))
183 (setq newsrc (cdr newsrc)))
184 (gnus-soup-save-areas)))
187 (defun gnus-batch-brew-soup ()
188 "Brew a SOUP packet from groups mention on the command line.
189 Will use the remaining command line arguments as regular expressions
190 for matching on group names.
192 For instance, if you want to brew on all the nnml groups, as well as
193 groups with \"emacs\" in the name, you could say something like:
195 $ emacs -batch -f gnus-batch-brew-soup ^nnml \".*emacs.*\""
199 ;;; Internal Functions:
201 ;; Store the current buffer.
202 (defun gnus-soup-store (directory prefix headers format index)
203 ;; Create the directory, if needed.
204 (or (file-directory-p directory)
205 (gnus-make-directory directory))
206 (let* ((msg-buf (find-file-noselect
207 (concat directory prefix ".MSG")))
208 (idx-buf (if (= index ?n)
211 (concat directory prefix ".IDX"))))
212 (article-buf (current-buffer))
213 from head-line beg type)
214 (setq gnus-soup-buffers (cons msg-buf gnus-soup-buffers))
215 (buffer-disable-undo msg-buf)
218 (setq gnus-soup-buffers (cons idx-buf gnus-soup-buffers))
219 (buffer-disable-undo idx-buf)))
221 ;; Make sure the last char in the buffer is a newline.
222 (goto-char (point-max))
223 (or (= (current-column) 0)
226 (goto-char (point-min))
228 (mail-strip-quoted-names
229 (or (mail-fetch-field "from")
230 (mail-fetch-field "really-from")
231 (mail-fetch-field "sender"))))
232 (goto-char (point-min))
233 ;; Depending on what encoding is supposed to be used, we make
237 ((= gnus-soup-encoding-type ?n)
238 (format "#! rnews %d\n" (buffer-size)))
239 ((= gnus-soup-encoding-type ?m)
240 (while (search-forward "\nFrom " nil t)
241 (replace-match "\n>From " t t))
242 (concat "From " (or from "unknown")
243 " " (current-time-string) "\n"))
244 ((= gnus-soup-encoding-type ?M)
246 (t (error "Unsupported type: %c" gnus-soup-encoding-type))))
247 ;; Insert the soup header and the article in the MSG buf.
249 (goto-char (point-max))
252 (insert-buffer-substring article-buf)
253 ;; Insert the index in the IDX buf.
256 (gnus-soup-insert-idx beg headers))
258 (error "Unknown index type: %c" type))))))
260 (defun gnus-soup-group-brew (group)
261 (let ((gnus-expert-user t)
262 (gnus-large-newsgroup nil))
263 (and (gnus-summary-read-group group)
264 (let ((gnus-newsgroup-processable
265 (gnus-sorted-complement
266 gnus-newsgroup-unreads
267 (append gnus-newsgroup-dormant gnus-newsgroup-marked))))
268 (gnus-soup-add-article nil)))
269 (gnus-summary-exit)))
271 (defun gnus-soup-insert-idx (offset header)
272 ;; [number subject from date id references chars lines xref]
273 (goto-char (point-max))
275 (format "%d\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t\n"
277 (or (mail-header-subject header) "(none)")
278 (or (mail-header-from header) "(nobody)")
279 (or (mail-header-date header) "")
280 (or (mail-header-id header)
281 (concat "soup-dummy-id-"
283 (lambda (time) (int-to-string time))
284 (current-time) "-")))
285 (or (mail-header-references header) "")
286 (or (mail-header-chars header) 0)
287 (or (mail-header-lines header) "0")
288 (or (mail-header-xref header) ""))))
290 (defun gnus-soup-save-areas ()
291 (gnus-soup-write-areas)
294 (while gnus-soup-buffers
295 (setq buf (car gnus-soup-buffers)
296 gnus-soup-buffers (cdr gnus-soup-buffers))
297 (if (not (buffer-name buf))
300 (and (buffer-modified-p) (save-buffer))
301 (kill-buffer (current-buffer)))))
302 (gnus-soup-write-prefixes)))
304 (defun gnus-soup-write-prefixes ()
305 (let ((prefix gnus-soup-last-prefix))
308 (gnus-set-work-buffer)
309 (insert (format "(setq gnus-soup-prev-prefix %d)\n"
311 (write-region (point-min) (point-max)
312 (concat (car (car prefix))
313 gnus-soup-prefix-file)
315 (setq prefix (cdr prefix))))))
317 (defun gnus-soup-pack (dir packer)
318 (let* ((files (mapconcat 'identity
319 '("AREAS" "*.MSG" "*.IDX" "INFO"
320 "LIST" "REPLIES" "COMMANDS" "ERRORS")
322 (packer (if (< (string-match "%s" packer)
323 (string-match "%d" packer))
325 (string-to-int (gnus-soup-unique-prefix dir)))
327 (string-to-int (gnus-soup-unique-prefix dir))
329 (dir (expand-file-name dir)))
330 (or (file-directory-p dir)
331 (gnus-make-directory dir))
332 (setq gnus-soup-areas nil)
333 (message "Packing %s..." packer)
334 (if (zerop (call-process "sh" nil nil nil "-c"
335 (concat "cd " dir " ; " packer)))
337 (call-process "sh" nil nil nil "-c"
338 (concat "cd " dir " ; rm " files))
339 (message "Packing...done" packer))
340 (error "Couldn't pack packet."))))
342 (defun gnus-soup-parse-areas (file)
343 "Parse soup area file FILE.
344 The result is a of vectors, each containing one entry from the AREA file.
345 The vector contain five strings,
346 [prefix name encoding description number]
347 though the two last may be nil if they are missing."
350 (set-buffer (find-file-noselect file 'force))
351 (buffer-disable-undo (current-buffer))
352 (goto-char (point-min))
355 (cons (vector (gnus-soup-field)
358 (and (eq (preceding-char) ?\t)
360 (and (eq (preceding-char) ?\t)
361 (string-to-int (gnus-soup-field))))
363 (if (eq (preceding-char) ?\t)
364 (beginning-of-line 2))))
367 (defun gnus-soup-parse-replies (file)
368 "Parse soup REPLIES file FILE.
369 The result is a of vectors, each containing one entry from the REPLIES
370 file. The vector contain three strings, [prefix name encoding]."
373 (set-buffer (find-file-noselect file))
374 (buffer-disable-undo (current-buffer))
375 (goto-char (point-min))
378 (cons (vector (gnus-soup-field) (gnus-soup-field)
381 (if (eq (preceding-char) ?\t)
382 (beginning-of-line 2)))
383 (kill-buffer (current-buffer)))
386 (defun gnus-soup-field ()
388 (buffer-substring (point) (progn (skip-chars-forward "^\t\n") (point)))
391 (defun gnus-soup-read-areas ()
393 (setq gnus-soup-areas
394 (gnus-soup-parse-areas (concat gnus-soup-directory "AREAS")))))
396 (defun gnus-soup-write-areas ()
397 "Write all areas to disk."
399 (if (not gnus-soup-areas)
402 (set-buffer (find-file-noselect
403 (concat gnus-soup-directory "AREAS")))
404 (buffer-disable-undo (current-buffer))
406 (let ((areas gnus-soup-areas)
409 (setq area (car areas)
411 (insert (format "%s\t%s\t%s%s\n"
412 (gnus-soup-area-prefix area)
413 (gnus-soup-area-name area)
414 (gnus-soup-area-encoding area)
415 (if (or (gnus-soup-area-description area)
416 (gnus-soup-area-number area))
417 (concat "\t" (or (gnus-soup-area-description
420 (if (gnus-soup-area-number area)
423 (gnus-soup-area-number
426 (write-region (point-min) (point-max)
427 (concat gnus-soup-directory "AREAS"))
428 (set-buffer-modified-p nil)
429 (kill-buffer (current-buffer)))))
431 (defun gnus-soup-write-replies (dir areas)
432 (or (file-directory-p dir)
433 (gnus-make-directory dir))
435 (set-buffer (find-file-noselect (concat dir "REPLIES")))
436 (buffer-disable-undo (current-buffer))
440 (setq area (car areas)
442 (insert (format "%s\t%s\t%s\n"
443 (gnus-soup-reply-prefix area)
444 (gnus-soup-reply-kind area)
445 (gnus-soup-reply-encoding area)))))
446 (write-region (point-min) (point-max) (concat dir "REPLIES"))
447 (set-buffer-modified-p nil)
448 (kill-buffer (current-buffer))))
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-file (concat dir gnus-soup-prefix-file))
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 (gnus-make-directory dir)
491 (message "Unpacking: %s" (format unpacker packet))
493 "sh" nil nil nil "-c"
494 (format "cd %s ; %s" (expand-file-name dir) (format unpacker packet)))
495 (message "Unpacking...done"))
497 (defun gnus-soup-send-packet (packet)
498 (gnus-soup-unpack-packet
499 gnus-soup-replies-directory gnus-soup-unpacker packet)
500 (let ((replies (gnus-soup-parse-replies
501 (concat gnus-soup-replies-directory "REPLIES"))))
504 (let* ((msg-file (concat gnus-soup-replies-directory
505 (gnus-soup-reply-prefix (car replies))
507 (msg-buf (and (file-exists-p msg-file)
508 (find-file-noselect msg-file)))
509 (tmp-buf (get-buffer-create " *soup send*"))
512 ((/= (gnus-soup-encoding-format
513 (gnus-soup-reply-encoding (car replies))) ?n)
514 (error "Unsupported encoding"))
518 (buffer-disable-undo msg-buf)
519 (buffer-disable-undo tmp-buf)
521 (goto-char (point-min))
523 (or (looking-at "#! *rnews +\\([0-9]+\\)")
524 (error "Bad header."))
527 end (+ (point) (string-to-int
529 (match-beginning 1) (match-end 1)))))
530 (switch-to-buffer tmp-buf)
532 (insert-buffer-substring msg-buf beg end)
533 (goto-char (point-min))
534 (search-forward "\n\n")
536 (insert mail-header-separator)
538 ((string= (gnus-soup-reply-kind (car replies)) "news")
539 (message "Sending news message to %s..."
540 (mail-fetch-field "newsgroups"))
542 (gnus-inews-article))
543 ((string= (gnus-soup-reply-kind (car replies)) "mail")
544 (message "Sending mail to %s..."
545 (mail-fetch-field "to"))
547 (gnus-mail-send-and-exit))
549 (error "Unknown reply kind")))
552 (delete-file (buffer-file-name))
553 (kill-buffer msg-buf)
554 (kill-buffer tmp-buf)
555 (message "Sent packet"))))
556 (setq replies (cdr replies)))
561 ;;; gnus-soup.el ends here