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