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