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