(defface tooltip): Inherit from variable-pitch.
[emacs.git] / lisp / gnus / gnus-soup.el
blob1f4306869484071bb9362fa3a936ba8c9af0b43b
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)
15 ;; any later version.
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.
27 ;;; Commentary:
29 ;;; Code:
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 ?u
73 "*Soup encoding type.
74 `u' is USENET 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 ;; Put the article in a buffer.
146 (set-buffer tmp-buf)
147 (when (gnus-request-article-this-buffer
148 (car articles) gnus-newsgroup-name)
149 (setq headers (nnheader-parse-head t))
150 (save-restriction
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."
169 (interactive)
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."
180 (interactive "P")
181 (let ((groups (gnus-group-process-prefix n)))
182 (while groups
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."
190 (interactive "P")
191 (let ((level (or level gnus-level-subscribed))
192 (newsrc (cdr gnus-newsrc-alist)))
193 (while newsrc
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)))
199 ;;;###autoload
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."
211 (interactive)
212 nil)
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)
230 (when idx-buf
231 (push idx-buf gnus-soup-buffers)
232 (buffer-disable-undo idx-buf))
233 (save-excursion
234 ;; Make sure the last char in the buffer is a newline.
235 (goto-char (point-max))
236 (unless (= (current-column) 0)
237 (insert "\n"))
238 ;; Find the "from".
239 (goto-char (point-min))
240 (setq from
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
247 ;; a soup header.
248 (setq head-line
249 (cond
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)
259 "\^a\^a\^a\^a\n")
260 (t (error "Unsupported type: %c" gnus-soup-encoding-type))))
261 ;; Insert the soup header and the article in the MSG buf.
262 (set-buffer msg-buf)
263 (goto-char (point-max))
264 (insert head-line)
265 (setq beg (point))
266 (insert-buffer-substring article-buf)
267 ;; Insert the index in the IDX buf.
268 (cond ((= index ?c)
269 (set-buffer idx-buf)
270 (gnus-soup-insert-idx beg headers))
271 ((/= index ?n)
272 (error "Unknown index type: %c" type)))
273 ;; Return the MSG buf.
274 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)
283 (eq (car entry) t)
284 (and (car entry)
285 (> (car entry) 0))
286 (and (not not-all)
287 (gnus-range-length (cdr (assq 'tick (gnus-info-marks
288 (nth 2 entry)))))))
289 (when (gnus-summary-read-group group nil t)
290 (setq gnus-newsgroup-processable
291 (reverse
292 (if (not not-all)
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))
301 (insert
302 (format "%d\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t\t\n"
303 offset
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-"
309 (mapconcat
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."
318 (interactive)
319 (gnus-soup-write-areas)
320 (save-excursion
321 (let (buf)
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))
327 (set-buffer buf)
328 (when (buffer-modified-p)
329 (save-buffer))
330 (kill-buffer (current-buffer)))))
331 (gnus-soup-write-prefixes)))
333 (defun gnus-soup-write-prefixes ()
334 (let ((prefixes gnus-soup-last-prefix)
335 prefix)
336 (save-excursion
337 (gnus-set-work-buffer)
338 (while (setq prefix (pop prefixes))
339 (erase-buffer)
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")
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)
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)
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 (with-temp-file (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 (with-temp-file (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 ((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"))
527 ((null msg-buf)
530 (buffer-disable-undo msg-buf)
531 (set-buffer msg-buf)
532 (goto-char (point-min))
533 (while (not (eobp))
534 (unless (looking-at "#! *rnews +\\([0-9]+\\)")
535 (error "Bad header"))
536 (forward-line 1)
537 (setq beg (point)
538 end (+ (point) (string-to-int
539 (buffer-substring
540 (match-beginning 1) (match-end 1)))))
541 (switch-to-buffer tmp-buf)
542 (erase-buffer)
543 (insert-buffer-substring msg-buf beg end)
544 (goto-char (point-min))
545 (search-forward "\n\n")
546 (forward-char -1)
547 (insert mail-header-separator)
548 (setq message-newsreader (setq message-mailer
549 (gnus-extended-version)))
550 (cond
551 ((string= (gnus-soup-reply-kind (car replies)) "news")
552 (gnus-message 5 "Sending news message to %s..."
553 (mail-fetch-field "newsgroups"))
554 (sit-for 1)
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"))
561 (sit-for 1)
562 (message-send-mail))
564 (error "Unknown reply kind")))
565 (set-buffer msg-buf)
566 (goto-char end))
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)))
572 t)))
574 (provide 'gnus-soup)
576 ;;; gnus-soup.el ends here