1 ;;; gnus-soup.el --- SOUP packet writing support for Gnus
2 ;; Copyright (C) 1995,96,97,98,99 Free Software Foundation, Inc.
4 ;; Author: Per Abrahamsen <abraham@iesd.auc.dk>
5 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
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.
29 (eval-when-compile (require 'cl))
39 (defvar gnus-soup-directory (nnheader-concat gnus-home-directory "SoupBrew/")
40 "*Directory containing an unpacked SOUP packet.")
42 (defvar gnus-soup-replies-directory
43 (nnheader-concat gnus-soup-directory "SoupReplies/")
44 "*Directory where Gnus will do processing of replies.")
46 (defvar gnus-soup-prefix-file "gnus-prefix"
47 "*Name of the file where Gnus stores the last used prefix.")
49 (defvar gnus-soup-packer "tar cf - %s | gzip > $HOME/Soupout%d.tgz"
50 "Format string command for packing a SOUP packet.
51 The SOUP files will be inserted where the %s is in the string.
52 This string MUST contain both %s and %d. The file number will be
53 inserted where %d appears.")
55 (defvar gnus-soup-unpacker "gunzip -c %s | tar xvf -"
56 "*Format string command for unpacking a SOUP packet.
57 The SOUP packet file name will be inserted at the %s.")
59 (defvar gnus-soup-packet-directory gnus-home-directory
60 "*Where gnus-soup will look for REPLIES packets.")
62 (defvar gnus-soup-packet-regexp "Soupin"
63 "*Regular expression matching SOUP REPLIES packets in `gnus-soup-packet-directory'.")
65 (defvar gnus-soup-ignored-headers "^Xref:"
66 "*Regexp to match headers to be removed when brewing SOUP packets.")
68 ;;; Internal Variables:
70 (defvar gnus-soup-encoding-type ?n
72 `n' is news format, `m' is Unix mbox format, and `M' is MMDF mailbox
75 (defvar gnus-soup-index-type ?c
77 `n' means no index file and `c' means standard Cnews overview
80 (defvar gnus-soup-areas nil)
81 (defvar gnus-soup-last-prefix nil)
82 (defvar gnus-soup-prev-prefix nil)
83 (defvar gnus-soup-buffers nil)
87 (defmacro gnus-soup-area-prefix (area)
89 (defmacro gnus-soup-set-area-prefix (area prefix)
90 `(aset ,area 0 ,prefix))
91 (defmacro gnus-soup-area-name (area)
93 (defmacro gnus-soup-area-encoding (area)
95 (defmacro gnus-soup-area-description (area)
97 (defmacro gnus-soup-area-number (area)
99 (defmacro gnus-soup-area-set-number (area value)
100 `(aset ,area 4 ,value))
102 (defmacro gnus-soup-encoding-format (encoding)
104 (defmacro gnus-soup-encoding-index (encoding)
106 (defmacro gnus-soup-encoding-kind (encoding)
109 (defmacro gnus-soup-reply-prefix (reply)
111 (defmacro gnus-soup-reply-kind (reply)
113 (defmacro gnus-soup-reply-encoding (reply)
118 (defun gnus-soup-send-replies ()
119 "Unpack and send all replies in the reply packet."
121 (let ((packets (directory-files
122 gnus-soup-packet-directory t gnus-soup-packet-regexp)))
124 (when (gnus-soup-send-packet (car packets))
125 (delete-file (car packets)))
126 (setq packets (cdr packets)))))
128 (defun gnus-soup-add-article (n)
129 "Add the current article to SOUP packet.
130 If N is a positive number, add the N next articles.
131 If N is a negative number, add the N previous articles.
132 If N is nil and any articles have been marked with the process mark,
133 move those articles instead."
135 (let* ((articles (gnus-summary-work-articles n))
136 (tmp-buf (gnus-get-buffer-create "*soup work*"))
137 (area (gnus-soup-area gnus-newsgroup-name))
138 (prefix (gnus-soup-area-prefix area))
140 (buffer-disable-undo tmp-buf)
143 ;; Put the article in a buffer.
145 (when (gnus-request-article-this-buffer
146 (car articles) gnus-newsgroup-name)
147 (setq headers (nnheader-parse-head t))
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)
163 (gnus-set-mode-line 'summary)))
165 (defun gnus-soup-pack-packet ()
166 "Make a SOUP packet from the SOUP areas."
168 (gnus-soup-read-areas)
169 (if (file-exists-p gnus-soup-directory)
170 (if (directory-files gnus-soup-directory nil "\\.MSG$")
171 (gnus-soup-pack gnus-soup-directory gnus-soup-packer)
172 (message "No files to pack."))
173 (message "No such directory: %s" gnus-soup-directory)))
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.*\"
208 Note -- this function hasn't been implemented yet."
212 ;;; Internal Functions:
214 ;; Store the current buffer.
215 (defun gnus-soup-store (directory prefix headers format index)
216 ;; Create the directory, if needed.
217 (gnus-make-directory directory)
218 (let* ((msg-buf (nnheader-find-file-noselect
219 (concat directory prefix ".MSG")))
220 (idx-buf (if (= index ?n)
222 (nnheader-find-file-noselect
223 (concat directory prefix ".IDX"))))
224 (article-buf (current-buffer))
225 from head-line beg type)
226 (setq gnus-soup-buffers (cons msg-buf (delq msg-buf gnus-soup-buffers)))
227 (buffer-disable-undo msg-buf)
229 (push idx-buf gnus-soup-buffers)
230 (buffer-disable-undo idx-buf))
232 ;; Make sure the last char in the buffer is a newline.
233 (goto-char (point-max))
234 (unless (= (current-column) 0)
237 (goto-char (point-min))
239 (gnus-mail-strip-quoted-names
240 (or (mail-fetch-field "from")
241 (mail-fetch-field "really-from")
242 (mail-fetch-field "sender"))))
243 (goto-char (point-min))
244 ;; Depending on what encoding is supposed to be used, we make
248 ((= gnus-soup-encoding-type ?n)
249 (format "#! rnews %d\n" (buffer-size)))
250 ((= gnus-soup-encoding-type ?m)
251 (while (search-forward "\nFrom " nil t)
252 (replace-match "\n>From " t t))
253 (concat "From " (or from "unknown")
254 " " (current-time-string) "\n"))
255 ((= gnus-soup-encoding-type ?M)
257 (t (error "Unsupported type: %c" gnus-soup-encoding-type))))
258 ;; Insert the soup header and the article in the MSG buf.
260 (goto-char (point-max))
263 (insert-buffer-substring article-buf)
264 ;; Insert the index in the IDX buf.
267 (gnus-soup-insert-idx beg headers))
269 (error "Unknown index type: %c" type)))
270 ;; Return the MSG buf.
273 (defun gnus-soup-group-brew (group &optional not-all)
274 "Enter GROUP and add all articles to a SOUP package.
275 If NOT-ALL, don't pack ticked articles."
276 (let ((gnus-expert-user t)
277 (gnus-large-newsgroup nil)
278 (entry (gnus-gethash group gnus-newsrc-hashtb)))
279 (when (or (null entry)
284 (gnus-range-length (cdr (assq 'tick (gnus-info-marks
286 (when (gnus-summary-read-group group nil t)
287 (setq gnus-newsgroup-processable
290 (append gnus-newsgroup-marked gnus-newsgroup-unreads)
291 gnus-newsgroup-unreads)))
292 (gnus-soup-add-article nil)
293 (gnus-summary-exit)))))
295 (defun gnus-soup-insert-idx (offset header)
296 ;; [number subject from date id references chars lines xref]
297 (goto-char (point-max))
299 (format "%d\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t\t\n"
301 (or (mail-header-subject header) "(none)")
302 (or (mail-header-from header) "(nobody)")
303 (or (mail-header-date header) "")
304 (or (mail-header-id header)
305 (concat "soup-dummy-id-"
307 (lambda (time) (int-to-string time))
308 (current-time) "-")))
309 (or (mail-header-references header) "")
310 (or (mail-header-chars header) 0)
311 (or (mail-header-lines header) "0"))))
313 (defun gnus-soup-save-areas ()
314 "Write all SOUP buffers."
316 (gnus-soup-write-areas)
319 (while gnus-soup-buffers
320 (setq buf (car gnus-soup-buffers)
321 gnus-soup-buffers (cdr gnus-soup-buffers))
322 (if (not (buffer-name buf))
325 (when (buffer-modified-p)
327 (kill-buffer (current-buffer)))))
328 (gnus-soup-write-prefixes)))
330 (defun gnus-soup-write-prefixes ()
331 (let ((prefixes gnus-soup-last-prefix)
334 (gnus-set-work-buffer)
335 (while (setq prefix (pop prefixes))
337 (insert (format "(setq gnus-soup-prev-prefix %d)\n" (cdr prefix)))
338 (gnus-write-buffer (concat (car prefix) gnus-soup-prefix-file))))))
340 (defun gnus-soup-pack (dir packer)
341 (let* ((files (mapconcat 'identity
342 '("AREAS" "*.MSG" "*.IDX" "INFO"
343 "LIST" "REPLIES" "COMMANDS" "ERRORS")
345 (packer (if (< (string-match "%s" packer)
346 (string-match "%d" packer))
348 (string-to-int (gnus-soup-unique-prefix dir)))
350 (string-to-int (gnus-soup-unique-prefix dir))
352 (dir (expand-file-name dir)))
353 (gnus-make-directory dir)
354 (setq gnus-soup-areas nil)
355 (gnus-message 4 "Packing %s..." packer)
356 (if (zerop (call-process shell-file-name
357 nil nil nil shell-command-switch
358 (concat "cd " dir " ; " packer)))
360 (call-process shell-file-name nil nil nil shell-command-switch
361 (concat "cd " dir " ; rm " files))
362 (gnus-message 4 "Packing...done" packer))
363 (error "Couldn't pack packet"))))
365 (defun gnus-soup-parse-areas (file)
366 "Parse soup area file FILE.
367 The result is a of vectors, each containing one entry from the AREA file.
368 The vector contain five strings,
369 [prefix name encoding description number]
370 though the two last may be nil if they are missing."
372 (when (file-exists-p file)
374 (set-buffer (nnheader-find-file-noselect file 'force))
375 (buffer-disable-undo)
376 (goto-char (point-min))
378 (push (vector (gnus-soup-field)
381 (and (eq (preceding-char) ?\t)
383 (and (eq (preceding-char) ?\t)
384 (string-to-int (gnus-soup-field))))
386 (when (eq (preceding-char) ?\t)
387 (beginning-of-line 2)))
388 (kill-buffer (current-buffer))))
391 (defun gnus-soup-parse-replies (file)
392 "Parse soup REPLIES file FILE.
393 The result is a of vectors, each containing one entry from the REPLIES
394 file. The vector contain three strings, [prefix name encoding]."
397 (set-buffer (nnheader-find-file-noselect file))
398 (buffer-disable-undo)
399 (goto-char (point-min))
401 (push (vector (gnus-soup-field) (gnus-soup-field)
404 (when (eq (preceding-char) ?\t)
405 (beginning-of-line 2)))
406 (kill-buffer (current-buffer)))
409 (defun gnus-soup-field ()
411 (buffer-substring (point) (progn (skip-chars-forward "^\t\n") (point)))
414 (defun gnus-soup-read-areas ()
416 (setq gnus-soup-areas
417 (gnus-soup-parse-areas (concat gnus-soup-directory "AREAS")))))
419 (defun gnus-soup-write-areas ()
420 "Write the AREAS file."
422 (when gnus-soup-areas
423 (with-temp-file (concat gnus-soup-directory "AREAS")
424 (let ((areas gnus-soup-areas)
426 (while (setq area (pop areas))
430 (gnus-soup-area-prefix area)
431 (gnus-soup-area-name area)
432 (gnus-soup-area-encoding area)
433 (if (or (gnus-soup-area-description area)
434 (gnus-soup-area-number area))
435 (concat "\t" (or (gnus-soup-area-description
437 (if (gnus-soup-area-number area)
438 (concat "\t" (int-to-string
439 (gnus-soup-area-number area)))
442 (defun gnus-soup-write-replies (dir areas)
443 "Write a REPLIES file in DIR containing AREAS."
444 (with-temp-file (concat dir "REPLIES")
446 (while (setq area (pop areas))
447 (insert (format "%s\t%s\t%s\n"
448 (gnus-soup-reply-prefix area)
449 (gnus-soup-reply-kind area)
450 (gnus-soup-reply-encoding area)))))))
452 (defun gnus-soup-area (group)
453 (gnus-soup-read-areas)
454 (let ((areas gnus-soup-areas)
455 (real-group (gnus-group-real-name group))
458 (setq area (car areas)
460 (when (equal (gnus-soup-area-name area) real-group)
464 (vector (gnus-soup-unique-prefix)
467 gnus-soup-encoding-type
469 (if (gnus-member-of-valid 'mail group) ?m ?n))
471 gnus-soup-areas (cons result gnus-soup-areas)))
474 (defun gnus-soup-unique-prefix (&optional dir)
475 (let* ((dir (file-name-as-directory (or dir gnus-soup-directory)))
476 (entry (assoc dir gnus-soup-last-prefix))
477 gnus-soup-prev-prefix)
480 (when (file-exists-p (concat dir gnus-soup-prefix-file))
482 (load (concat dir gnus-soup-prefix-file) nil t t)))
483 (push (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 (gnus-make-directory dir)
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 (nnheader-find-file-noselect msg-file)))
513 (tmp-buf (gnus-get-buffer-create " *soup send*"))
516 ((/= (gnus-soup-encoding-format
517 (gnus-soup-reply-encoding (car replies)))
519 (error "Unsupported encoding"))
523 (buffer-disable-undo msg-buf)
525 (goto-char (point-min))
527 (unless (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 (let ((message-syntax-checks
549 'dont-check-for-anything-just-trust-me))
550 (funcall message-send-news-function)))
551 ((string= (gnus-soup-reply-kind (car replies)) "mail")
552 (gnus-message 5 "Sending mail to %s..."
553 (mail-fetch-field "to"))
557 (error "Unknown reply kind")))
560 (delete-file (buffer-file-name))
561 (kill-buffer msg-buf)
562 (kill-buffer tmp-buf)
563 (gnus-message 4 "Sent packet"))))
564 (setq replies (cdr replies)))
569 ;;; gnus-soup.el ends here