Merge from Gnus trunk.
[bpt/emacs.git] / lisp / gnus / gnus-soup.el
CommitLineData
eec82323 1;;; gnus-soup.el --- SOUP packet writing support for Gnus
16409b0b
GM
2
3;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
4;; Free Software Foundation, Inc.
eec82323
LMI
5
6;; Author: Per Abrahamsen <abraham@iesd.auc.dk>
6748645f 7;; Lars Magne Ingebrigtsen <larsi@gnus.org>
eec82323
LMI
8;; Keywords: news, mail
9
10;; This file is part of GNU Emacs.
11
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.
16
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.
21
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.
26
27;;; Commentary:
28
29;;; Code:
30
51bab999
RS
31(eval-when-compile (require 'cl))
32
eec82323
LMI
33(require 'gnus)
34(require 'gnus-art)
35(require 'message)
36(require 'gnus-start)
37(require 'gnus-range)
38
39;;; User Variables:
40
41(defvar gnus-soup-directory (nnheader-concat gnus-home-directory "SoupBrew/")
42 "*Directory containing an unpacked SOUP packet.")
43
44(defvar gnus-soup-replies-directory
45 (nnheader-concat gnus-soup-directory "SoupReplies/")
46 "*Directory where Gnus will do processing of replies.")
47
48(defvar gnus-soup-prefix-file "gnus-prefix"
49 "*Name of the file where Gnus stores the last used prefix.")
50
51(defvar gnus-soup-packer "tar cf - %s | gzip > $HOME/Soupout%d.tgz"
52 "Format string command for packing a SOUP packet.
53The SOUP files will be inserted where the %s is in the string.
54This string MUST contain both %s and %d. The file number will be
55inserted where %d appears.")
56
57(defvar gnus-soup-unpacker "gunzip -c %s | tar xvf -"
58 "*Format string command for unpacking a SOUP packet.
59The SOUP packet file name will be inserted at the %s.")
60
61(defvar gnus-soup-packet-directory gnus-home-directory
62 "*Where gnus-soup will look for REPLIES packets.")
63
64(defvar gnus-soup-packet-regexp "Soupin"
65 "*Regular expression matching SOUP REPLIES packets in `gnus-soup-packet-directory'.")
66
67(defvar gnus-soup-ignored-headers "^Xref:"
68 "*Regexp to match headers to be removed when brewing SOUP packets.")
69
70;;; Internal Variables:
71
16409b0b 72(defvar gnus-soup-encoding-type ?u
eec82323 73 "*Soup encoding type.
16409b0b 74`u' is USENET news format, `m' is Unix mbox format, and `M' is MMDF mailbox
eec82323
LMI
75format.")
76
77(defvar gnus-soup-index-type ?c
78 "*Soup index type.
79`n' means no index file and `c' means standard Cnews overview
80format.")
81
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)
86
87;;; Access macros:
88
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))
103
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))
110
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))
117
118;;; Commands:
119
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)))))
129
130(defun gnus-soup-add-article (n)
131 "Add the current article to SOUP packet.
132If N is a positive number, add the N next articles.
133If N is a negative number, add the N previous articles.
134If N is nil and any articles have been marked with the process mark,
135move those articles instead."
136 (interactive "P")
eec82323 137 (let* ((articles (gnus-summary-work-articles n))
6748645f 138 (tmp-buf (gnus-get-buffer-create "*soup work*"))
eec82323
LMI
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
16409b0b
GM
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))))
eec82323
LMI
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))
6748645f
LMI
164 (gnus-soup-save-areas)
165 (gnus-set-mode-line 'summary)))
eec82323
LMI
166
167(defun gnus-soup-pack-packet ()
168 "Make a SOUP packet from the SOUP areas."
169 (interactive)
170 (gnus-soup-read-areas)
16409b0b
GM
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)))
eec82323
LMI
176
177(defun gnus-group-brew-soup (n)
178 "Make a soup packet from the current group.
179Uses 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)))
187
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)))
198
199;;;###autoload
200(defun gnus-batch-brew-soup ()
201 "Brew a SOUP packet from groups mention on the command line.
202Will use the remaining command line arguments as regular expressions
203for matching on group names.
204
205For instance, if you want to brew on all the nnml groups, as well as
206groups with \"emacs\" in the name, you could say something like:
207
6748645f
LMI
208$ emacs -batch -f gnus-batch-brew-soup ^nnml \".*emacs.*\"
209
210Note -- this function hasn't been implemented yet."
eec82323
LMI
211 (interactive)
212 nil)
213
214;;; Internal Functions:
215
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)
223 nil
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
16409b0b
GM
250 ((or (= gnus-soup-encoding-type ?u)
251 (= gnus-soup-encoding-type ?n)) ;;Gnus back compatibility.
eec82323
LMI
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)))
275
276(defun gnus-soup-group-brew (group &optional not-all)
277 "Enter GROUP and add all articles to a SOUP package.
278If 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)))))
297
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"))))
315
316(defun gnus-soup-save-areas ()
6748645f
LMI
317 "Write all SOUP buffers."
318 (interactive)
eec82323
LMI
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))
326 ()
327 (set-buffer buf)
328 (when (buffer-modified-p)
329 (save-buffer))
330 (kill-buffer (current-buffer)))))
331 (gnus-soup-write-prefixes)))
332
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)))
16409b0b
GM
341 (let ((coding-system-for-write mm-text-coding-system))
342 (gnus-write-buffer (concat (car prefix) gnus-soup-prefix-file)))))))
eec82323
LMI
343
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))
a8151ef7 367 (error "Couldn't pack packet"))))
eec82323
LMI
368
369(defun gnus-soup-parse-areas (file)
370 "Parse soup area file FILE.
371The result is a of vectors, each containing one entry from the AREA file.
372The vector contain five strings,
373 [prefix name encoding description number]
374though the two last may be nil if they are missing."
375 (let (areas)
6748645f
LMI
376 (when (file-exists-p file)
377 (save-excursion
378 (set-buffer (nnheader-find-file-noselect file 'force))
16409b0b 379 (buffer-disable-undo)
6748645f
LMI
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))))
eec82323
LMI
393 areas))
394
395(defun gnus-soup-parse-replies (file)
396 "Parse soup REPLIES file FILE.
397The result is a of vectors, each containing one entry from the REPLIES
398file. The vector contain three strings, [prefix name encoding]."
399 (let (replies)
400 (save-excursion
401 (set-buffer (nnheader-find-file-noselect file))
16409b0b 402 (buffer-disable-undo)
eec82323
LMI
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))
412
413(defun gnus-soup-field ()
414 (prog1
415 (buffer-substring (point) (progn (skip-chars-forward "^\t\n") (point)))
416 (forward-char 1)))
417
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")))))
422
423(defun gnus-soup-write-areas ()
424 "Write the AREAS file."
425 (interactive)
426 (when gnus-soup-areas
16409b0b 427 (with-temp-file (concat gnus-soup-directory "AREAS")
eec82323
LMI
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 "")) ""))))))))
445
446(defun gnus-soup-write-replies (dir areas)
447 "Write a REPLIES file in DIR containing AREAS."
16409b0b 448 (with-temp-file (concat dir "REPLIES")
eec82323
LMI
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)))))))
455
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))
477
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
483 ()
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))))
492
493(defun gnus-soup-unpack-packet (dir unpacker packet)
494 "Unpack PACKET into DIR using UNPACKER.
495Return 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")))
504
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)))
6748645f 517 (tmp-buf (gnus-get-buffer-create " *soup send*"))
eec82323
LMI
518 beg end)
519 (cond
16409b0b
GM
520 ((and (/= (gnus-soup-encoding-format
521 (gnus-soup-reply-encoding (car replies)))
522 ?u)
523 (/= (gnus-soup-encoding-format
524 (gnus-soup-reply-encoding (car replies)))
525 ?n)) ;; Gnus back compatibility.
eec82323
LMI
526 (error "Unsupported encoding"))
527 ((null msg-buf)
528 t)
529 (t
530 (buffer-disable-undo msg-buf)
eec82323
LMI
531 (set-buffer msg-buf)
532 (goto-char (point-min))
533 (while (not (eobp))
534 (unless (looking-at "#! *rnews +\\([0-9]+\\)")
a8151ef7 535 (error "Bad header"))
eec82323
LMI
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))
563 (t
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)))
573
574(provide 'gnus-soup)
575
576;;; gnus-soup.el ends here