Indent.
[gnus] / lisp / gnus-soup.el
index 25257cf..b444032 100644 (file)
@@ -1,8 +1,10 @@
 ;;; gnus-soup.el --- SOUP packet writing support for Gnus
-;; Copyright (C) 1995,96 Free Software Foundation, Inc.
+
+;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2002
+;;     Free Software Foundation, Inc.
 
 ;; Author: Per Abrahamsen <abraham@iesd.auc.dk>
-;;     Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
+;;     Lars Magne Ingebrigtsen <larsi@gnus.org>
 ;; Keywords: news, mail
 
 ;; This file is part of GNU Emacs.
 
 ;;; Code:
 
-(require 'gnus-msg)
-(require 'gnus)
 (eval-when-compile (require 'cl))
 
+(require 'gnus)
+(require 'gnus-art)
+(require 'message)
+(require 'gnus-start)
+(require 'gnus-range)
+
 ;;; User Variables:
 
-(defvar gnus-soup-directory "~/SoupBrew/"
+(defvar gnus-soup-directory (nnheader-concat gnus-home-directory "SoupBrew/")
   "*Directory containing an unpacked SOUP packet.")
 
-(defvar gnus-soup-replies-directory (concat gnus-soup-directory "SoupReplies/")
+(defvar gnus-soup-replies-directory
+  (nnheader-concat gnus-soup-directory "SoupReplies/")
   "*Directory where Gnus will do processing of replies.")
 
 (defvar gnus-soup-prefix-file "gnus-prefix"
 (defvar gnus-soup-packer "tar cf - %s | gzip > $HOME/Soupout%d.tgz"
   "Format string command for packing a SOUP packet.
 The SOUP files will be inserted where the %s is in the string.
-This string MUST contain both %s and %d. The file number will be
+This string MUST contain both %s and %d.  The file number will be
 inserted where %d appears.")
 
 (defvar gnus-soup-unpacker "gunzip -c %s | tar xvf -"
   "*Format string command for unpacking a SOUP packet.
 The SOUP packet file name will be inserted at the %s.")
 
-(defvar gnus-soup-packet-directory "~/"
+(defvar gnus-soup-packet-directory gnus-home-directory
   "*Where gnus-soup will look for REPLIES packets.")
 
 (defvar gnus-soup-packet-regexp "Soupin"
@@ -62,15 +69,15 @@ The SOUP packet file name will be inserted at the %s.")
 
 ;;; Internal Variables:
 
-(defvar gnus-soup-encoding-type ?n
+(defvar gnus-soup-encoding-type ?u
   "*Soup encoding type.
-`n' is news format, `m' is Unix mbox format, and `M' is MMDF mailbox
+`u' is USENET news format, `m' is Unix mbox format, and `M' is MMDF mailbox
 format.")
 
 (defvar gnus-soup-index-type ?c
   "*Soup index type.
 `n' means no index file and `c' means standard Cnews overview
-format.") 
+format.")
 
 (defvar gnus-soup-areas nil)
 (defvar gnus-soup-last-prefix nil)
@@ -116,8 +123,8 @@ format.")
   (let ((packets (directory-files
                  gnus-soup-packet-directory t gnus-soup-packet-regexp)))
     (while packets
-      (and (gnus-soup-send-packet (car packets))
-          (delete-file (car packets)))
+      (when (gnus-soup-send-packet (car packets))
+       (delete-file (car packets)))
       (setq packets (cdr packets)))))
 
 (defun gnus-soup-add-article (n)
@@ -127,43 +134,45 @@ If N is a negative number, add the N previous articles.
 If N is nil and any articles have been marked with the process mark,
 move those articles instead."
   (interactive "P")
-  (gnus-set-global-variables)
   (let* ((articles (gnus-summary-work-articles n))
-        (tmp-buf (get-buffer-create "*soup work*"))
+        (tmp-buf (gnus-get-buffer-create "*soup work*"))
         (area (gnus-soup-area gnus-newsgroup-name))
         (prefix (gnus-soup-area-prefix area))
         headers)
     (buffer-disable-undo tmp-buf)
     (save-excursion
       (while articles
-       ;; Find the header of the article.
-       (set-buffer gnus-summary-buffer)
-       (when (setq headers (gnus-summary-article-header (car articles)))
-         ;; Put the article in a buffer.
-         (set-buffer tmp-buf)
-         (when (gnus-request-article-this-buffer 
-                (car articles) gnus-newsgroup-name)
-           (save-restriction
-             (nnheader-narrow-to-headers)
-             (nnheader-remove-header gnus-soup-ignored-headers t))
-           (gnus-soup-store gnus-soup-directory prefix headers
-                            gnus-soup-encoding-type 
-                            gnus-soup-index-type)
-           (gnus-soup-area-set-number 
-            area (1+ (or (gnus-soup-area-number area) 0)))))
-       ;; Mark article as read. 
-       (set-buffer gnus-summary-buffer)
+       ;; Put the article in a buffer.
+       (set-buffer tmp-buf)
+       (when (gnus-request-article-this-buffer
+              (car articles) gnus-newsgroup-name)
+         (setq headers (nnheader-parse-head t))
+         (save-restriction
+           (message-narrow-to-head)
+           (message-remove-header gnus-soup-ignored-headers t))
+         (gnus-soup-store gnus-soup-directory prefix headers
+                          gnus-soup-encoding-type
+                          gnus-soup-index-type)
+         (gnus-soup-area-set-number
+          area (1+ (or (gnus-soup-area-number area) 0)))
+         ;; Mark article as read.
+         (set-buffer gnus-summary-buffer)
+         (gnus-summary-mark-as-read (car articles) gnus-souped-mark))
        (gnus-summary-remove-process-mark (car articles))
-       (gnus-summary-mark-as-read (car articles) gnus-souped-mark)
        (setq articles (cdr articles)))
       (kill-buffer tmp-buf))
-    (gnus-soup-save-areas)))
+    (gnus-soup-save-areas)
+    (gnus-set-mode-line 'summary)))
 
 (defun gnus-soup-pack-packet ()
   "Make a SOUP packet from the SOUP areas."
   (interactive)
   (gnus-soup-read-areas)
-  (gnus-soup-pack gnus-soup-directory gnus-soup-packer))
+  (if (file-exists-p gnus-soup-directory)
+      (if (directory-files gnus-soup-directory nil "\\.MSG$")
+         (gnus-soup-pack gnus-soup-directory gnus-soup-packer)
+       (message "No files to pack."))
+    (message "No such directory: %s" gnus-soup-directory)))
 
 (defun gnus-group-brew-soup (n)
   "Make a soup packet from the current group.
@@ -182,8 +191,8 @@ Uses the process/prefix convention."
   (let ((level (or level gnus-level-subscribed))
        (newsrc (cdr gnus-newsrc-alist)))
     (while newsrc
-      (and (<= (nth 1 (car newsrc)) level)
-          (gnus-soup-group-brew (caar newsrc) t))
+      (when (<= (nth 1 (car newsrc)) level)
+       (gnus-soup-group-brew (caar newsrc) t))
       (setq newsrc (cdr newsrc)))
     (gnus-soup-save-areas)))
 
@@ -196,36 +205,36 @@ for matching on group names.
 For instance, if you want to brew on all the nnml groups, as well as
 groups with \"emacs\" in the name, you could say something like:
 
-$ emacs -batch -f gnus-batch-brew-soup ^nnml \".*emacs.*\""
+$ emacs -batch -f gnus-batch-brew-soup ^nnml \".*emacs.*\"
+
+Note -- this function hasn't been implemented yet."
   (interactive)
-  )
-  
+  nil)
+
 ;;; Internal Functions:
 
-;; Store the current buffer. 
+;; Store the current buffer.
 (defun gnus-soup-store (directory prefix headers format index)
-  ;; Create the directory, if needed. 
-  (or (file-directory-p directory)
-      (gnus-make-directory directory))
-  (let* ((msg-buf (find-file-noselect
+  ;; Create the directory, if needed.
+  (gnus-make-directory directory)
+  (let* ((msg-buf (nnheader-find-file-noselect
                   (concat directory prefix ".MSG")))
         (idx-buf (if (= index ?n)
                      nil
-                   (find-file-noselect
+                   (nnheader-find-file-noselect
                     (concat directory prefix ".IDX"))))
         (article-buf (current-buffer))
         from head-line beg type)
     (setq gnus-soup-buffers (cons msg-buf (delq msg-buf gnus-soup-buffers)))
     (buffer-disable-undo msg-buf)
-    (and idx-buf 
-        (progn
-          (setq gnus-soup-buffers (cons idx-buf gnus-soup-buffers))
-          (buffer-disable-undo idx-buf)))
+    (when idx-buf
+      (push idx-buf gnus-soup-buffers)
+      (buffer-disable-undo idx-buf))
     (save-excursion
       ;; Make sure the last char in the buffer is a newline.
       (goto-char (point-max))
-      (or (= (current-column) 0)
-         (insert "\n"))
+      (unless (= (current-column) 0)
+       (insert "\n"))
       ;; Find the "from".
       (goto-char (point-min))
       (setq from
@@ -235,10 +244,11 @@ $ emacs -batch -f gnus-batch-brew-soup ^nnml \".*emacs.*\""
                 (mail-fetch-field "sender"))))
       (goto-char (point-min))
       ;; Depending on what encoding is supposed to be used, we make
-      ;; a soup header. 
+      ;; a soup header.
       (setq head-line
-           (cond 
-            ((= gnus-soup-encoding-type ?n)
+           (cond
+            ((or (= gnus-soup-encoding-type ?u)
+                 (= gnus-soup-encoding-type ?n)) ;;Gnus back compatibility.
              (format "#! rnews %d\n" (buffer-size)))
             ((= gnus-soup-encoding-type ?m)
              (while (search-forward "\nFrom " nil t)
@@ -274,7 +284,7 @@ If NOT-ALL, don't pack ticked articles."
              (and (car entry)
                   (> (car entry) 0))
              (and (not not-all)
-                  (gnus-range-length (cdr (assq 'tick (gnus-info-marks 
+                  (gnus-range-length (cdr (assq 'tick (gnus-info-marks
                                                        (nth 2 entry)))))))
       (when (gnus-summary-read-group group nil t)
        (setq gnus-newsgroup-processable
@@ -295,15 +305,17 @@ If NOT-ALL, don't pack ticked articles."
           (or (mail-header-from header) "(nobody)")
           (or (mail-header-date header) "")
           (or (mail-header-id header)
-              (concat "soup-dummy-id-" 
-                      (mapconcat 
+              (concat "soup-dummy-id-"
+                      (mapconcat
                        (lambda (time) (int-to-string time))
                        (current-time) "-")))
           (or (mail-header-references header) "")
-          (or (mail-header-chars header) 0) 
+          (or (mail-header-chars header) 0)
           (or (mail-header-lines header) "0"))))
 
 (defun gnus-soup-save-areas ()
+  "Write all SOUP buffers."
+  (interactive)
   (gnus-soup-write-areas)
   (save-excursion
     (let (buf)
@@ -313,21 +325,21 @@ If NOT-ALL, don't pack ticked articles."
        (if (not (buffer-name buf))
            ()
          (set-buffer buf)
-         (and (buffer-modified-p) (save-buffer))
+         (when (buffer-modified-p)
+           (save-buffer))
          (kill-buffer (current-buffer)))))
     (gnus-soup-write-prefixes)))
 
 (defun gnus-soup-write-prefixes ()
-  (let ((prefix gnus-soup-last-prefix))
+  (let ((prefixes gnus-soup-last-prefix)
+       prefix)
     (save-excursion
-      (while prefix
-       (gnus-set-work-buffer)
-       (insert (format "(setq gnus-soup-prev-prefix %d)\n" (cdar prefix)))
-       (gnus-make-directory (caar prefix))
-       (write-region (point-min) (point-max)
-                     (concat (caar prefix) gnus-soup-prefix-file) 
-                     nil 'nomesg)
-       (setq prefix (cdr prefix))))))
+      (gnus-set-work-buffer)
+      (while (setq prefix (pop prefixes))
+       (erase-buffer)
+       (insert (format "(setq gnus-soup-prev-prefix %d)\n" (cdr prefix)))
+       (let ((coding-system-for-write mm-text-coding-system))
+         (gnus-write-buffer (concat (car prefix) gnus-soup-prefix-file)))))))
 
 (defun gnus-soup-pack (dir packer)
   (let* ((files (mapconcat 'identity
@@ -338,64 +350,63 @@ If NOT-ALL, don't pack ticked articles."
                        (string-match "%d" packer))
                     (format packer files
                             (string-to-int (gnus-soup-unique-prefix dir)))
-                  (format packer 
+                  (format packer
                           (string-to-int (gnus-soup-unique-prefix dir))
                           files)))
         (dir (expand-file-name dir)))
-    (or (file-directory-p dir)
-       (gnus-make-directory dir))
+    (gnus-make-directory dir)
     (setq gnus-soup-areas nil)
-    (message "Packing %s..." packer)
-    (if (zerop (call-process "sh" nil nil nil "-c" 
+    (gnus-message 4 "Packing %s..." packer)
+    (if (zerop (call-process shell-file-name
+                            nil nil nil shell-command-switch
                             (concat "cd " dir " ; " packer)))
        (progn
-         (call-process "sh" nil nil nil "-c" 
+         (call-process shell-file-name nil nil nil shell-command-switch
                        (concat "cd " dir " ; rm " files))
-         (message "Packing...done" packer))
-      (error "Couldn't pack packet."))))
+         (gnus-message 4 "Packing...done" packer))
+      (error "Couldn't pack packet"))))
 
 (defun gnus-soup-parse-areas (file)
   "Parse soup area file FILE.
 The result is a of vectors, each containing one entry from the AREA file.
-The vector contain five strings, 
+The vector contain five strings,
   [prefix name encoding description number]
 though the two last may be nil if they are missing."
   (let (areas)
-    (save-excursion
-      (set-buffer (find-file-noselect file 'force))
-      (buffer-disable-undo (current-buffer))
-      (goto-char (point-min))
-      (while (not (eobp))
-       (setq areas
-             (cons (vector (gnus-soup-field) 
-                           (gnus-soup-field)
-                           (gnus-soup-field)
-                           (and (eq (preceding-char) ?\t)
-                                (gnus-soup-field))
-                           (and (eq (preceding-char) ?\t)
-                                (string-to-int (gnus-soup-field))))
-                   areas))
-       (if (eq (preceding-char) ?\t)
+    (when (file-exists-p file)
+      (save-excursion
+       (set-buffer (nnheader-find-file-noselect file 'force))
+       (buffer-disable-undo)
+       (goto-char (point-min))
+       (while (not (eobp))
+         (push (vector (gnus-soup-field)
+                       (gnus-soup-field)
+                       (gnus-soup-field)
+                       (and (eq (preceding-char) ?\t)
+                            (gnus-soup-field))
+                       (and (eq (preceding-char) ?\t)
+                            (string-to-int (gnus-soup-field))))
+               areas)
+         (when (eq (preceding-char) ?\t)
            (beginning-of-line 2)))
-      (kill-buffer (current-buffer)))
+       (kill-buffer (current-buffer))))
     areas))
 
 (defun gnus-soup-parse-replies (file)
   "Parse soup REPLIES file FILE.
 The result is a of vectors, each containing one entry from the REPLIES
-file. The vector contain three strings, [prefix name encoding]."
+file.  The vector contain three strings, [prefix name encoding]."
   (let (replies)
     (save-excursion
-      (set-buffer (find-file-noselect file))
-      (buffer-disable-undo (current-buffer))
+      (set-buffer (nnheader-find-file-noselect file))
+      (buffer-disable-undo)
       (goto-char (point-min))
       (while (not (eobp))
-       (setq replies
-             (cons (vector (gnus-soup-field) (gnus-soup-field)
-                           (gnus-soup-field))
-                   replies))
-       (if (eq (preceding-char) ?\t)
-           (beginning-of-line 2)))
+       (push (vector (gnus-soup-field) (gnus-soup-field)
+                     (gnus-soup-field))
+             replies)
+       (when (eq (preceding-char) ?\t)
+         (beginning-of-line 2)))
       (kill-buffer (current-buffer)))
     replies))
 
@@ -413,33 +424,33 @@ file. The vector contain three strings, [prefix name encoding]."
   "Write the AREAS file."
   (interactive)
   (when gnus-soup-areas
-    (nnheader-temp-write (concat gnus-soup-directory "AREAS")
+    (with-temp-file (concat gnus-soup-directory "AREAS")
       (let ((areas gnus-soup-areas)
            area)
        (while (setq area (pop areas))
          (insert
-          (format 
+          (format
            "%s\t%s\t%s%s\n"
            (gnus-soup-area-prefix area)
-           (gnus-soup-area-name area) 
+           (gnus-soup-area-name area)
            (gnus-soup-area-encoding area)
-           (if (or (gnus-soup-area-description area) 
+           (if (or (gnus-soup-area-description area)
                    (gnus-soup-area-number area))
                (concat "\t" (or (gnus-soup-area-description
                                  area) "")
                        (if (gnus-soup-area-number area)
-                           (concat "\t" (int-to-string 
+                           (concat "\t" (int-to-string
                                          (gnus-soup-area-number area)))
                          "")) ""))))))))
 
 (defun gnus-soup-write-replies (dir areas)
   "Write a REPLIES file in DIR containing AREAS."
-  (nnheader-temp-write (concat dir "REPLIES")
+  (with-temp-file (concat dir "REPLIES")
     (let (area)
       (while (setq area (pop areas))
        (insert (format "%s\t%s\t%s\n"
                        (gnus-soup-reply-prefix area)
-                       (gnus-soup-reply-kind area) 
+                       (gnus-soup-reply-kind area)
                        (gnus-soup-reply-encoding area)))))))
 
 (defun gnus-soup-area (group)
@@ -450,18 +461,18 @@ file. The vector contain three strings, [prefix name encoding]."
     (while areas
       (setq area (car areas)
            areas (cdr areas))
-      (if (equal (gnus-soup-area-name area) real-group)
-         (setq result area)))
-    (or result
-       (setq result
-             (vector (gnus-soup-unique-prefix)
-                     real-group 
-                     (format "%c%c%c"
-                             gnus-soup-encoding-type
-                             gnus-soup-index-type
-                             (if (gnus-member-of-valid 'mail group) ?m ?n))
-                     nil nil)
-             gnus-soup-areas (cons result gnus-soup-areas)))
+      (when (equal (gnus-soup-area-name area) real-group)
+       (setq result area)))
+    (unless result
+      (setq result
+           (vector (gnus-soup-unique-prefix)
+                   real-group
+                   (format "%c%c%c"
+                           gnus-soup-encoding-type
+                           gnus-soup-index-type
+                           (if (gnus-member-of-valid 'mail group) ?m ?n))
+                   nil nil)
+           gnus-soup-areas (cons result gnus-soup-areas)))
     result))
 
 (defun gnus-soup-unique-prefix (&optional dir)
@@ -470,13 +481,11 @@ file. The vector contain three strings, [prefix name encoding]."
         gnus-soup-prev-prefix)
     (if entry
        ()
-      (and (file-exists-p (concat dir gnus-soup-prefix-file))
-          (condition-case nil
-              (load (concat dir gnus-soup-prefix-file) nil t t)
-            (error nil)))
-      (setq gnus-soup-last-prefix 
-           (cons (setq entry (cons dir (or gnus-soup-prev-prefix 0)))
-                 gnus-soup-last-prefix)))
+      (when (file-exists-p (concat dir gnus-soup-prefix-file))
+       (ignore-errors
+         (load (concat dir gnus-soup-prefix-file) nil t t)))
+      (push (setq entry (cons dir (or gnus-soup-prev-prefix 0)))
+           gnus-soup-last-prefix))
     (setcdr entry (1+ (cdr entry)))
     (gnus-soup-write-prefixes)
     (int-to-string (cdr entry))))
@@ -485,18 +494,18 @@ file. The vector contain three strings, [prefix name encoding]."
   "Unpack PACKET into DIR using UNPACKER.
 Return whether the unpacking was successful."
   (gnus-make-directory dir)
-  (message "Unpacking: %s" (format unpacker packet))
+  (gnus-message 4 "Unpacking: %s" (format unpacker packet))
   (prog1
       (zerop (call-process
-             "sh" nil nil nil "-c"
-             (format "cd %s ; %s" (expand-file-name dir) 
+             shell-file-name nil nil nil shell-command-switch
+             (format "cd %s ; %s" (expand-file-name dir)
                      (format unpacker packet))))
-    (message "Unpacking...done")))
+    (gnus-message 4 "Unpacking...done")))
 
 (defun gnus-soup-send-packet (packet)
-  (gnus-soup-unpack-packet 
+  (gnus-soup-unpack-packet
    gnus-soup-replies-directory gnus-soup-unpacker packet)
-  (let ((replies (gnus-soup-parse-replies 
+  (let ((replies (gnus-soup-parse-replies
                  (concat gnus-soup-replies-directory "REPLIES"))))
     (save-excursion
       (while replies
@@ -504,46 +513,62 @@ Return whether the unpacking was successful."
                                 (gnus-soup-reply-prefix (car replies))
                                 ".MSG"))
               (msg-buf (and (file-exists-p msg-file)
-                            (find-file-noselect msg-file)))
-              (tmp-buf (get-buffer-create " *soup send*"))
+                            (nnheader-find-file-noselect msg-file)))
+              (tmp-buf (gnus-get-buffer-create " *soup send*"))
               beg end)
-         (cond 
-          ((/= (gnus-soup-encoding-format 
-                (gnus-soup-reply-encoding (car replies))) ?n)
+         (cond
+          ((and (/= (gnus-soup-encoding-format
+                     (gnus-soup-reply-encoding (car replies)))
+                    ?u)
+                (/= (gnus-soup-encoding-format
+                     (gnus-soup-reply-encoding (car replies)))
+                    ?n)) ;; Gnus back compatibility.
            (error "Unsupported encoding"))
           ((null msg-buf)
            t)
           (t
            (buffer-disable-undo msg-buf)
-           (buffer-disable-undo tmp-buf)
            (set-buffer msg-buf)
            (goto-char (point-min))
            (while (not (eobp))
-             (or (looking-at "#! *rnews +\\([0-9]+\\)")
-                 (error "Bad header."))
+             (unless (looking-at "#! *rnews +\\([0-9]+\\)")
+               (error "Bad header"))
              (forward-line 1)
              (setq beg (point)
-                   end (+ (point) (string-to-int 
-                                   (buffer-substring 
+                   end (+ (point) (string-to-int
+                                   (buffer-substring
                                     (match-beginning 1) (match-end 1)))))
              (switch-to-buffer tmp-buf)
              (erase-buffer)
+             (mm-disable-multibyte)
              (insert-buffer-substring msg-buf beg end)
-             (goto-char (point-min))
-             (search-forward "\n\n")
-             (forward-char -1)
-             (insert mail-header-separator)
-             (cond 
+             (cond
               ((string= (gnus-soup-reply-kind (car replies)) "news")
-               (message "Sending news message to %s..."
-                        (mail-fetch-field "newsgroups"))
+               (gnus-message 5 "Sending news message to %s..."
+                             (mail-fetch-field "newsgroups"))
                (sit-for 1)
-               (gnus-inews-article))
+               (let ((message-syntax-checks
+                      'dont-check-for-anything-just-trust-me)
+                     (method (if (message-functionp message-post-method)
+                                 (funcall message-post-method)
+                               message-post-method))
+                     result)
+                 (run-hooks 'message-send-news-hook)
+                 (gnus-open-server method)
+                 (message "Sending news via %s..."
+                          (gnus-server-string method))
+                 (unless (let ((mail-header-separator ""))
+                           (gnus-request-post method))
+                   (message "Couldn't send message via news: %s"
+                            (nnheader-get-report (car method))))))
               ((string= (gnus-soup-reply-kind (car replies)) "mail")
-               (message "Sending mail to %s..."
-                        (mail-fetch-field "to"))
+               (gnus-message 5 "Sending mail to %s..."
+                             (mail-fetch-field "to"))
                (sit-for 1)
-               (gnus-mail-send-and-exit))
+               (let ((mail-header-separator ""))
+                 (mm-with-unibyte-current-buffer
+                   (funcall (or message-send-mail-real-function
+                                message-send-mail-function)))))
               (t
                (error "Unknown reply kind")))
              (set-buffer msg-buf)
@@ -551,10 +576,10 @@ Return whether the unpacking was successful."
            (delete-file (buffer-file-name))
            (kill-buffer msg-buf)
            (kill-buffer tmp-buf)
-           (message "Sent packet"))))
+           (gnus-message 4 "Sent packet"))))
        (setq replies (cdr replies)))
       t)))
-                  
+
 (provide 'gnus-soup)
 
 ;;; gnus-soup.el ends here