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