*** empty log message ***
[gnus] / lisp / gnus-soup.el
1 ;;; gnus-soup.el --- SOUP packet writing support for Gnus
2 ;; Copyright (C) 1995 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         (write-region (point-min) (point-max)
308                       (concat (car (car prefix)) 
309                               gnus-soup-prefix-file) 
310                       nil 'nomesg)
311         (setq prefix (cdr prefix))))))
312
313 (defun gnus-soup-pack (dir packer)
314   (let* ((files (mapconcat 'identity
315                            '("AREAS" "*.MSG" "*.IDX" "INFO"
316                              "LIST" "REPLIES" "COMMANDS" "ERRORS")
317                            " "))
318          (packer (if (< (string-match "%s" packer)
319                         (string-match "%d" packer))
320                      (format packer files
321                              (string-to-int (gnus-soup-unique-prefix dir)))
322                    (format packer 
323                            (string-to-int (gnus-soup-unique-prefix dir))
324                            files)))
325          (dir (expand-file-name dir)))
326     (or (file-directory-p dir)
327         (gnus-make-directory dir))
328     (setq gnus-soup-areas nil)
329     (message "Packing %s..." packer)
330     (if (zerop (call-process "sh" nil nil nil "-c" 
331                              (concat "cd " dir " ; " packer)))
332         (progn
333           (call-process "sh" nil nil nil "-c" 
334                         (concat "cd " dir " ; rm " files))
335           (message "Packing...done" packer))
336       (error "Couldn't pack packet."))))
337
338 (defun gnus-soup-parse-areas (file)
339   "Parse soup area file FILE.
340 The result is a of vectors, each containing one entry from the AREA file.
341 The vector contain five strings, 
342   [prefix name encoding description number]
343 though the two last may be nil if they are missing."
344   (let (areas)
345     (save-excursion
346       (set-buffer (find-file-noselect file 'force))
347       (buffer-disable-undo (current-buffer))
348       (goto-char (point-min))
349       (while (not (eobp))
350         (setq areas
351               (cons (vector (gnus-soup-field) 
352                             (gnus-soup-field)
353                             (gnus-soup-field)
354                             (and (eq (preceding-char) ?\t)
355                                  (gnus-soup-field))
356                             (and (eq (preceding-char) ?\t)
357                                  (string-to-int (gnus-soup-field))))
358                     areas))
359         (if (eq (preceding-char) ?\t)
360             (beginning-of-line 2))))
361     areas))
362
363 (defun gnus-soup-parse-replies (file)
364   "Parse soup REPLIES file FILE.
365 The result is a of vectors, each containing one entry from the REPLIES
366 file. The vector contain three strings, [prefix name encoding]."
367   (let (replies)
368     (save-excursion
369       (set-buffer (find-file-noselect file))
370       (buffer-disable-undo (current-buffer))
371       (goto-char (point-min))
372       (while (not (eobp))
373         (setq replies
374               (cons (vector (gnus-soup-field) (gnus-soup-field)
375                             (gnus-soup-field))
376                     replies))
377         (if (eq (preceding-char) ?\t)
378             (beginning-of-line 2)))
379       (kill-buffer (current-buffer)))
380     replies))
381
382 (defun gnus-soup-field ()
383   (prog1
384       (buffer-substring (point) (progn (skip-chars-forward "^\t\n") (point)))
385     (forward-char 1)))
386
387 (defun gnus-soup-read-areas ()
388   (or gnus-soup-areas
389       (setq gnus-soup-areas
390             (gnus-soup-parse-areas (concat gnus-soup-directory "AREAS")))))
391
392 (defun gnus-soup-write-areas ()
393   "Write all areas to disk."
394   (interactive)
395   (if (not gnus-soup-areas)
396       ()
397     (save-excursion
398       (set-buffer (find-file-noselect
399                    (concat gnus-soup-directory "AREAS")))
400       (buffer-disable-undo (current-buffer))
401       (erase-buffer)
402       (let ((areas gnus-soup-areas)
403             area)
404         (while areas
405           (setq area (car areas)
406                 areas (cdr areas))
407           (insert (format "%s\t%s\t%s%s\n"
408                           (gnus-soup-area-prefix area)
409                           (gnus-soup-area-name area) 
410                           (gnus-soup-area-encoding area)
411                           (if (or (gnus-soup-area-description area) 
412                                   (gnus-soup-area-number area))
413                               (concat "\t" (or (gnus-soup-area-description
414                                                 area)
415                                                "")
416                                       (if (gnus-soup-area-number area)
417                                           (concat "\t" 
418                                                   (int-to-string
419                                                    (gnus-soup-area-number 
420                                                     area)))
421                                         "")) "")))))
422       (write-region (point-min) (point-max)
423                     (concat gnus-soup-directory "AREAS"))
424       (set-buffer-modified-p nil)
425       (kill-buffer (current-buffer)))))
426
427 (defun gnus-soup-write-replies (dir areas)
428   (or (file-directory-p dir)
429       (gnus-make-directory dir))
430   (save-excursion
431     (set-buffer (find-file-noselect (concat dir "REPLIES")))
432     (buffer-disable-undo (current-buffer))
433     (erase-buffer)
434     (let (area)
435       (while areas
436         (setq area (car areas)
437               areas (cdr areas))
438         (insert (format "%s\t%s\t%s\n"
439                         (gnus-soup-reply-prefix area)
440                         (gnus-soup-reply-kind area) 
441                         (gnus-soup-reply-encoding area)))))
442     (write-region (point-min) (point-max) (concat dir "REPLIES"))
443     (set-buffer-modified-p nil)
444     (kill-buffer (current-buffer))))
445
446 (defun gnus-soup-area (group)
447   (gnus-soup-read-areas)
448   (let ((areas gnus-soup-areas)
449         (real-group (gnus-group-real-name group))
450         area result)
451     (while areas
452       (setq area (car areas)
453             areas (cdr areas))
454       (if (equal (gnus-soup-area-name area) real-group)
455           (setq result area)))
456     (or result
457         (setq result
458               (vector (gnus-soup-unique-prefix)
459                       real-group 
460                       (format "%c%c%c"
461                               gnus-soup-encoding-type
462                               gnus-soup-index-type
463                               (if (gnus-member-of-valid 'mail group) ?m ?n))
464                       nil nil)
465               gnus-soup-areas (cons result gnus-soup-areas)))
466     result))
467
468 (defun gnus-soup-unique-prefix (&optional dir)
469   (let* ((dir (file-name-as-directory (or dir gnus-soup-directory)))
470          (entry (assoc dir gnus-soup-last-prefix))
471          gnus-soup-prev-prefix)
472     (if entry
473         ()
474       (and (file-exists-p (concat dir gnus-soup-prefix-file))
475            (condition-case nil
476                (load-file (concat dir gnus-soup-prefix-file))
477              (error nil)))
478       (setq gnus-soup-last-prefix 
479             (cons (setq entry (cons dir (or gnus-soup-prev-prefix 0)))
480                   gnus-soup-last-prefix)))
481     (setcdr entry (1+ (cdr entry)))
482     (gnus-soup-write-prefixes)
483     (int-to-string (cdr entry))))
484
485 (defun gnus-soup-unpack-packet (dir unpacker packet)
486   (gnus-make-directory dir)
487   (message "Unpacking: %s" (format unpacker packet))
488   (call-process
489    "sh" nil nil nil "-c"
490    (format "cd %s ; %s" (expand-file-name dir) (format unpacker packet)))
491   (message "Unpacking...done"))
492
493 (defun gnus-soup-send-packet (packet)
494   (gnus-soup-unpack-packet 
495    gnus-soup-replies-directory gnus-soup-unpacker packet)
496   (let ((replies (gnus-soup-parse-replies 
497                   (concat gnus-soup-replies-directory "REPLIES"))))
498     (save-excursion
499       (while replies
500         (let* ((msg-file (concat gnus-soup-replies-directory
501                                  (gnus-soup-reply-prefix (car replies))
502                                  ".MSG"))
503                (msg-buf (and (file-exists-p msg-file)
504                              (find-file-noselect msg-file)))
505                (tmp-buf (get-buffer-create " *soup send*"))
506                beg end)
507           (cond 
508            ((/= (gnus-soup-encoding-format 
509                  (gnus-soup-reply-encoding (car replies))) ?n)
510             (error "Unsupported encoding"))
511            ((null msg-buf)
512             t)
513            (t
514             (buffer-disable-undo msg-buf)
515             (buffer-disable-undo tmp-buf)
516             (set-buffer msg-buf)
517             (goto-char (point-min))
518             (while (not (eobp))
519               (or (looking-at "#! *rnews +\\([0-9]+\\)")
520                   (error "Bad header."))
521               (forward-line 1)
522               (setq beg (point)
523                     end (+ (point) (string-to-int 
524                                     (buffer-substring 
525                                      (match-beginning 1) (match-end 1)))))
526               (switch-to-buffer tmp-buf)
527               (erase-buffer)
528               (insert-buffer-substring msg-buf beg end)
529               (goto-char (point-min))
530               (search-forward "\n\n")
531               (forward-char -1)
532               (insert mail-header-separator)
533               (cond 
534                ((string= (gnus-soup-reply-kind (car replies)) "news")
535                 (message "Sending news message to %s..."
536                          (mail-fetch-field "newsgroups"))
537                 (sit-for 1)
538                 (gnus-inews-article))
539                ((string= (gnus-soup-reply-kind (car replies)) "mail")
540                 (message "Sending mail to %s..."
541                          (mail-fetch-field "to"))
542                 (sit-for 1)
543                 (gnus-mail-send-and-exit))
544                (t
545                 (error "Unknown reply kind")))
546               (set-buffer msg-buf)
547               (goto-char end))
548             (delete-file (buffer-file-name))
549             (kill-buffer msg-buf)
550             (kill-buffer tmp-buf)
551             (message "Sent packet"))))
552         (setq replies (cdr replies)))
553       t)))
554                    
555 (provide 'gnus-soup)
556
557 ;;; gnus-soup.el ends here