1 ;;; gnus-soup.el --- SOUP packet writing support for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
4 ;; Free Software Foundation, Inc.
6 ;; Author: Per Abrahamsen <abraham@iesd.auc.dk>
7 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; Keywords: news, mail
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
31 (eval-when-compile (require 'cl
))
41 (defvar gnus-soup-directory
(nnheader-concat gnus-home-directory
"SoupBrew/")
42 "*Directory containing an unpacked SOUP packet.")
44 (defvar gnus-soup-replies-directory
45 (nnheader-concat gnus-soup-directory
"SoupReplies/")
46 "*Directory where Gnus will do processing of replies.")
48 (defvar gnus-soup-prefix-file
"gnus-prefix"
49 "*Name of the file where Gnus stores the last used prefix.")
51 (defvar gnus-soup-packer
"tar cf - %s | gzip > $HOME/Soupout%d.tgz"
52 "Format string command for packing a SOUP packet.
53 The SOUP files will be inserted where the %s is in the string.
54 This string MUST contain both %s and %d. The file number will be
55 inserted where %d appears.")
57 (defvar gnus-soup-unpacker
"gunzip -c %s | tar xvf -"
58 "*Format string command for unpacking a SOUP packet.
59 The SOUP packet file name will be inserted at the %s.")
61 (defvar gnus-soup-packet-directory gnus-home-directory
62 "*Where gnus-soup will look for REPLIES packets.")
64 (defvar gnus-soup-packet-regexp
"Soupin"
65 "*Regular expression matching SOUP REPLIES packets in `gnus-soup-packet-directory'.")
67 (defvar gnus-soup-ignored-headers
"^Xref:"
68 "*Regexp to match headers to be removed when brewing SOUP packets.")
70 ;;; Internal Variables:
72 (defvar gnus-soup-encoding-type ?u
74 `u' is USENET news format, `m' is Unix mbox format, and `M' is MMDF mailbox
77 (defvar gnus-soup-index-type ?c
79 `n' means no index file and `c' means standard Cnews overview
82 (defvar gnus-soup-areas nil
)
83 (defvar gnus-soup-last-prefix nil
)
84 (defvar gnus-soup-prev-prefix nil
)
85 (defvar gnus-soup-buffers nil
)
89 (defmacro gnus-soup-area-prefix
(area)
91 (defmacro gnus-soup-set-area-prefix
(area prefix
)
92 `(aset ,area
0 ,prefix
))
93 (defmacro gnus-soup-area-name
(area)
95 (defmacro gnus-soup-area-encoding
(area)
97 (defmacro gnus-soup-area-description
(area)
99 (defmacro gnus-soup-area-number
(area)
101 (defmacro gnus-soup-area-set-number
(area value
)
102 `(aset ,area
4 ,value
))
104 (defmacro gnus-soup-encoding-format
(encoding)
106 (defmacro gnus-soup-encoding-index
(encoding)
108 (defmacro gnus-soup-encoding-kind
(encoding)
111 (defmacro gnus-soup-reply-prefix
(reply)
113 (defmacro gnus-soup-reply-kind
(reply)
115 (defmacro gnus-soup-reply-encoding
(reply)
120 (defun gnus-soup-send-replies ()
121 "Unpack and send all replies in the reply packet."
123 (let ((packets (directory-files
124 gnus-soup-packet-directory t gnus-soup-packet-regexp
)))
126 (when (gnus-soup-send-packet (car packets
))
127 (delete-file (car packets
)))
128 (setq packets
(cdr packets
)))))
130 (defun gnus-soup-add-article (n)
131 "Add the current article to SOUP packet.
132 If N is a positive number, add the N next articles.
133 If N is a negative number, add the N previous articles.
134 If N is nil and any articles have been marked with the process mark,
135 move those articles instead."
137 (let* ((articles (gnus-summary-work-articles n
))
138 (tmp-buf (gnus-get-buffer-create "*soup work*"))
139 (area (gnus-soup-area gnus-newsgroup-name
))
140 (prefix (gnus-soup-area-prefix area
))
142 (buffer-disable-undo tmp-buf
)
145 ;; Put the article in a buffer.
147 (when (gnus-request-article-this-buffer
148 (car articles
) gnus-newsgroup-name
)
149 (setq headers
(nnheader-parse-head t
))
151 (message-narrow-to-head)
152 (message-remove-header gnus-soup-ignored-headers t
))
153 (gnus-soup-store gnus-soup-directory prefix headers
154 gnus-soup-encoding-type
155 gnus-soup-index-type
)
156 (gnus-soup-area-set-number
157 area
(1+ (or (gnus-soup-area-number area
) 0))))
158 ;; Mark article as read.
159 (set-buffer gnus-summary-buffer
)
160 (gnus-summary-remove-process-mark (car articles
))
161 (gnus-summary-mark-as-read (car articles
) gnus-souped-mark
)
162 (setq articles
(cdr articles
)))
163 (kill-buffer tmp-buf
))
164 (gnus-soup-save-areas)
165 (gnus-set-mode-line 'summary
)))
167 (defun gnus-soup-pack-packet ()
168 "Make a SOUP packet from the SOUP areas."
170 (gnus-soup-read-areas)
171 (if (file-exists-p gnus-soup-directory
)
172 (if (directory-files gnus-soup-directory nil
"\\.MSG$")
173 (gnus-soup-pack gnus-soup-directory gnus-soup-packer
)
174 (message "No files to pack."))
175 (message "No such directory: %s" gnus-soup-directory
)))
177 (defun gnus-group-brew-soup (n)
178 "Make a soup packet from the current group.
179 Uses the process/prefix convention."
181 (let ((groups (gnus-group-process-prefix n
)))
183 (gnus-group-remove-mark (car groups
))
184 (gnus-soup-group-brew (car groups
) t
)
185 (setq groups
(cdr groups
)))
186 (gnus-soup-save-areas)))
188 (defun gnus-brew-soup (&optional level
)
189 "Go through all groups on LEVEL or less and make a soup packet."
191 (let ((level (or level gnus-level-subscribed
))
192 (newsrc (cdr gnus-newsrc-alist
)))
194 (when (<= (nth 1 (car newsrc
)) level
)
195 (gnus-soup-group-brew (caar newsrc
) t
))
196 (setq newsrc
(cdr newsrc
)))
197 (gnus-soup-save-areas)))
200 (defun gnus-batch-brew-soup ()
201 "Brew a SOUP packet from groups mention on the command line.
202 Will use the remaining command line arguments as regular expressions
203 for matching on group names.
205 For instance, if you want to brew on all the nnml groups, as well as
206 groups with \"emacs\" in the name, you could say something like:
208 $ emacs -batch -f gnus-batch-brew-soup ^nnml \".*emacs.*\"
210 Note -- this function hasn't been implemented yet."
214 ;;; Internal Functions:
216 ;; Store the current buffer.
217 (defun gnus-soup-store (directory prefix headers format index
)
218 ;; Create the directory, if needed.
219 (gnus-make-directory directory
)
220 (let* ((msg-buf (nnheader-find-file-noselect
221 (concat directory prefix
".MSG")))
222 (idx-buf (if (= index ?n
)
224 (nnheader-find-file-noselect
225 (concat directory prefix
".IDX"))))
226 (article-buf (current-buffer))
227 from head-line beg type
)
228 (setq gnus-soup-buffers
(cons msg-buf
(delq msg-buf gnus-soup-buffers
)))
229 (buffer-disable-undo msg-buf
)
231 (push idx-buf gnus-soup-buffers
)
232 (buffer-disable-undo idx-buf
))
234 ;; Make sure the last char in the buffer is a newline.
235 (goto-char (point-max))
236 (unless (= (current-column) 0)
239 (goto-char (point-min))
241 (gnus-mail-strip-quoted-names
242 (or (mail-fetch-field "from")
243 (mail-fetch-field "really-from")
244 (mail-fetch-field "sender"))))
245 (goto-char (point-min))
246 ;; Depending on what encoding is supposed to be used, we make
250 ((or (= gnus-soup-encoding-type ?u
)
251 (= gnus-soup-encoding-type ?n
)) ;;Gnus back compatibility.
252 (format "#! rnews %d\n" (buffer-size)))
253 ((= gnus-soup-encoding-type ?m
)
254 (while (search-forward "\nFrom " nil t
)
255 (replace-match "\n>From " t t
))
256 (concat "From " (or from
"unknown")
257 " " (current-time-string) "\n"))
258 ((= gnus-soup-encoding-type ?M
)
260 (t (error "Unsupported type: %c" gnus-soup-encoding-type
))))
261 ;; Insert the soup header and the article in the MSG buf.
263 (goto-char (point-max))
266 (insert-buffer-substring article-buf
)
267 ;; Insert the index in the IDX buf.
270 (gnus-soup-insert-idx beg headers
))
272 (error "Unknown index type: %c" type
)))
273 ;; Return the MSG buf.
276 (defun gnus-soup-group-brew (group &optional not-all
)
277 "Enter GROUP and add all articles to a SOUP package.
278 If NOT-ALL, don't pack ticked articles."
279 (let ((gnus-expert-user t
)
280 (gnus-large-newsgroup nil
)
281 (entry (gnus-gethash group gnus-newsrc-hashtb
)))
282 (when (or (null entry
)
287 (gnus-range-length (cdr (assq 'tick
(gnus-info-marks
289 (when (gnus-summary-read-group group nil t
)
290 (setq gnus-newsgroup-processable
293 (append gnus-newsgroup-marked gnus-newsgroup-unreads
)
294 gnus-newsgroup-unreads
)))
295 (gnus-soup-add-article nil
)
296 (gnus-summary-exit)))))
298 (defun gnus-soup-insert-idx (offset header
)
299 ;; [number subject from date id references chars lines xref]
300 (goto-char (point-max))
302 (format "%d\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t\t\n"
304 (or (mail-header-subject header
) "(none)")
305 (or (mail-header-from header
) "(nobody)")
306 (or (mail-header-date header
) "")
307 (or (mail-header-id header
)
308 (concat "soup-dummy-id-"
310 (lambda (time) (int-to-string time
))
311 (current-time) "-")))
312 (or (mail-header-references header
) "")
313 (or (mail-header-chars header
) 0)
314 (or (mail-header-lines header
) "0"))))
316 (defun gnus-soup-save-areas ()
317 "Write all SOUP buffers."
319 (gnus-soup-write-areas)
322 (while gnus-soup-buffers
323 (setq buf
(car gnus-soup-buffers
)
324 gnus-soup-buffers
(cdr gnus-soup-buffers
))
325 (if (not (buffer-name buf
))
328 (when (buffer-modified-p)
330 (kill-buffer (current-buffer)))))
331 (gnus-soup-write-prefixes)))
333 (defun gnus-soup-write-prefixes ()
334 (let ((prefixes gnus-soup-last-prefix
)
337 (gnus-set-work-buffer)
338 (while (setq prefix
(pop prefixes
))
340 (insert (format "(setq gnus-soup-prev-prefix %d)\n" (cdr prefix
)))
341 (let ((coding-system-for-write mm-text-coding-system
))
342 (gnus-write-buffer (concat (car prefix
) gnus-soup-prefix-file
)))))))
344 (defun gnus-soup-pack (dir packer
)
345 (let* ((files (mapconcat 'identity
346 '("AREAS" "*.MSG" "*.IDX" "INFO"
347 "LIST" "REPLIES" "COMMANDS" "ERRORS")
349 (packer (if (< (string-match "%s" packer
)
350 (string-match "%d" packer
))
352 (string-to-int (gnus-soup-unique-prefix dir
)))
354 (string-to-int (gnus-soup-unique-prefix dir
))
356 (dir (expand-file-name dir
)))
357 (gnus-make-directory dir
)
358 (setq gnus-soup-areas nil
)
359 (gnus-message 4 "Packing %s..." packer
)
360 (if (zerop (call-process shell-file-name
361 nil nil nil shell-command-switch
362 (concat "cd " dir
" ; " packer
)))
364 (call-process shell-file-name nil nil nil shell-command-switch
365 (concat "cd " dir
" ; rm " files
))
366 (gnus-message 4 "Packing...done" packer
))
367 (error "Couldn't pack packet"))))
369 (defun gnus-soup-parse-areas (file)
370 "Parse soup area file FILE.
371 The result is a of vectors, each containing one entry from the AREA file.
372 The vector contain five strings,
373 [prefix name encoding description number]
374 though the two last may be nil if they are missing."
376 (when (file-exists-p file
)
378 (set-buffer (nnheader-find-file-noselect file
'force
))
379 (buffer-disable-undo)
380 (goto-char (point-min))
382 (push (vector (gnus-soup-field)
385 (and (eq (preceding-char) ?
\t)
387 (and (eq (preceding-char) ?
\t)
388 (string-to-int (gnus-soup-field))))
390 (when (eq (preceding-char) ?
\t)
391 (beginning-of-line 2)))
392 (kill-buffer (current-buffer))))
395 (defun gnus-soup-parse-replies (file)
396 "Parse soup REPLIES file FILE.
397 The result is a of vectors, each containing one entry from the REPLIES
398 file. The vector contain three strings, [prefix name encoding]."
401 (set-buffer (nnheader-find-file-noselect file
))
402 (buffer-disable-undo)
403 (goto-char (point-min))
405 (push (vector (gnus-soup-field) (gnus-soup-field)
408 (when (eq (preceding-char) ?
\t)
409 (beginning-of-line 2)))
410 (kill-buffer (current-buffer)))
413 (defun gnus-soup-field ()
415 (buffer-substring (point) (progn (skip-chars-forward "^\t\n") (point)))
418 (defun gnus-soup-read-areas ()
420 (setq gnus-soup-areas
421 (gnus-soup-parse-areas (concat gnus-soup-directory
"AREAS")))))
423 (defun gnus-soup-write-areas ()
424 "Write the AREAS file."
426 (when gnus-soup-areas
427 (with-temp-file (concat gnus-soup-directory
"AREAS")
428 (let ((areas gnus-soup-areas
)
430 (while (setq area
(pop areas
))
434 (gnus-soup-area-prefix area
)
435 (gnus-soup-area-name area
)
436 (gnus-soup-area-encoding area
)
437 (if (or (gnus-soup-area-description area
)
438 (gnus-soup-area-number area
))
439 (concat "\t" (or (gnus-soup-area-description
441 (if (gnus-soup-area-number area
)
442 (concat "\t" (int-to-string
443 (gnus-soup-area-number area
)))
446 (defun gnus-soup-write-replies (dir areas
)
447 "Write a REPLIES file in DIR containing AREAS."
448 (with-temp-file (concat dir
"REPLIES")
450 (while (setq area
(pop areas
))
451 (insert (format "%s\t%s\t%s\n"
452 (gnus-soup-reply-prefix area
)
453 (gnus-soup-reply-kind area
)
454 (gnus-soup-reply-encoding area
)))))))
456 (defun gnus-soup-area (group)
457 (gnus-soup-read-areas)
458 (let ((areas gnus-soup-areas
)
459 (real-group (gnus-group-real-name group
))
462 (setq area
(car areas
)
464 (when (equal (gnus-soup-area-name area
) real-group
)
468 (vector (gnus-soup-unique-prefix)
471 gnus-soup-encoding-type
473 (if (gnus-member-of-valid 'mail group
) ?m ?n
))
475 gnus-soup-areas
(cons result gnus-soup-areas
)))
478 (defun gnus-soup-unique-prefix (&optional dir
)
479 (let* ((dir (file-name-as-directory (or dir gnus-soup-directory
)))
480 (entry (assoc dir gnus-soup-last-prefix
))
481 gnus-soup-prev-prefix
)
484 (when (file-exists-p (concat dir gnus-soup-prefix-file
))
486 (load (concat dir gnus-soup-prefix-file
) nil t t
)))
487 (push (setq entry
(cons dir
(or gnus-soup-prev-prefix
0)))
488 gnus-soup-last-prefix
))
489 (setcdr entry
(1+ (cdr entry
)))
490 (gnus-soup-write-prefixes)
491 (int-to-string (cdr entry
))))
493 (defun gnus-soup-unpack-packet (dir unpacker packet
)
494 "Unpack PACKET into DIR using UNPACKER.
495 Return whether the unpacking was successful."
496 (gnus-make-directory dir
)
497 (gnus-message 4 "Unpacking: %s" (format unpacker packet
))
500 shell-file-name nil nil nil shell-command-switch
501 (format "cd %s ; %s" (expand-file-name dir
)
502 (format unpacker packet
))))
503 (gnus-message 4 "Unpacking...done")))
505 (defun gnus-soup-send-packet (packet)
506 (gnus-soup-unpack-packet
507 gnus-soup-replies-directory gnus-soup-unpacker packet
)
508 (let ((replies (gnus-soup-parse-replies
509 (concat gnus-soup-replies-directory
"REPLIES"))))
512 (let* ((msg-file (concat gnus-soup-replies-directory
513 (gnus-soup-reply-prefix (car replies
))
515 (msg-buf (and (file-exists-p msg-file
)
516 (nnheader-find-file-noselect msg-file
)))
517 (tmp-buf (gnus-get-buffer-create " *soup send*"))
520 ((and (/= (gnus-soup-encoding-format
521 (gnus-soup-reply-encoding (car replies
)))
523 (/= (gnus-soup-encoding-format
524 (gnus-soup-reply-encoding (car replies
)))
525 ?n
)) ;; Gnus back compatibility.
526 (error "Unsupported encoding"))
530 (buffer-disable-undo msg-buf
)
532 (goto-char (point-min))
534 (unless (looking-at "#! *rnews +\\([0-9]+\\)")
535 (error "Bad header"))
538 end
(+ (point) (string-to-int
540 (match-beginning 1) (match-end 1)))))
541 (switch-to-buffer tmp-buf
)
543 (insert-buffer-substring msg-buf beg end
)
544 (goto-char (point-min))
545 (search-forward "\n\n")
547 (insert mail-header-separator
)
548 (setq message-newsreader
(setq message-mailer
549 (gnus-extended-version)))
551 ((string= (gnus-soup-reply-kind (car replies
)) "news")
552 (gnus-message 5 "Sending news message to %s..."
553 (mail-fetch-field "newsgroups"))
555 (let ((message-syntax-checks
556 'dont-check-for-anything-just-trust-me
))
557 (funcall message-send-news-function
)))
558 ((string= (gnus-soup-reply-kind (car replies
)) "mail")
559 (gnus-message 5 "Sending mail to %s..."
560 (mail-fetch-field "to"))
564 (error "Unknown reply kind")))
567 (delete-file (buffer-file-name))
568 (kill-buffer msg-buf
)
569 (kill-buffer tmp-buf
)
570 (gnus-message 4 "Sent packet"))))
571 (setq replies
(cdr replies
)))
576 ;;; arch-tag: eddfa69d-13e8-4aea-84ef-62a526ef185c
577 ;;; gnus-soup.el ends here