*** 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   (or (file-directory-p directory)
212       (make-directory directory) t)
213   (let* ((msg-buf (find-file-noselect
214                    (concat directory prefix ".MSG")))
215          (idx-buf (if (= index ?n)
216                       nil
217                     (find-file-noselect
218                      (concat directory prefix ".IDX"))))
219          (article-buf (current-buffer))
220          from head-line beg type)
221     (setq gnus-soup-buffers (cons msg-buf (delq msg-buf gnus-soup-buffers)))
222     (buffer-disable-undo msg-buf)
223     (and idx-buf 
224          (progn
225            (setq gnus-soup-buffers (cons idx-buf gnus-soup-buffers))
226            (buffer-disable-undo idx-buf)))
227     (save-excursion
228       ;; Make sure the last char in the buffer is a newline.
229       (goto-char (point-max))
230       (or (= (current-column) 0)
231           (insert "\n"))
232       ;; Find the "from".
233       (goto-char (point-min))
234       (setq from
235             (gnus-mail-strip-quoted-names
236              (or (mail-fetch-field "from")
237                  (mail-fetch-field "really-from")
238                  (mail-fetch-field "sender"))))
239       (goto-char (point-min))
240       ;; Depending on what encoding is supposed to be used, we make
241       ;; a soup header. 
242       (setq head-line
243             (cond 
244              ((= gnus-soup-encoding-type ?n)
245               (format "#! rnews %d\n" (buffer-size)))
246              ((= gnus-soup-encoding-type ?m)
247               (while (search-forward "\nFrom " nil t)
248                 (replace-match "\n>From " t t))
249               (concat "From " (or from "unknown")
250                       " " (current-time-string) "\n"))
251              ((= gnus-soup-encoding-type ?M)
252               "\^a\^a\^a\^a\n")
253              (t (error "Unsupported type: %c" gnus-soup-encoding-type))))
254       ;; Insert the soup header and the article in the MSG buf.
255       (set-buffer msg-buf)
256       (goto-char (point-max))
257       (insert head-line)
258       (setq beg (point))
259       (insert-buffer-substring article-buf)
260       ;; Insert the index in the IDX buf.
261       (cond ((= index ?c)
262              (set-buffer idx-buf)
263              (gnus-soup-insert-idx beg headers))
264             ((/= index ?n)
265              (error "Unknown index type: %c" type)))
266       ;; Return the MSG buf.
267       msg-buf)))
268
269 (defun gnus-soup-group-brew (group &optional not-all)
270   "Enter GROUP and add all articles to a SOUP package.
271 If NOT-ALL, don't pack ticked articles."
272   (let ((gnus-expert-user t)
273         (gnus-large-newsgroup nil)
274         (entry (gnus-gethash group gnus-newsrc-hashtb)))
275     (when (or (null entry)
276               (eq (car entry) t)
277               (and (car entry)
278                    (> (car entry) 0))
279               (and (not not-all)
280                    (gnus-range-length (cdr (assq 'tick (gnus-info-marks 
281                                                         (nth 2 entry)))))))
282       (when (gnus-summary-read-group group nil t)
283         (setq gnus-newsgroup-processable
284               (reverse
285                (if (not not-all)
286                    (append gnus-newsgroup-marked gnus-newsgroup-unreads)
287                  gnus-newsgroup-unreads)))
288         (gnus-soup-add-article nil)
289         (gnus-summary-exit)))))
290
291 (defun gnus-soup-insert-idx (offset header)
292   ;; [number subject from date id references chars lines xref]
293   (goto-char (point-max))
294   (insert
295    (format "%d\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t\t\n"
296            offset
297            (or (mail-header-subject header) "(none)")
298            (or (mail-header-from header) "(nobody)")
299            (or (mail-header-date header) "")
300            (or (mail-header-id header)
301                (concat "soup-dummy-id-" 
302                        (mapconcat 
303                         (lambda (time) (int-to-string time))
304                         (current-time) "-")))
305            (or (mail-header-references header) "")
306            (or (mail-header-chars header) 0) 
307            (or (mail-header-lines header) "0"))))
308
309 (defun gnus-soup-save-areas ()
310   (gnus-soup-write-areas)
311   (save-excursion
312     (let (buf)
313       (while gnus-soup-buffers
314         (setq buf (car gnus-soup-buffers)
315               gnus-soup-buffers (cdr gnus-soup-buffers))
316         (if (not (buffer-name buf))
317             ()
318           (set-buffer buf)
319           (and (buffer-modified-p) (save-buffer))
320           (kill-buffer (current-buffer)))))
321     (gnus-soup-write-prefixes)))
322
323 (defun gnus-soup-write-prefixes ()
324   (let ((prefix gnus-soup-last-prefix))
325     (save-excursion
326       (while prefix
327         (gnus-set-work-buffer)
328         (insert (format "(setq gnus-soup-prev-prefix %d)\n" (cdar prefix)))
329         (make-directory (caar prefix) t)
330         (write-region (point-min) (point-max)
331                       (concat (caar prefix) gnus-soup-prefix-file) 
332                       nil 'nomesg)
333         (setq prefix (cdr prefix))))))
334
335 (defun gnus-soup-pack (dir packer)
336   (let* ((files (mapconcat 'identity
337                            '("AREAS" "*.MSG" "*.IDX" "INFO"
338                              "LIST" "REPLIES" "COMMANDS" "ERRORS")
339                            " "))
340          (packer (if (< (string-match "%s" packer)
341                         (string-match "%d" packer))
342                      (format packer files
343                              (string-to-int (gnus-soup-unique-prefix dir)))
344                    (format packer 
345                            (string-to-int (gnus-soup-unique-prefix dir))
346                            files)))
347          (dir (expand-file-name dir)))
348     (or (file-directory-p dir)
349         (make-directory dir t))
350     (setq gnus-soup-areas nil)
351     (gnus-message 4 "Packing %s..." packer)
352     (if (zerop (call-process shell-file-name
353                              nil nil nil shell-command-switch 
354                              (concat "cd " dir " ; " packer)))
355         (progn
356           (call-process shell-file-name nil nil nil shell-command-switch 
357                         (concat "cd " dir " ; rm " files))
358           (gnus-message 4 "Packing...done" packer))
359       (error "Couldn't pack packet."))))
360
361 (defun gnus-soup-parse-areas (file)
362   "Parse soup area file FILE.
363 The result is a of vectors, each containing one entry from the AREA file.
364 The vector contain five strings, 
365   [prefix name encoding description number]
366 though the two last may be nil if they are missing."
367   (let (areas)
368     (save-excursion
369       (set-buffer (find-file-noselect file 'force))
370       (buffer-disable-undo (current-buffer))
371       (goto-char (point-min))
372       (while (not (eobp))
373         (setq areas
374               (cons (vector (gnus-soup-field) 
375                             (gnus-soup-field)
376                             (gnus-soup-field)
377                             (and (eq (preceding-char) ?\t)
378                                  (gnus-soup-field))
379                             (and (eq (preceding-char) ?\t)
380                                  (string-to-int (gnus-soup-field))))
381                     areas))
382         (if (eq (preceding-char) ?\t)
383             (beginning-of-line 2)))
384       (kill-buffer (current-buffer)))
385     areas))
386
387 (defun gnus-soup-parse-replies (file)
388   "Parse soup REPLIES file FILE.
389 The result is a of vectors, each containing one entry from the REPLIES
390 file. The vector contain three strings, [prefix name encoding]."
391   (let (replies)
392     (save-excursion
393       (set-buffer (find-file-noselect file))
394       (buffer-disable-undo (current-buffer))
395       (goto-char (point-min))
396       (while (not (eobp))
397         (setq replies
398               (cons (vector (gnus-soup-field) (gnus-soup-field)
399                             (gnus-soup-field))
400                     replies))
401         (if (eq (preceding-char) ?\t)
402             (beginning-of-line 2)))
403       (kill-buffer (current-buffer)))
404     replies))
405
406 (defun gnus-soup-field ()
407   (prog1
408       (buffer-substring (point) (progn (skip-chars-forward "^\t\n") (point)))
409     (forward-char 1)))
410
411 (defun gnus-soup-read-areas ()
412   (or gnus-soup-areas
413       (setq gnus-soup-areas
414             (gnus-soup-parse-areas (concat gnus-soup-directory "AREAS")))))
415
416 (defun gnus-soup-write-areas ()
417   "Write the AREAS file."
418   (interactive)
419   (when gnus-soup-areas
420     (nnheader-temp-write (concat gnus-soup-directory "AREAS")
421       (let ((areas gnus-soup-areas)
422             area)
423         (while (setq area (pop areas))
424           (insert
425            (format 
426             "%s\t%s\t%s%s\n"
427             (gnus-soup-area-prefix area)
428             (gnus-soup-area-name area) 
429             (gnus-soup-area-encoding area)
430             (if (or (gnus-soup-area-description area) 
431                     (gnus-soup-area-number area))
432                 (concat "\t" (or (gnus-soup-area-description
433                                   area) "")
434                         (if (gnus-soup-area-number area)
435                             (concat "\t" (int-to-string 
436                                           (gnus-soup-area-number area)))
437                           "")) ""))))))))
438
439 (defun gnus-soup-write-replies (dir areas)
440   "Write a REPLIES file in DIR containing AREAS."
441   (nnheader-temp-write (concat dir "REPLIES")
442     (let (area)
443       (while (setq area (pop areas))
444         (insert (format "%s\t%s\t%s\n"
445                         (gnus-soup-reply-prefix area)
446                         (gnus-soup-reply-kind area) 
447                         (gnus-soup-reply-encoding area)))))))
448
449 (defun gnus-soup-area (group)
450   (gnus-soup-read-areas)
451   (let ((areas gnus-soup-areas)
452         (real-group (gnus-group-real-name group))
453         area result)
454     (while areas
455       (setq area (car areas)
456             areas (cdr areas))
457       (if (equal (gnus-soup-area-name area) real-group)
458           (setq result area)))
459     (or result
460         (setq result
461               (vector (gnus-soup-unique-prefix)
462                       real-group 
463                       (format "%c%c%c"
464                               gnus-soup-encoding-type
465                               gnus-soup-index-type
466                               (if (gnus-member-of-valid 'mail group) ?m ?n))
467                       nil nil)
468               gnus-soup-areas (cons result gnus-soup-areas)))
469     result))
470
471 (defun gnus-soup-unique-prefix (&optional dir)
472   (let* ((dir (file-name-as-directory (or dir gnus-soup-directory)))
473          (entry (assoc dir gnus-soup-last-prefix))
474          gnus-soup-prev-prefix)
475     (if entry
476         ()
477       (and (file-exists-p (concat dir gnus-soup-prefix-file))
478            (condition-case nil
479                (load (concat dir gnus-soup-prefix-file) nil t t)
480              (error nil)))
481       (setq gnus-soup-last-prefix 
482             (cons (setq entry (cons dir (or gnus-soup-prev-prefix 0)))
483                   gnus-soup-last-prefix)))
484     (setcdr entry (1+ (cdr entry)))
485     (gnus-soup-write-prefixes)
486     (int-to-string (cdr entry))))
487
488 (defun gnus-soup-unpack-packet (dir unpacker packet)
489   "Unpack PACKET into DIR using UNPACKER.
490 Return whether the unpacking was successful."
491   (make-directory dir t)
492   (gnus-message 4 "Unpacking: %s" (format unpacker packet))
493   (prog1
494       (zerop (call-process
495               shell-file-name nil nil nil shell-command-switch
496               (format "cd %s ; %s" (expand-file-name dir) 
497                       (format unpacker packet))))
498     (gnus-message 4 "Unpacking...done")))
499
500 (defun gnus-soup-send-packet (packet)
501   (gnus-soup-unpack-packet 
502    gnus-soup-replies-directory gnus-soup-unpacker packet)
503   (let ((replies (gnus-soup-parse-replies 
504                   (concat gnus-soup-replies-directory "REPLIES"))))
505     (save-excursion
506       (while replies
507         (let* ((msg-file (concat gnus-soup-replies-directory
508                                  (gnus-soup-reply-prefix (car replies))
509                                  ".MSG"))
510                (msg-buf (and (file-exists-p msg-file)
511                              (find-file-noselect msg-file)))
512                (tmp-buf (get-buffer-create " *soup send*"))
513                beg end)
514           (cond 
515            ((/= (gnus-soup-encoding-format 
516                  (gnus-soup-reply-encoding (car replies))) ?n)
517             (error "Unsupported encoding"))
518            ((null msg-buf)
519             t)
520            (t
521             (buffer-disable-undo msg-buf)
522             (buffer-disable-undo tmp-buf)
523             (set-buffer msg-buf)
524             (goto-char (point-min))
525             (while (not (eobp))
526               (or (looking-at "#! *rnews +\\([0-9]+\\)")
527                   (error "Bad header."))
528               (forward-line 1)
529               (setq beg (point)
530                     end (+ (point) (string-to-int 
531                                     (buffer-substring 
532                                      (match-beginning 1) (match-end 1)))))
533               (switch-to-buffer tmp-buf)
534               (erase-buffer)
535               (insert-buffer-substring msg-buf beg end)
536               (goto-char (point-min))
537               (search-forward "\n\n")
538               (forward-char -1)
539               (insert mail-header-separator)
540               (setq message-newsreader (setq message-mailer
541                                              (gnus-extended-version)))
542               (cond 
543                ((string= (gnus-soup-reply-kind (car replies)) "news")
544                 (gnus-message 5 "Sending news message to %s..."
545                               (mail-fetch-field "newsgroups"))
546                 (sit-for 1)
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