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