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