This commit was generated by cvs2svn to compensate for changes in r1309,
[elisp/gnus.git-] / lisp / gnus-soup.el
1 ;;; gnus-soup.el --- SOUP packet writing support for Gnus
2 ;; Copyright (C) 1995,96,97,98 Free Software Foundation, Inc.
3
4 ;; Author: Per Abrahamsen <abraham@iesd.auc.dk>
5 ;;      Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news, mail
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-art)
33 (require 'message)
34 (require 'gnus-start)
35 (require 'gnus-range)
36
37 ;;; User Variables:
38
39 (defvar gnus-soup-directory (nnheader-concat gnus-home-directory "SoupBrew/")
40   "*Directory containing an unpacked SOUP packet.")
41
42 (defvar gnus-soup-replies-directory
43   (nnheader-concat gnus-soup-directory "SoupReplies/")
44   "*Directory where Gnus will do processing of replies.")
45
46 (defvar gnus-soup-prefix-file "gnus-prefix"
47   "*Name of the file where Gnus stores the last used prefix.")
48
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.")
54
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.")
58
59 (defvar gnus-soup-packet-directory gnus-home-directory
60   "*Where gnus-soup will look for REPLIES packets.")
61
62 (defvar gnus-soup-packet-regexp "Soupin"
63   "*Regular expression matching SOUP REPLIES packets in `gnus-soup-packet-directory'.")
64
65 (defvar gnus-soup-ignored-headers "^Xref:"
66   "*Regexp to match headers to be removed when brewing SOUP packets.")
67
68 ;;; Internal Variables:
69
70 (defvar gnus-soup-encoding-type ?n
71   "*Soup encoding type.
72 `n' is news format, `m' is Unix mbox format, and `M' is MMDF mailbox
73 format.")
74
75 (defvar gnus-soup-index-type ?c
76   "*Soup index type.
77 `n' means no index file and `c' means standard Cnews overview
78 format.")
79
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)
84
85 ;;; Access macros:
86
87 (defmacro gnus-soup-area-prefix (area)
88   `(aref ,area 0))
89 (defmacro gnus-soup-set-area-prefix (area prefix)
90   `(aset ,area 0 ,prefix))
91 (defmacro gnus-soup-area-name (area)
92   `(aref ,area 1))
93 (defmacro gnus-soup-area-encoding (area)
94   `(aref ,area 2))
95 (defmacro gnus-soup-area-description (area)
96   `(aref ,area 3))
97 (defmacro gnus-soup-area-number (area)
98   `(aref ,area 4))
99 (defmacro gnus-soup-area-set-number (area value)
100   `(aset ,area 4 ,value))
101
102 (defmacro gnus-soup-encoding-format (encoding)
103   `(aref ,encoding 0))
104 (defmacro gnus-soup-encoding-index (encoding)
105   `(aref ,encoding 1))
106 (defmacro gnus-soup-encoding-kind (encoding)
107   `(aref ,encoding 2))
108
109 (defmacro gnus-soup-reply-prefix (reply)
110   `(aref ,reply 0))
111 (defmacro gnus-soup-reply-kind (reply)
112   `(aref ,reply 1))
113 (defmacro gnus-soup-reply-encoding (reply)
114   `(aref ,reply 2))
115
116 ;;; Commands:
117
118 (defun gnus-soup-send-replies ()
119   "Unpack and send all replies in the reply packet."
120   (interactive)
121   (let ((packets (directory-files
122                   gnus-soup-packet-directory t gnus-soup-packet-regexp)))
123     (while packets
124       (when (gnus-soup-send-packet (car packets))
125         (delete-file (car packets)))
126       (setq packets (cdr packets)))))
127
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."
134   (interactive "P")
135   (let* ((articles (gnus-summary-work-articles n))
136          (tmp-buf (get-buffer-create "*soup work*"))
137          (area (gnus-soup-area gnus-newsgroup-name))
138          (prefix (gnus-soup-area-prefix area))
139          headers)
140     (buffer-disable-undo tmp-buf)
141     (save-excursion
142       (while articles
143         ;; Find the header of the article.
144         (set-buffer gnus-summary-buffer)
145         (when (setq headers (gnus-summary-article-header (car articles)))
146           ;; Put the article in a buffer.
147           (set-buffer tmp-buf)
148           (when (gnus-request-article-this-buffer
149                  (car articles) gnus-newsgroup-name)
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
166 (defun gnus-soup-pack-packet ()
167   "Make a SOUP packet from the SOUP areas."
168   (interactive)
169   (gnus-soup-read-areas)
170   (unless (file-exists-p gnus-soup-directory)
171     (message "No such directory: %s" gnus-soup-directory))
172   (when (null (directory-files gnus-soup-directory nil "\\.MSG$"))
173     (message "No files to pack."))
174   (gnus-soup-pack gnus-soup-directory gnus-soup-packer))
175
176 (defun gnus-group-brew-soup (n)
177   "Make a soup packet from the current group.
178 Uses the process/prefix convention."
179   (interactive "P")
180   (let ((groups (gnus-group-process-prefix n)))
181     (while groups
182       (gnus-group-remove-mark (car groups))
183       (gnus-soup-group-brew (car groups) t)
184       (setq groups (cdr groups)))
185     (gnus-soup-save-areas)))
186
187 (defun gnus-brew-soup (&optional level)
188   "Go through all groups on LEVEL or less and make a soup packet."
189   (interactive "P")
190   (let ((level (or level gnus-level-subscribed))
191         (newsrc (cdr gnus-newsrc-alist)))
192     (while newsrc
193       (when (<= (nth 1 (car newsrc)) level)
194         (gnus-soup-group-brew (caar newsrc) t))
195       (setq newsrc (cdr newsrc)))
196     (gnus-soup-save-areas)))
197
198 ;;;###autoload
199 (defun gnus-batch-brew-soup ()
200   "Brew a SOUP packet from groups mention on the command line.
201 Will use the remaining command line arguments as regular expressions
202 for matching on group names.
203
204 For instance, if you want to brew on all the nnml groups, as well as
205 groups with \"emacs\" in the name, you could say something like:
206
207 $ emacs -batch -f gnus-batch-brew-soup ^nnml \".*emacs.*\""
208   (interactive)
209   nil)
210
211 ;;; Internal Functions:
212
213 ;; Store the current buffer.
214 (defun gnus-soup-store (directory prefix headers format index)
215   ;; Create the directory, if needed.
216   (gnus-make-directory directory)
217   (let* ((msg-buf (nnheader-find-file-noselect
218                    (concat directory prefix ".MSG")))
219          (idx-buf (if (= index ?n)
220                       nil
221                     (nnheader-find-file-noselect
222                      (concat directory prefix ".IDX"))))
223          (article-buf (current-buffer))
224          from head-line beg type)
225     (setq gnus-soup-buffers (cons msg-buf (delq msg-buf gnus-soup-buffers)))
226     (buffer-disable-undo msg-buf)
227     (when idx-buf
228       (push idx-buf gnus-soup-buffers)
229       (buffer-disable-undo idx-buf))
230     (save-excursion
231       ;; Make sure the last char in the buffer is a newline.
232       (goto-char (point-max))
233       (unless (= (current-column) 0)
234         (insert "\n"))
235       ;; Find the "from".
236       (goto-char (point-min))
237       (setq from
238             (gnus-mail-strip-quoted-names
239              (or (mail-fetch-field "from")
240                  (mail-fetch-field "really-from")
241                  (mail-fetch-field "sender"))))
242       (goto-char (point-min))
243       ;; Depending on what encoding is supposed to be used, we make
244       ;; a soup header.
245       (setq head-line
246             (cond
247              ((= gnus-soup-encoding-type ?n)
248               (format "#! rnews %d\n" (buffer-size)))
249              ((= gnus-soup-encoding-type ?m)
250               (while (search-forward "\nFrom " nil t)
251                 (replace-match "\n>From " t t))
252               (concat "From " (or from "unknown")
253                       " " (current-time-string) "\n"))
254              ((= gnus-soup-encoding-type ?M)
255               "\^a\^a\^a\^a\n")
256              (t (error "Unsupported type: %c" gnus-soup-encoding-type))))
257       ;; Insert the soup header and the article in the MSG buf.
258       (set-buffer msg-buf)
259       (goto-char (point-max))
260       (insert head-line)
261       (setq beg (point))
262       (insert-buffer-substring article-buf)
263       ;; Insert the index in the IDX buf.
264       (cond ((= index ?c)
265              (set-buffer idx-buf)
266              (gnus-soup-insert-idx beg headers))
267             ((/= index ?n)
268              (error "Unknown index type: %c" type)))
269       ;; Return the MSG buf.
270       msg-buf)))
271
272 (defun gnus-soup-group-brew (group &optional not-all)
273   "Enter GROUP and add all articles to a SOUP package.
274 If NOT-ALL, don't pack ticked articles."
275   (let ((gnus-expert-user t)
276         (gnus-large-newsgroup nil)
277         (entry (gnus-gethash group gnus-newsrc-hashtb)))
278     (when (or (null entry)
279               (eq (car entry) t)
280               (and (car entry)
281                    (> (car entry) 0))
282               (and (not not-all)
283                    (gnus-range-length (cdr (assq 'tick (gnus-info-marks
284                                                         (nth 2 entry)))))))
285       (when (gnus-summary-read-group group nil t)
286         (setq gnus-newsgroup-processable
287               (reverse
288                (if (not not-all)
289                    (append gnus-newsgroup-marked gnus-newsgroup-unreads)
290                  gnus-newsgroup-unreads)))
291         (gnus-soup-add-article nil)
292         (gnus-summary-exit)))))
293
294 (defun gnus-soup-insert-idx (offset header)
295   ;; [number subject from date id references chars lines xref]
296   (goto-char (point-max))
297   (insert
298    (format "%d\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t\t\n"
299            offset
300            (or (mail-header-subject header) "(none)")
301            (or (mail-header-from header) "(nobody)")
302            (or (mail-header-date header) "")
303            (or (mail-header-id header)
304                (concat "soup-dummy-id-"
305                        (mapconcat
306                         (lambda (time) (int-to-string time))
307                         (current-time) "-")))
308            (or (mail-header-references header) "")
309            (or (mail-header-chars header) 0)
310            (or (mail-header-lines header) "0"))))
311
312 (defun gnus-soup-save-areas ()
313   "Write all SOUP buffers."
314   (interactive)
315   (gnus-soup-write-areas)
316   (save-excursion
317     (let (buf)
318       (while gnus-soup-buffers
319         (setq buf (car gnus-soup-buffers)
320               gnus-soup-buffers (cdr gnus-soup-buffers))
321         (if (not (buffer-name buf))
322             ()
323           (set-buffer buf)
324           (when (buffer-modified-p)
325             (save-buffer))
326           (kill-buffer (current-buffer)))))
327     (gnus-soup-write-prefixes)))
328
329 (defun gnus-soup-write-prefixes ()
330   (let ((prefixes gnus-soup-last-prefix)
331         prefix)
332     (save-excursion
333       (gnus-set-work-buffer)
334       (while (setq prefix (pop prefixes))
335         (erase-buffer)
336         (insert (format "(setq gnus-soup-prev-prefix %d)\n" (cdr prefix)))
337         (gnus-write-buffer (concat (car prefix) gnus-soup-prefix-file))))))
338
339 (defun gnus-soup-pack (dir packer)
340   (let* ((files (mapconcat 'identity
341                            '("AREAS" "*.MSG" "*.IDX" "INFO"
342                              "LIST" "REPLIES" "COMMANDS" "ERRORS")
343                            " "))
344          (packer (if (< (string-match "%s" packer)
345                         (string-match "%d" packer))
346                      (format packer files
347                              (string-to-int (gnus-soup-unique-prefix dir)))
348                    (format packer
349                            (string-to-int (gnus-soup-unique-prefix dir))
350                            files)))
351          (dir (expand-file-name dir)))
352     (gnus-make-directory dir)
353     (setq gnus-soup-areas nil)
354     (gnus-message 4 "Packing %s..." packer)
355     (if (zerop (call-process shell-file-name
356                              nil nil nil shell-command-switch
357                              (concat "cd " dir " ; " packer)))
358         (progn
359           (call-process shell-file-name nil nil nil shell-command-switch
360                         (concat "cd " dir " ; rm " files))
361           (gnus-message 4 "Packing...done" packer))
362       (error "Couldn't pack packet"))))
363
364 (defun gnus-soup-parse-areas (file)
365   "Parse soup area file FILE.
366 The result is a of vectors, each containing one entry from the AREA file.
367 The vector contain five strings,
368   [prefix name encoding description number]
369 though the two last may be nil if they are missing."
370   (let (areas)
371     (when (file-exists-p file)
372       (save-excursion
373         (set-buffer (nnheader-find-file-noselect file 'force))
374         (buffer-disable-undo (current-buffer))
375         (goto-char (point-min))
376         (while (not (eobp))
377           (push (vector (gnus-soup-field)
378                         (gnus-soup-field)
379                         (gnus-soup-field)
380                         (and (eq (preceding-char) ?\t)
381                              (gnus-soup-field))
382                         (and (eq (preceding-char) ?\t)
383                              (string-to-int (gnus-soup-field))))
384                 areas)
385           (when (eq (preceding-char) ?\t)
386             (beginning-of-line 2)))
387         (kill-buffer (current-buffer))))
388     areas))
389
390 (defun gnus-soup-parse-replies (file)
391   "Parse soup REPLIES file FILE.
392 The result is a of vectors, each containing one entry from the REPLIES
393 file.  The vector contain three strings, [prefix name encoding]."
394   (let (replies)
395     (save-excursion
396       (set-buffer (nnheader-find-file-noselect file))
397       (buffer-disable-undo (current-buffer))
398       (goto-char (point-min))
399       (while (not (eobp))
400         (push (vector (gnus-soup-field) (gnus-soup-field)
401                       (gnus-soup-field))
402               replies)
403         (when (eq (preceding-char) ?\t)
404           (beginning-of-line 2)))
405       (kill-buffer (current-buffer)))
406     replies))
407
408 (defun gnus-soup-field ()
409   (prog1
410       (buffer-substring (point) (progn (skip-chars-forward "^\t\n") (point)))
411     (forward-char 1)))
412
413 (defun gnus-soup-read-areas ()
414   (or gnus-soup-areas
415       (setq gnus-soup-areas
416             (gnus-soup-parse-areas (concat gnus-soup-directory "AREAS")))))
417
418 (defun gnus-soup-write-areas ()
419   "Write the AREAS file."
420   (interactive)
421   (when gnus-soup-areas
422     (nnheader-temp-write (concat gnus-soup-directory "AREAS")
423       (let ((areas gnus-soup-areas)
424             area)
425         (while (setq area (pop areas))
426           (insert
427            (format
428             "%s\t%s\t%s%s\n"
429             (gnus-soup-area-prefix area)
430             (gnus-soup-area-name area)
431             (gnus-soup-area-encoding area)
432             (if (or (gnus-soup-area-description area)
433                     (gnus-soup-area-number area))
434                 (concat "\t" (or (gnus-soup-area-description
435                                   area) "")
436                         (if (gnus-soup-area-number area)
437                             (concat "\t" (int-to-string
438                                           (gnus-soup-area-number area)))
439                           "")) ""))))))))
440
441 (defun gnus-soup-write-replies (dir areas)
442   "Write a REPLIES file in DIR containing AREAS."
443   (nnheader-temp-write (concat dir "REPLIES")
444     (let (area)
445       (while (setq area (pop areas))
446         (insert (format "%s\t%s\t%s\n"
447                         (gnus-soup-reply-prefix area)
448                         (gnus-soup-reply-kind area)
449                         (gnus-soup-reply-encoding area)))))))
450
451 (defun gnus-soup-area (group)
452   (gnus-soup-read-areas)
453   (let ((areas gnus-soup-areas)
454         (real-group (gnus-group-real-name group))
455         area result)
456     (while areas
457       (setq area (car areas)
458             areas (cdr areas))
459       (when (equal (gnus-soup-area-name area) real-group)
460         (setq result area)))
461     (unless result
462       (setq result
463             (vector (gnus-soup-unique-prefix)
464                     real-group
465                     (format "%c%c%c"
466                             gnus-soup-encoding-type
467                             gnus-soup-index-type
468                             (if (gnus-member-of-valid 'mail group) ?m ?n))
469                     nil nil)
470             gnus-soup-areas (cons result gnus-soup-areas)))
471     result))
472
473 (defun gnus-soup-unique-prefix (&optional dir)
474   (let* ((dir (file-name-as-directory (or dir gnus-soup-directory)))
475          (entry (assoc dir gnus-soup-last-prefix))
476          gnus-soup-prev-prefix)
477     (if entry
478         ()
479       (when (file-exists-p (concat dir gnus-soup-prefix-file))
480         (ignore-errors
481           (load (concat dir gnus-soup-prefix-file) nil t t)))
482       (push (setq entry (cons dir (or gnus-soup-prev-prefix 0)))
483             gnus-soup-last-prefix))
484     (setcdr entry (1+ (cdr entry)))
485     (gnus-soup-write-prefixes)
486     (int-to-string (cdr entry))))
487
488 (defun gnus-soup-unpack-packet (dir unpacker packet)
489   "Unpack PACKET into DIR using UNPACKER.
490 Return whether the unpacking was successful."
491   (gnus-make-directory dir)
492   (gnus-message 4 "Unpacking: %s" (format unpacker packet))
493   (prog1
494       (zerop (call-process
495               shell-file-name nil nil nil shell-command-switch
496               (format "cd %s ; %s" (expand-file-name dir)
497                       (format unpacker packet))))
498     (gnus-message 4 "Unpacking...done")))
499
500 (defun gnus-soup-send-packet (packet)
501   (gnus-soup-unpack-packet
502    gnus-soup-replies-directory gnus-soup-unpacker packet)
503   (let ((replies (gnus-soup-parse-replies
504                   (concat gnus-soup-replies-directory "REPLIES"))))
505     (save-excursion
506       (while replies
507         (let* ((msg-file (concat gnus-soup-replies-directory
508                                  (gnus-soup-reply-prefix (car replies))
509                                  ".MSG"))
510                (msg-buf (and (file-exists-p msg-file)
511                              (nnheader-find-file-noselect msg-file)))
512                (tmp-buf (get-buffer-create " *soup send*"))
513                beg end)
514           (cond
515            ((/= (gnus-soup-encoding-format
516                  (gnus-soup-reply-encoding (car replies)))
517                 ?n)
518             (error "Unsupported encoding"))
519            ((null msg-buf)
520             t)
521            (t
522             (buffer-disable-undo msg-buf)
523             (set-buffer msg-buf)
524             (goto-char (point-min))
525             (while (not (eobp))
526               (unless (looking-at "#! *rnews +\\([0-9]+\\)")
527                 (error "Bad header"))
528               (forward-line 1)
529               (setq beg (point)
530                     end (+ (point) (string-to-int
531                                     (buffer-substring
532                                      (match-beginning 1) (match-end 1)))))
533               (switch-to-buffer tmp-buf)
534               (erase-buffer)
535               (insert-buffer-substring msg-buf beg end)
536               (goto-char (point-min))
537               (search-forward "\n\n")
538               (forward-char -1)
539               (insert mail-header-separator)
540               (setq message-newsreader (setq message-mailer
541                                              (gnus-extended-version)))
542               (cond
543                ((string= (gnus-soup-reply-kind (car replies)) "news")
544                 (gnus-message 5 "Sending news message to %s..."
545                               (mail-fetch-field "newsgroups"))
546                 (sit-for 1)
547                 (let ((message-syntax-checks
548                        'dont-check-for-anything-just-trust-me))
549                   (funcall message-send-news-function)))
550                ((string= (gnus-soup-reply-kind (car replies)) "mail")
551                 (gnus-message 5 "Sending mail to %s..."
552                               (mail-fetch-field "to"))
553                 (sit-for 1)
554                 (message-send-mail))
555                (t
556                 (error "Unknown reply kind")))
557               (set-buffer msg-buf)
558               (goto-char end))
559             (delete-file (buffer-file-name))
560             (kill-buffer msg-buf)
561             (kill-buffer tmp-buf)
562             (gnus-message 4 "Sent packet"))))
563         (setq replies (cdr replies)))
564       t)))
565
566 (provide 'gnus-soup)
567
568 ;;; gnus-soup.el ends here