("latin-1-prefix"): Change ~s to give \e,A'\e(B and
[emacs.git] / lisp / gnus / gnus-soup.el
blob09b58a7c8a3c7244dc56c008d38637ec7e816af5
1 ;;; gnus-soup.el --- SOUP packet writing support for Gnus
2 ;; Copyright (C) 1995,96,97,98 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)
13 ;; any later version.
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.
25 ;;; Commentary:
27 ;;; Code:
29 (eval-when-compile (require 'cl))
31 (eval-when-compile (require 'cl))
33 (require 'gnus)
34 (require 'gnus-art)
35 (require 'message)
36 (require 'gnus-start)
37 (require 'gnus-range)
39 ;;; User Variables:
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 ?n
73 "*Soup encoding type.
74 `n' is news format, `m' is Unix mbox format, and `M' is MMDF mailbox
75 format.")
77 (defvar gnus-soup-index-type ?c
78 "*Soup index type.
79 `n' means no index file and `c' means standard Cnews overview
80 format.")
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)
87 ;;; Access macros:
89 (defmacro gnus-soup-area-prefix (area)
90 `(aref ,area 0))
91 (defmacro gnus-soup-set-area-prefix (area prefix)
92 `(aset ,area 0 ,prefix))
93 (defmacro gnus-soup-area-name (area)
94 `(aref ,area 1))
95 (defmacro gnus-soup-area-encoding (area)
96 `(aref ,area 2))
97 (defmacro gnus-soup-area-description (area)
98 `(aref ,area 3))
99 (defmacro gnus-soup-area-number (area)
100 `(aref ,area 4))
101 (defmacro gnus-soup-area-set-number (area value)
102 `(aset ,area 4 ,value))
104 (defmacro gnus-soup-encoding-format (encoding)
105 `(aref ,encoding 0))
106 (defmacro gnus-soup-encoding-index (encoding)
107 `(aref ,encoding 1))
108 (defmacro gnus-soup-encoding-kind (encoding)
109 `(aref ,encoding 2))
111 (defmacro gnus-soup-reply-prefix (reply)
112 `(aref ,reply 0))
113 (defmacro gnus-soup-reply-kind (reply)
114 `(aref ,reply 1))
115 (defmacro gnus-soup-reply-encoding (reply)
116 `(aref ,reply 2))
118 ;;; Commands:
120 (defun gnus-soup-send-replies ()
121 "Unpack and send all replies in the reply packet."
122 (interactive)
123 (let ((packets (directory-files
124 gnus-soup-packet-directory t gnus-soup-packet-regexp)))
125 (while packets
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."
136 (interactive "P")
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))
141 headers)
142 (buffer-disable-undo tmp-buf)
143 (save-excursion
144 (while articles
145 ;; Find the header of the article.
146 (set-buffer gnus-summary-buffer)
147 (when (setq headers (gnus-summary-article-header (car articles)))
148 ;; Put the article in a buffer.
149 (set-buffer tmp-buf)
150 (when (gnus-request-article-this-buffer
151 (car articles) gnus-newsgroup-name)
152 (save-restriction
153 (message-narrow-to-head)
154 (message-remove-header gnus-soup-ignored-headers t))
155 (gnus-soup-store gnus-soup-directory prefix headers
156 gnus-soup-encoding-type
157 gnus-soup-index-type)
158 (gnus-soup-area-set-number
159 area (1+ (or (gnus-soup-area-number area) 0)))))
160 ;; Mark article as read.
161 (set-buffer gnus-summary-buffer)
162 (gnus-summary-remove-process-mark (car articles))
163 (gnus-summary-mark-as-read (car articles) gnus-souped-mark)
164 (setq articles (cdr articles)))
165 (kill-buffer tmp-buf))
166 (gnus-soup-save-areas)
167 (gnus-set-mode-line 'summary)))
169 (defun gnus-soup-pack-packet ()
170 "Make a SOUP packet from the SOUP areas."
171 (interactive)
172 (gnus-soup-read-areas)
173 (unless (file-exists-p gnus-soup-directory)
174 (message "No such directory: %s" gnus-soup-directory))
175 (when (null (directory-files gnus-soup-directory nil "\\.MSG$"))
176 (message "No files to pack."))
177 (gnus-soup-pack gnus-soup-directory gnus-soup-packer))
179 (defun gnus-group-brew-soup (n)
180 "Make a soup packet from the current group.
181 Uses the process/prefix convention."
182 (interactive "P")
183 (let ((groups (gnus-group-process-prefix n)))
184 (while groups
185 (gnus-group-remove-mark (car groups))
186 (gnus-soup-group-brew (car groups) t)
187 (setq groups (cdr groups)))
188 (gnus-soup-save-areas)))
190 (defun gnus-brew-soup (&optional level)
191 "Go through all groups on LEVEL or less and make a soup packet."
192 (interactive "P")
193 (let ((level (or level gnus-level-subscribed))
194 (newsrc (cdr gnus-newsrc-alist)))
195 (while newsrc
196 (when (<= (nth 1 (car newsrc)) level)
197 (gnus-soup-group-brew (caar newsrc) t))
198 (setq newsrc (cdr newsrc)))
199 (gnus-soup-save-areas)))
201 ;;;###autoload
202 (defun gnus-batch-brew-soup ()
203 "Brew a SOUP packet from groups mention on the command line.
204 Will use the remaining command line arguments as regular expressions
205 for matching on group names.
207 For instance, if you want to brew on all the nnml groups, as well as
208 groups with \"emacs\" in the name, you could say something like:
210 $ emacs -batch -f gnus-batch-brew-soup ^nnml \".*emacs.*\"
212 Note -- this function hasn't been implemented yet."
213 (interactive)
214 nil)
216 ;;; Internal Functions:
218 ;; Store the current buffer.
219 (defun gnus-soup-store (directory prefix headers format index)
220 ;; Create the directory, if needed.
221 (gnus-make-directory directory)
222 (let* ((msg-buf (nnheader-find-file-noselect
223 (concat directory prefix ".MSG")))
224 (idx-buf (if (= index ?n)
226 (nnheader-find-file-noselect
227 (concat directory prefix ".IDX"))))
228 (article-buf (current-buffer))
229 from head-line beg type)
230 (setq gnus-soup-buffers (cons msg-buf (delq msg-buf gnus-soup-buffers)))
231 (buffer-disable-undo msg-buf)
232 (when idx-buf
233 (push idx-buf gnus-soup-buffers)
234 (buffer-disable-undo idx-buf))
235 (save-excursion
236 ;; Make sure the last char in the buffer is a newline.
237 (goto-char (point-max))
238 (unless (= (current-column) 0)
239 (insert "\n"))
240 ;; Find the "from".
241 (goto-char (point-min))
242 (setq from
243 (gnus-mail-strip-quoted-names
244 (or (mail-fetch-field "from")
245 (mail-fetch-field "really-from")
246 (mail-fetch-field "sender"))))
247 (goto-char (point-min))
248 ;; Depending on what encoding is supposed to be used, we make
249 ;; a soup header.
250 (setq head-line
251 (cond
252 ((= gnus-soup-encoding-type ?n)
253 (format "#! rnews %d\n" (buffer-size)))
254 ((= gnus-soup-encoding-type ?m)
255 (while (search-forward "\nFrom " nil t)
256 (replace-match "\n>From " t t))
257 (concat "From " (or from "unknown")
258 " " (current-time-string) "\n"))
259 ((= gnus-soup-encoding-type ?M)
260 "\^a\^a\^a\^a\n")
261 (t (error "Unsupported type: %c" gnus-soup-encoding-type))))
262 ;; Insert the soup header and the article in the MSG buf.
263 (set-buffer msg-buf)
264 (goto-char (point-max))
265 (insert head-line)
266 (setq beg (point))
267 (insert-buffer-substring article-buf)
268 ;; Insert the index in the IDX buf.
269 (cond ((= index ?c)
270 (set-buffer idx-buf)
271 (gnus-soup-insert-idx beg headers))
272 ((/= index ?n)
273 (error "Unknown index type: %c" type)))
274 ;; Return the MSG buf.
275 msg-buf)))
277 (defun gnus-soup-group-brew (group &optional not-all)
278 "Enter GROUP and add all articles to a SOUP package.
279 If NOT-ALL, don't pack ticked articles."
280 (let ((gnus-expert-user t)
281 (gnus-large-newsgroup nil)
282 (entry (gnus-gethash group gnus-newsrc-hashtb)))
283 (when (or (null entry)
284 (eq (car entry) t)
285 (and (car entry)
286 (> (car entry) 0))
287 (and (not not-all)
288 (gnus-range-length (cdr (assq 'tick (gnus-info-marks
289 (nth 2 entry)))))))
290 (when (gnus-summary-read-group group nil t)
291 (setq gnus-newsgroup-processable
292 (reverse
293 (if (not not-all)
294 (append gnus-newsgroup-marked gnus-newsgroup-unreads)
295 gnus-newsgroup-unreads)))
296 (gnus-soup-add-article nil)
297 (gnus-summary-exit)))))
299 (defun gnus-soup-insert-idx (offset header)
300 ;; [number subject from date id references chars lines xref]
301 (goto-char (point-max))
302 (insert
303 (format "%d\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t\t\n"
304 offset
305 (or (mail-header-subject header) "(none)")
306 (or (mail-header-from header) "(nobody)")
307 (or (mail-header-date header) "")
308 (or (mail-header-id header)
309 (concat "soup-dummy-id-"
310 (mapconcat
311 (lambda (time) (int-to-string time))
312 (current-time) "-")))
313 (or (mail-header-references header) "")
314 (or (mail-header-chars header) 0)
315 (or (mail-header-lines header) "0"))))
317 (defun gnus-soup-save-areas ()
318 "Write all SOUP buffers."
319 (interactive)
320 (gnus-soup-write-areas)
321 (save-excursion
322 (let (buf)
323 (while gnus-soup-buffers
324 (setq buf (car gnus-soup-buffers)
325 gnus-soup-buffers (cdr gnus-soup-buffers))
326 (if (not (buffer-name buf))
328 (set-buffer buf)
329 (when (buffer-modified-p)
330 (save-buffer))
331 (kill-buffer (current-buffer)))))
332 (gnus-soup-write-prefixes)))
334 (defun gnus-soup-write-prefixes ()
335 (let ((prefixes gnus-soup-last-prefix)
336 prefix)
337 (save-excursion
338 (gnus-set-work-buffer)
339 (while (setq prefix (pop prefixes))
340 (erase-buffer)
341 (insert (format "(setq gnus-soup-prev-prefix %d)\n" (cdr prefix)))
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")
348 " "))
349 (packer (if (< (string-match "%s" packer)
350 (string-match "%d" packer))
351 (format packer files
352 (string-to-int (gnus-soup-unique-prefix dir)))
353 (format packer
354 (string-to-int (gnus-soup-unique-prefix dir))
355 files)))
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)))
363 (progn
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."
375 (let (areas)
376 (when (file-exists-p file)
377 (save-excursion
378 (set-buffer (nnheader-find-file-noselect file 'force))
379 (buffer-disable-undo (current-buffer))
380 (goto-char (point-min))
381 (while (not (eobp))
382 (push (vector (gnus-soup-field)
383 (gnus-soup-field)
384 (gnus-soup-field)
385 (and (eq (preceding-char) ?\t)
386 (gnus-soup-field))
387 (and (eq (preceding-char) ?\t)
388 (string-to-int (gnus-soup-field))))
389 areas)
390 (when (eq (preceding-char) ?\t)
391 (beginning-of-line 2)))
392 (kill-buffer (current-buffer))))
393 areas))
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]."
399 (let (replies)
400 (save-excursion
401 (set-buffer (nnheader-find-file-noselect file))
402 (buffer-disable-undo (current-buffer))
403 (goto-char (point-min))
404 (while (not (eobp))
405 (push (vector (gnus-soup-field) (gnus-soup-field)
406 (gnus-soup-field))
407 replies)
408 (when (eq (preceding-char) ?\t)
409 (beginning-of-line 2)))
410 (kill-buffer (current-buffer)))
411 replies))
413 (defun gnus-soup-field ()
414 (prog1
415 (buffer-substring (point) (progn (skip-chars-forward "^\t\n") (point)))
416 (forward-char 1)))
418 (defun gnus-soup-read-areas ()
419 (or gnus-soup-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."
425 (interactive)
426 (when gnus-soup-areas
427 (nnheader-temp-write (concat gnus-soup-directory "AREAS")
428 (let ((areas gnus-soup-areas)
429 area)
430 (while (setq area (pop areas))
431 (insert
432 (format
433 "%s\t%s\t%s%s\n"
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
440 area) "")
441 (if (gnus-soup-area-number area)
442 (concat "\t" (int-to-string
443 (gnus-soup-area-number area)))
444 "")) ""))))))))
446 (defun gnus-soup-write-replies (dir areas)
447 "Write a REPLIES file in DIR containing AREAS."
448 (nnheader-temp-write (concat dir "REPLIES")
449 (let (area)
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))
460 area result)
461 (while areas
462 (setq area (car areas)
463 areas (cdr areas))
464 (when (equal (gnus-soup-area-name area) real-group)
465 (setq result area)))
466 (unless result
467 (setq result
468 (vector (gnus-soup-unique-prefix)
469 real-group
470 (format "%c%c%c"
471 gnus-soup-encoding-type
472 gnus-soup-index-type
473 (if (gnus-member-of-valid 'mail group) ?m ?n))
474 nil nil)
475 gnus-soup-areas (cons result gnus-soup-areas)))
476 result))
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)
482 (if entry
484 (when (file-exists-p (concat dir gnus-soup-prefix-file))
485 (ignore-errors
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))
498 (prog1
499 (zerop (call-process
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"))))
510 (save-excursion
511 (while replies
512 (let* ((msg-file (concat gnus-soup-replies-directory
513 (gnus-soup-reply-prefix (car replies))
514 ".MSG"))
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*"))
518 beg end)
519 (cond
520 ((/= (gnus-soup-encoding-format
521 (gnus-soup-reply-encoding (car replies)))
523 (error "Unsupported encoding"))
524 ((null msg-buf)
527 (buffer-disable-undo msg-buf)
528 (set-buffer msg-buf)
529 (goto-char (point-min))
530 (while (not (eobp))
531 (unless (looking-at "#! *rnews +\\([0-9]+\\)")
532 (error "Bad header"))
533 (forward-line 1)
534 (setq beg (point)
535 end (+ (point) (string-to-int
536 (buffer-substring
537 (match-beginning 1) (match-end 1)))))
538 (switch-to-buffer tmp-buf)
539 (erase-buffer)
540 (insert-buffer-substring msg-buf beg end)
541 (goto-char (point-min))
542 (search-forward "\n\n")
543 (forward-char -1)
544 (insert mail-header-separator)
545 (setq message-newsreader (setq message-mailer
546 (gnus-extended-version)))
547 (cond
548 ((string= (gnus-soup-reply-kind (car replies)) "news")
549 (gnus-message 5 "Sending news message to %s..."
550 (mail-fetch-field "newsgroups"))
551 (sit-for 1)
552 (let ((message-syntax-checks
553 'dont-check-for-anything-just-trust-me))
554 (funcall message-send-news-function)))
555 ((string= (gnus-soup-reply-kind (car replies)) "mail")
556 (gnus-message 5 "Sending mail to %s..."
557 (mail-fetch-field "to"))
558 (sit-for 1)
559 (message-send-mail))
561 (error "Unknown reply kind")))
562 (set-buffer msg-buf)
563 (goto-char end))
564 (delete-file (buffer-file-name))
565 (kill-buffer msg-buf)
566 (kill-buffer tmp-buf)
567 (gnus-message 4 "Sent packet"))))
568 (setq replies (cdr replies)))
569 t)))
571 (provide 'gnus-soup)
573 ;;; gnus-soup.el ends here