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 (nnheader-concat gnus-home-directory "SoupBrew/")
38 "*Directory containing an unpacked SOUP packet.")
40 (defvar gnus-soup-replies-directory
41 (nnheader-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 gnus-home-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 (when (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 (unless (file-exists-p gnus-soup-directory)
170 (message "No such directory: %s" gnus-soup-directory))
171 (when (null (directory-files gnus-soup-directory nil "\\.MSG$"))
172 (message "No files to pack."))
173 (gnus-soup-pack gnus-soup-directory gnus-soup-packer))
175 (defun gnus-group-brew-soup (n)
176 "Make a soup packet from the current group.
177 Uses the process/prefix convention."
179 (let ((groups (gnus-group-process-prefix n)))
181 (gnus-group-remove-mark (car groups))
182 (gnus-soup-group-brew (car groups) t)
183 (setq groups (cdr groups)))
184 (gnus-soup-save-areas)))
186 (defun gnus-brew-soup (&optional level)
187 "Go through all groups on LEVEL or less and make a soup packet."
189 (let ((level (or level gnus-level-subscribed))
190 (newsrc (cdr gnus-newsrc-alist)))
192 (when (<= (nth 1 (car newsrc)) level)
193 (gnus-soup-group-brew (caar newsrc) t))
194 (setq newsrc (cdr newsrc)))
195 (gnus-soup-save-areas)))
198 (defun gnus-batch-brew-soup ()
199 "Brew a SOUP packet from groups mention on the command line.
200 Will use the remaining command line arguments as regular expressions
201 for matching on group names.
203 For instance, if you want to brew on all the nnml groups, as well as
204 groups with \"emacs\" in the name, you could say something like:
206 $ emacs -batch -f gnus-batch-brew-soup ^nnml \".*emacs.*\""
210 ;;; Internal Functions:
212 ;; Store the current buffer.
213 (defun gnus-soup-store (directory prefix headers format index)
214 ;; Create the directory, if needed.
215 (gnus-make-directory directory)
216 (let* ((msg-buf (nnheader-find-file-noselect
217 (concat directory prefix ".MSG")))
218 (idx-buf (if (= index ?n)
220 (nnheader-find-file-noselect
221 (concat directory prefix ".IDX"))))
222 (article-buf (current-buffer))
223 from head-line beg type)
224 (setq gnus-soup-buffers (cons msg-buf (delq msg-buf gnus-soup-buffers)))
225 (buffer-disable-undo msg-buf)
227 (push idx-buf gnus-soup-buffers)
228 (buffer-disable-undo idx-buf))
230 ;; Make sure the last char in the buffer is a newline.
231 (goto-char (point-max))
232 (unless (= (current-column) 0)
235 (goto-char (point-min))
237 (gnus-mail-strip-quoted-names
238 (or (mail-fetch-field "from")
239 (mail-fetch-field "really-from")
240 (mail-fetch-field "sender"))))
241 (goto-char (point-min))
242 ;; Depending on what encoding is supposed to be used, we make
246 ((= gnus-soup-encoding-type ?n)
247 (format "#! rnews %d\n" (buffer-size)))
248 ((= gnus-soup-encoding-type ?m)
249 (while (search-forward "\nFrom " nil t)
250 (replace-match "\n>From " t t))
251 (concat "From " (or from "unknown")
252 " " (current-time-string) "\n"))
253 ((= gnus-soup-encoding-type ?M)
255 (t (error "Unsupported type: %c" gnus-soup-encoding-type))))
256 ;; Insert the soup header and the article in the MSG buf.
258 (goto-char (point-max))
261 (insert-buffer-substring article-buf)
262 ;; Insert the index in the IDX buf.
265 (gnus-soup-insert-idx beg headers))
267 (error "Unknown index type: %c" type)))
268 ;; Return the MSG buf.
271 (defun gnus-soup-group-brew (group &optional not-all)
272 "Enter GROUP and add all articles to a SOUP package.
273 If NOT-ALL, don't pack ticked articles."
274 (let ((gnus-expert-user t)
275 (gnus-large-newsgroup nil)
276 (entry (gnus-gethash group gnus-newsrc-hashtb)))
277 (when (or (null entry)
282 (gnus-range-length (cdr (assq 'tick (gnus-info-marks
284 (when (gnus-summary-read-group group nil t)
285 (setq gnus-newsgroup-processable
288 (append gnus-newsgroup-marked gnus-newsgroup-unreads)
289 gnus-newsgroup-unreads)))
290 (gnus-soup-add-article nil)
291 (gnus-summary-exit)))))
293 (defun gnus-soup-insert-idx (offset header)
294 ;; [number subject from date id references chars lines xref]
295 (goto-char (point-max))
297 (format "%d\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t\t\n"
299 (or (mail-header-subject header) "(none)")
300 (or (mail-header-from header) "(nobody)")
301 (or (mail-header-date header) "")
302 (or (mail-header-id header)
303 (concat "soup-dummy-id-"
305 (lambda (time) (int-to-string time))
306 (current-time) "-")))
307 (or (mail-header-references header) "")
308 (or (mail-header-chars header) 0)
309 (or (mail-header-lines header) "0"))))
311 (defun gnus-soup-save-areas ()
312 (gnus-soup-write-areas)
315 (while gnus-soup-buffers
316 (setq buf (car gnus-soup-buffers)
317 gnus-soup-buffers (cdr gnus-soup-buffers))
318 (if (not (buffer-name buf))
321 (when (buffer-modified-p)
323 (kill-buffer (current-buffer)))))
324 (gnus-soup-write-prefixes)))
326 (defun gnus-soup-write-prefixes ()
327 (let ((prefixes gnus-soup-last-prefix)
330 (gnus-set-work-buffer)
331 (while (setq prefix (pop prefixes))
333 (insert (format "(setq gnus-soup-prev-prefix %d)\n" (cdr prefix)))
334 (gnus-write-buffer (concat (car prefix) gnus-soup-prefix-file))))))
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 (gnus-make-directory dir)
350 (setq gnus-soup-areas nil)
351 (gnus-message 4 "Packing %s..." packer)
352 (if (zerop (call-process shell-file-name
353 nil nil nil shell-command-switch
354 (concat "cd " dir " ; " packer)))
356 (call-process shell-file-name nil nil nil shell-command-switch
357 (concat "cd " dir " ; rm " files))
358 (gnus-message 4 "Packing...done" packer))
359 (error "Couldn't pack packet"))))
361 (defun gnus-soup-parse-areas (file)
362 "Parse soup area file FILE.
363 The result is a of vectors, each containing one entry from the AREA file.
364 The vector contain five strings,
365 [prefix name encoding description number]
366 though the two last may be nil if they are missing."
369 (set-buffer (nnheader-find-file-noselect file 'force))
370 (buffer-disable-undo (current-buffer))
371 (goto-char (point-min))
373 (push (vector (gnus-soup-field)
376 (and (eq (preceding-char) ?\t)
378 (and (eq (preceding-char) ?\t)
379 (string-to-int (gnus-soup-field))))
381 (when (eq (preceding-char) ?\t)
382 (beginning-of-line 2)))
383 (kill-buffer (current-buffer)))
386 (defun gnus-soup-parse-replies (file)
387 "Parse soup REPLIES file FILE.
388 The result is a of vectors, each containing one entry from the REPLIES
389 file. The vector contain three strings, [prefix name encoding]."
392 (set-buffer (nnheader-find-file-noselect file))
393 (buffer-disable-undo (current-buffer))
394 (goto-char (point-min))
396 (push (vector (gnus-soup-field) (gnus-soup-field)
399 (when (eq (preceding-char) ?\t)
400 (beginning-of-line 2)))
401 (kill-buffer (current-buffer)))
404 (defun gnus-soup-field ()
406 (buffer-substring (point) (progn (skip-chars-forward "^\t\n") (point)))
409 (defun gnus-soup-read-areas ()
411 (setq gnus-soup-areas
412 (gnus-soup-parse-areas (concat gnus-soup-directory "AREAS")))))
414 (defun gnus-soup-write-areas ()
415 "Write the AREAS file."
417 (when gnus-soup-areas
418 (nnheader-temp-write (concat gnus-soup-directory "AREAS")
419 (let ((areas gnus-soup-areas)
421 (while (setq area (pop areas))
425 (gnus-soup-area-prefix area)
426 (gnus-soup-area-name area)
427 (gnus-soup-area-encoding area)
428 (if (or (gnus-soup-area-description area)
429 (gnus-soup-area-number area))
430 (concat "\t" (or (gnus-soup-area-description
432 (if (gnus-soup-area-number area)
433 (concat "\t" (int-to-string
434 (gnus-soup-area-number area)))
437 (defun gnus-soup-write-replies (dir areas)
438 "Write a REPLIES file in DIR containing AREAS."
439 (nnheader-temp-write (concat dir "REPLIES")
441 (while (setq area (pop 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)))))))
447 (defun gnus-soup-area (group)
448 (gnus-soup-read-areas)
449 (let ((areas gnus-soup-areas)
450 (real-group (gnus-group-real-name group))
453 (setq area (car areas)
455 (when (equal (gnus-soup-area-name area) real-group)
459 (vector (gnus-soup-unique-prefix)
462 gnus-soup-encoding-type
464 (if (gnus-member-of-valid 'mail group) ?m ?n))
466 gnus-soup-areas (cons result gnus-soup-areas)))
469 (defun gnus-soup-unique-prefix (&optional dir)
470 (let* ((dir (file-name-as-directory (or dir gnus-soup-directory)))
471 (entry (assoc dir gnus-soup-last-prefix))
472 gnus-soup-prev-prefix)
475 (when (file-exists-p (concat dir gnus-soup-prefix-file))
477 (load (concat dir gnus-soup-prefix-file) nil t t)))
478 (push (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 (gnus-message 4 "Unpacking: %s" (format unpacker packet))
491 shell-file-name nil nil nil shell-command-switch
492 (format "cd %s ; %s" (expand-file-name dir)
493 (format unpacker packet))))
494 (gnus-message 4 "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 (nnheader-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)))
514 (error "Unsupported encoding"))
518 (buffer-disable-undo msg-buf)
519 (buffer-disable-undo tmp-buf)
521 (goto-char (point-min))
523 (unless (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)
537 (setq message-newsreader (setq message-mailer
538 (gnus-extended-version)))
540 ((string= (gnus-soup-reply-kind (car replies)) "news")
541 (gnus-message 5 "Sending news message to %s..."
542 (mail-fetch-field "newsgroups"))
544 (let ((message-syntax-checks
545 'dont-check-for-anything-just-trust-me))
546 (funcall message-send-news-function)))
547 ((string= (gnus-soup-reply-kind (car replies)) "mail")
548 (gnus-message 5 "Sending mail to %s..."
549 (mail-fetch-field "to"))
553 (error "Unknown reply kind")))
556 (delete-file (buffer-file-name))
557 (kill-buffer msg-buf)
558 (kill-buffer tmp-buf)
559 (gnus-message 4 "Sent packet"))))
560 (setq replies (cdr replies)))
565 ;;; gnus-soup.el ends here