(cf. the last paragraph of `kill-buffer' docstring)
[riece] / lisp / riece-rdcc.el
index d5217f0..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-"
   :group 'riece)
 
-(defcustom riece-rdcc-server-address "127.0.0.1"
+(defcustom riece-rdcc-server-address nil
   "Local address of the DCC server.
 Only used for sending files."
-  :type 'vector
+  :type 'string
+  :group 'riece-rdcc)
+
+(defcustom riece-rdcc-ruby-command "ruby"
+  "Command name for Ruby interpreter."
+  :type 'string
+  :group 'riece-rdcc)
+
+(defcustom riece-rdcc-send-program
+  '("\
+address = " address "
+unless address
+  sock = UDPSocket.new
+  sock.connect('164.46.176.4', 7)              # www.unixuser.org/echo
+  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]}\")
+session = server.accept
+if session
+  total = 0
+  File.open(" file ") {|file|
+    while (bytes = file.read(4096))
+      total += bytes.length
+      puts(\"#{total}\")
+      session.write(bytes)
+    end
+  }
+  session.close
+end
+")
+  "Ruby program to send file with DCC."
+  :type 'list
+  :group 'riece-rdcc)
+
+(defcustom riece-rdcc-decode-address-program
+  '("\
+puts(\"#{" address " >> 24 & 0xFF}.#{" address " >> 16 & 0xFF}.#{"
+    address " >> 8 & 0xFF}.#{" address " & 0xFF}\")")
+  "Ruby program to numeric IP address."
+  :type 'list
   :group 'riece-rdcc)
 
 (defvar riece-rdcc-requests nil)
@@ -40,6 +86,17 @@ Only used for sending files."
 (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))
+    (while pointer
+      (setq pointer (memq variable program))
+      (if pointer
+         (setcar pointer value)))
+    program))
+
 (defun riece-rdcc-server-filter (process input)
   (save-excursion
     (set-buffer (process-buffer process))
@@ -58,36 +115,30 @@ Only used for sending files."
 (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
    (let ((completion-ignore-case t))
-     (unless riece-rdcc-server-address
-       (error "Set riece-rdcc-server-address to your host"))
      (list (completing-read
            "User: "
            (mapcar #'list (riece-get-users-on-server)))
           (expand-file-name (read-file-name "File: ")))))
   (let ((process
-        (start-process "DCC" " *DCC*" "ruby" "-rsocket")))
-    (process-send-string process (concat "\
-server = TCPServer.new('" riece-rdcc-server-address "', 0)
-puts(\"#{server.addr[3].split(/\\./).collect{|c| c.to_i}.pack('cccc').unpack('N')[0]} #{server.addr[1]}\")
-session = server.accept
-if session
-  total = 0
-  File.open('" file "') {|file|
-    while (bytes = file.read(1024))
-      total += bytes.length
-      puts(\"#{total}\")
-      session.write(bytes)
-    end
-  }
-  session.close
-end
-"))
+        (start-process "DCC" (generate-new-buffer " *DCC*")
+                       "ruby" "-rsocket")))
+    (process-send-string process
+                        (apply #'concat
+                               (riece-rdcc-substitute-variables
+                                (riece-rdcc-substitute-variables
+                                 riece-rdcc-send-program
+                                 'address
+                                 (if riece-rdcc-server-address
+                                     (concat "'" riece-rdcc-server-address
+                                             "'")
+                                   "nil"))
+                                'file
+                                (concat "'" file "'"))))
     (process-send-eof process)
     (save-excursion
       (set-buffer (process-buffer process))
@@ -118,7 +169,7 @@ end
     (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)))
@@ -129,15 +180,21 @@ end
     (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
-    (call-process "ruby" nil t nil "-e" (concat "\
-puts(\"#{" address " >> 24 & 0xFF}.#{" address " >> 16 & 0xFF}.#{" address " >> 8 & 0xFF}.#{" address " & 0xFF}\")"))
+    (call-process riece-rdcc-ruby-command nil t nil "-e"
+                 (apply #'concat
+                        (riece-rdcc-substitute-variables
+                         riece-rdcc-decode-address-program
+                         'address
+                         address)))
     (buffer-substring (point-min) (1- (point-max)))))
 
 (defun riece-command-dcc-receive (request file)
@@ -145,43 +202,54 @@ puts(\"#{" address " >> 24 & 0xFF}.#{" address " >> 16 & 0xFF}.#{" address " >>
    (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)
@@ -197,7 +265,7 @@ puts(\"#{" address " >> 24 & 0xFF}.#{" address " >> 16 & 0xFF}.#{" address " >>
            (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)))
@@ -225,6 +293,7 @@ puts(\"#{" address " >> 24 & 0xFF}.#{" address " >> 16 & 0xFF}.#{" address " >>
 
 (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))