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