1 ;;; riece-rdcc.el --- ruby implementation of DCC add-on
2 ;; Copyright (C) 1998-2003 Daiki Ueno
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: IRC, riece
7 ;; This file is part of Riece.
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 (defgroup riece-rdcc nil
27 "DCC implementation using ruby"
31 (defcustom riece-rdcc-server-address "127.0.0.1"
32 "Local address of the DCC server.
33 Only used for sending files."
37 (defvar riece-rdcc-requests nil)
39 (defvar riece-rdcc-request-user nil)
40 (defvar riece-rdcc-request-file nil)
41 (defvar riece-rdcc-request-size nil)
43 (defun riece-rdcc-server-filter (process input)
45 (set-buffer (process-buffer process))
46 (goto-char (point-max))
48 (goto-char (point-min))
49 (while (and (not (eobp))
50 (looking-at "\\([0-9]+\\)\n"))
51 (message "Sending %s...(%s/%d)"
52 riece-rdcc-request-file
53 (match-string 1) riece-rdcc-request-size)
56 (delete-region (point-min) (point)))))
58 (defun riece-rdcc-server-sentinel (process status)
59 (kill-buffer (process-buffer process))
60 (delete-process process))
62 (defun riece-command-dcc-send (user file)
64 (let ((completion-ignore-case t))
65 (unless riece-rdcc-server-address
66 (error "Set riece-rdcc-server-address to your host"))
67 (list (completing-read
69 (mapcar #'list (riece-get-users-on-server)))
70 (expand-file-name (read-file-name "File: ")))))
72 (start-process "DCC" " *DCC*" "ruby" "-rsocket")))
73 (process-send-string process (concat "\
74 server = TCPServer.new('" riece-rdcc-server-address "', 0)
75 puts(\"#{server.addr[3].split(/\\./).collect{|c| c.to_i}.pack('cccc').unpack('N')[0]} #{server.addr[1]}\")
76 session = server.accept
79 File.open('" file "') {|file|
80 bytes = file.read(1024)
88 (process-send-eof process)
90 (set-buffer (process-buffer process))
91 (while (and (eq (process-status process) 'run)
93 (goto-char (point-min))
94 (not (looking-at "\\([0-9]+\\) \\([0-9]+\\)"))))
95 (accept-process-output))
96 (if (eq (process-status process) 'run)
97 (let ((address (match-string 1))
98 (port (match-string 2)))
100 (make-local-variable 'riece-rdcc-request-size)
101 (setq riece-rdcc-request-file file
102 riece-rdcc-request-size (nth 7 (file-attributes file)))
103 (set-buffer-modified-p nil)
104 (set-process-filter process #'riece-rdcc-server-filter)
105 (set-process-sentinel process #'riece-rdcc-server-sentinel)
107 (format "PRIVMSG %s :\1DCC SEND %s %s %s %d\1\r\n"
108 user (file-name-nondirectory file)
110 riece-rdcc-request-size)))))))
112 (defun riece-rdcc-filter (process input)
114 (set-buffer (process-buffer process))
115 (goto-char (point-max))
117 (message "Receiving %s from %s...(%d/%d)"
118 (file-name-nondirectory buffer-file-name)
119 riece-rdcc-request-user
121 riece-rdcc-request-size)))
123 (defun riece-rdcc-sentinel (process status)
125 (set-buffer (process-buffer process))
126 (unless (= (buffer-size) riece-rdcc-request-size)
127 (error "Premature end of file"))
128 (message "Receiving %s from %s...done"
129 (file-name-nondirectory buffer-file-name)
130 riece-rdcc-request-user)
131 (let ((coding-system-for-write 'binary))
134 (defun riece-rdcc-decode-address (address)
136 (call-process "ruby" nil t nil "-e" (concat "\
137 puts(\"#{" address " >> 24 & 0xFF}.#{" address " >> 16 & 0xFF}.#{" address " >> 8 & 0xFF}.#{" address " & 0xFF}\")"))
138 (buffer-substring (point-min) (1- (point-max)))))
140 (defun riece-command-dcc-receive (request file)
143 (unless riece-rdcc-requests
144 (error "No request"))
146 (if (= (length riece-rdcc-requests) 1)
147 (car riece-rdcc-requests)
148 (with-output-to-temp-buffer "*Help*"
149 (let ((requests riece-rdcc-requests)
152 (princ (format "%2d: %s %s (%d bytes)\n"
155 (nth 1 (car requests))
156 (nth 4 (car requests))))
157 (setq index (1+ index)
158 requests (cdr requests)))))
159 (let ((number (read-string "Request#: ")))
160 (unless (string-match "^[0-9]+$" number)
161 (error "Not a number"))
162 (if (or (> (setq number (string-to-number number))
163 (length riece-rdcc-requests))
165 (error "Invalid number"))
166 (nth (1- number) riece-rdcc-requests)))
167 (expand-file-name (read-file-name "Save as: ")))))
168 (let* (selective-display
169 (coding-system-for-read 'binary)
170 (coding-system-for-write 'binary)
171 (process (open-network-stream
173 (riece-rdcc-decode-address (nth 2 request))
175 (setq riece-rdcc-requests (delq request riece-rdcc-requests))
176 (with-current-buffer (process-buffer process)
177 (set-buffer-multibyte nil)
178 (buffer-disable-undo)
179 (setq buffer-file-name file)
180 (make-local-variable 'riece-rdcc-request-user)
181 (setq riece-rdcc-request-user (car request))
182 (make-local-variable 'riece-rdcc-request-size)
183 (setq riece-rdcc-request-size (nth 4 request)))
184 (set-process-filter process #'riece-rdcc-filter)
185 (set-process-sentinel process #'riece-rdcc-sentinel)))
187 (defun riece-handle-dcc-request (prefix target message)
188 (let ((case-fold-search t))
190 "SEND \\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\)"
192 (let ((file (match-string 1 message))
193 (address (match-string 2 message))
194 (port (string-to-number (match-string 3 message)))
195 (size (string-to-number (match-string 4 message)))
196 (buffer (if (riece-channel-p target)
197 (cdr (riece-identity-assoc-no-server
198 (riece-make-identity target)
199 riece-channel-buffer-alist))))
200 (user (riece-prefix-nickname prefix)))
201 (setq riece-rdcc-requests
202 (cons (list user file address port size)
203 riece-rdcc-requests))
204 (riece-insert-change buffer (format "DCC SEND from %s\n" user))
206 (if (and riece-channel-buffer-mode
207 (not (eq buffer riece-channel-buffer)))
208 (list riece-dialogue-buffer riece-others-buffer)
209 riece-dialogue-buffer)
211 (riece-concat-server-name
212 (format "DCC SEND from %s (%s) to %s"
214 (riece-strip-user-at-host
215 (riece-prefix-user-at-host prefix))
220 (defun riece-rdcc-requires ()
223 (defvar riece-dialogue-mode-map)
224 (defun riece-rdcc-insinuate ()
225 (add-hook 'riece-ctcp-dcc-request-hook 'riece-handle-dcc-request)
226 (define-key riece-dialogue-mode-map "\C-ds" 'riece-command-dcc-send)
227 (define-key riece-dialogue-mode-map "\C-dr" 'riece-command-dcc-receive))
229 (provide 'riece-rdcc)
231 ;;; riece-rdcc.el ends here