(cf. the last paragraph of `kill-buffer' docstring)
[riece] / lisp / riece-rdcc.el
index f1a5f95..e02b587 100644 (file)
 
 ;;; Code:
 
+(require 'riece-globals)
+(require 'riece-misc)
+(require 'riece-channel)
+(require 'riece-identity)
+(require 'riece-ctcp)                  ;for riece-ctcp-additional-clientinfo
+
 (defgroup riece-rdcc nil
   "DCC implementation using ruby"
   :prefix "riece-"
@@ -41,10 +47,11 @@ Only used for sending files."
 
 (defcustom riece-rdcc-send-program
   '("\
-unless " address "
+address = " address "
+unless address
   sock = UDPSocket.new
   sock.connect('164.46.176.4', 7)              # www.unixuser.org/echo
-  address = sock.getsockname[4 .. 8].unpack('CCCC').join('.')
+  address = sock.getsockname[4 .. 7].unpack('CCCC').join('.')
 end
 server = TCPServer.new(address, 0)
 puts(\"#{server.addr[3].split(/\\./).collect{|c| c.to_i}.pack('CCCC').unpack('N')[0]} #{server.addr[1]}\")
@@ -79,6 +86,8 @@ puts(\"#{" address " >> 24 & 0xFF}.#{" address " >> 16 & 0xFF}.#{"
 (defvar riece-rdcc-request-file nil)
 (defvar riece-rdcc-request-size nil)
 
+(defvar jka-compr-compression-info-list)
+(defvar jam-zcat-filename-list)
 (defun riece-rdcc-substitute-variables (program variable value)
   (setq program (copy-sequence program))
   (let ((pointer program))
@@ -106,8 +115,7 @@ puts(\"#{" address " >> 24 & 0xFF}.#{" address " >> 16 & 0xFF}.#{"
 (defun riece-rdcc-server-sentinel (process status)
   (with-current-buffer (process-buffer process)
     (message "Sending %s...done" riece-rdcc-request-file))
-  (kill-buffer (process-buffer process))
-  (delete-process process))
+  (kill-buffer (process-buffer process)))
 
 (defun riece-command-dcc-send (user file)
   (interactive
@@ -117,7 +125,8 @@ puts(\"#{" address " >> 24 & 0xFF}.#{" address " >> 16 & 0xFF}.#{"
            (mapcar #'list (riece-get-users-on-server)))
           (expand-file-name (read-file-name "File: ")))))
   (let ((process
-        (start-process "DCC" " *DCC*" "ruby" "-rsocket")))
+        (start-process "DCC" (generate-new-buffer " *DCC*")
+                       "ruby" "-rsocket")))
     (process-send-string process
                         (apply #'concat
                                (riece-rdcc-substitute-variables
@@ -160,7 +169,7 @@ puts(\"#{" address " >> 24 & 0xFF}.#{" address " >> 16 & 0xFF}.#{"
     (goto-char (point-max))
     (insert input)
     (message "Receiving %s from %s...(%d/%d)"
-            (file-name-nondirectory buffer-file-name)
+            (file-name-nondirectory riece-rdcc-request-file)
             riece-rdcc-request-user
             (1- (point))
             riece-rdcc-request-size)))
@@ -171,10 +180,12 @@ puts(\"#{" address " >> 24 & 0xFF}.#{" address " >> 16 & 0xFF}.#{"
     (unless (= (buffer-size) riece-rdcc-request-size)
       (error "Premature end of file"))
     (message "Receiving %s from %s...done"
-            (file-name-nondirectory buffer-file-name)
+            (file-name-nondirectory riece-rdcc-request-file)
             riece-rdcc-request-user)
-    (let ((coding-system-for-write 'binary))
-      (save-buffer))))
+    (let ((coding-system-for-write 'binary)
+         jka-compr-compression-info-list jam-zcat-filename-list)
+      (write-region (point-min) (point-max) riece-rdcc-request-file)))
+  (kill-buffer (process-buffer process)))
 
 (defun riece-rdcc-decode-address (address)
   (with-temp-buffer
@@ -191,43 +202,54 @@ puts(\"#{" address " >> 24 & 0xFF}.#{" address " >> 16 & 0xFF}.#{"
    (progn
      (unless riece-rdcc-requests
        (error "No request"))
-     (list
-      (if (= (length riece-rdcc-requests) 1)
-         (car riece-rdcc-requests)
-       (with-output-to-temp-buffer "*Help*"
-         (let ((requests riece-rdcc-requests)
-               (index 1))
-           (while requests
-             (princ (format "%2d: %s %s (%d bytes)\n"
-                            index
-                            (car (car requests))
-                            (nth 1 (car requests))
-                            (nth 4 (car requests))))
-             (setq index (1+ index)
-                   requests (cdr requests)))))
-       (let ((number (read-string "Request#: ")))
-         (unless (string-match "^[0-9]+$" number)
-           (error "Not a number"))
-         (if (or (> (setq number (string-to-number number))
-                    (length riece-rdcc-requests))
-                 (< number 1))
-             (error "Invalid number"))
-         (nth (1- number) riece-rdcc-requests)))
-      (expand-file-name (read-file-name "Save as: ")))))
+     (let* ((request
+            (if (= (length riece-rdcc-requests) 1)
+                (car riece-rdcc-requests)
+              (with-output-to-temp-buffer "*Help*"
+                (let ((requests riece-rdcc-requests)
+                      (index 1))
+                  (while requests
+                    (princ (format "%2d: %s %s (%d bytes)\n"
+                                   index
+                                   (car (car requests))
+                                   (nth 1 (car requests))
+                                   (nth 4 (car requests))))
+                    (setq index (1+ index)
+                          requests (cdr requests)))))
+              (let ((number (read-string "Request#: ")))
+                (unless (string-match "^[0-9]+$" number)
+                  (error "Not a number"))
+                (if (or (> (setq number (string-to-number number))
+                           (length riece-rdcc-requests))
+                        (< number 1))
+                    (error "Invalid number"))
+                (nth (1- number) riece-rdcc-requests))))
+           (default-name (expand-file-name
+                          (nth 1 request) default-directory)))
+       (list request
+            (expand-file-name
+             (read-file-name
+              (concat "Save as (default "
+                      (file-name-nondirectory default-name) ") ")
+              (file-name-directory default-name)
+              default-name))))))
   (let* (selective-display
         (coding-system-for-read 'binary)
         (coding-system-for-write 'binary)
         (process (open-network-stream
-                  "DCC" " *DCC*"
+                  "DCC" (generate-new-buffer " *DCC*")
                   (riece-rdcc-decode-address (nth 2 request))
                   (nth 3 request))))
     (setq riece-rdcc-requests (delq request riece-rdcc-requests))
     (with-current-buffer (process-buffer process)
-      (set-buffer-multibyte nil)
+      (if (fboundp 'set-buffer-multibyte)
+         (set-buffer-multibyte nil))
       (buffer-disable-undo)
-      (setq buffer-file-name file)
+      (erase-buffer)
       (make-local-variable 'riece-rdcc-request-user)
       (setq riece-rdcc-request-user (car request))
+      (make-local-variable 'riece-rdcc-request-file)
+      (setq riece-rdcc-request-file file)
       (make-local-variable 'riece-rdcc-request-size)
       (setq riece-rdcc-request-size (nth 4 request)))
     (set-process-filter process #'riece-rdcc-filter)
@@ -243,7 +265,7 @@ puts(\"#{" address " >> 24 & 0xFF}.#{" address " >> 16 & 0xFF}.#{"
            (port (string-to-number (match-string 3 message)))
            (size (string-to-number (match-string 4 message)))
            (buffer (if (riece-channel-p target)
-                       (cdr (riece-identity-assoc-no-server
+                       (cdr (riece-identity-assoc
                              (riece-make-identity target)
                              riece-channel-buffer-alist))))
            (user (riece-prefix-nickname prefix)))
@@ -271,6 +293,7 @@ puts(\"#{" address " >> 24 & 0xFF}.#{" address " >> 16 & 0xFF}.#{"
 
 (defvar riece-dialogue-mode-map)
 (defun riece-rdcc-insinuate ()
+  (add-to-list 'riece-ctcp-additional-clientinfo "DCC" t)
   (add-hook 'riece-ctcp-dcc-request-hook 'riece-handle-dcc-request)
   (define-key riece-dialogue-mode-map "\C-ds" 'riece-command-dcc-send)
   (define-key riece-dialogue-mode-map "\C-dr" 'riece-command-dcc-receive))