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