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