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