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 (require 'riece-globals)
28 (require 'riece-channel)
29 (require 'riece-identity)
31 (defgroup riece-rdcc nil
32 "DCC implementation using ruby"
36 (defcustom riece-rdcc-server-address nil
37 "Local address of the DCC server.
38 Only used for sending files."
42 (defcustom riece-rdcc-ruby-command "ruby"
43 "Command name for Ruby interpreter."
47 (defcustom riece-rdcc-send-program
52 sock.connect('164.46.176.4', 7) # www.unixuser.org/echo
53 address = sock.getsockname[4 .. 7].unpack('CCCC').join('.')
55 server = TCPServer.new(address, 0)
56 puts(\"#{server.addr[3].split(/\\./).collect{|c| c.to_i}.pack('CCCC').unpack('N')[0]} #{server.addr[1]}\")
57 session = server.accept
60 File.open(" file ") {|file|
61 while (bytes = file.read(4096))
70 "Ruby program to send file with DCC."
74 (defcustom riece-rdcc-decode-address-program
76 puts(\"#{" address " >> 24 & 0xFF}.#{" address " >> 16 & 0xFF}.#{"
77 address " >> 8 & 0xFF}.#{" address " & 0xFF}\")")
78 "Ruby program to numeric IP address."
82 (defvar riece-rdcc-requests nil)
84 (defvar riece-rdcc-request-user nil)
85 (defvar riece-rdcc-request-file nil)
86 (defvar riece-rdcc-request-size nil)
88 (defvar jka-compr-compression-info-list)
89 (defvar jam-zcat-filename-list)
90 (defun riece-rdcc-substitute-variables (program variable value)
91 (setq program (copy-sequence program))
92 (let ((pointer program))
94 (setq pointer (memq variable program))
96 (setcar pointer value)))
99 (defun riece-rdcc-server-filter (process input)
101 (set-buffer (process-buffer process))
102 (goto-char (point-max))
104 (goto-char (point-min))
105 (while (and (not (eobp))
106 (looking-at "\\([0-9]+\\)\n"))
107 (message "Sending %s...(%s/%d)"
108 riece-rdcc-request-file
109 (match-string 1) riece-rdcc-request-size)
112 (delete-region (point-min) (point)))))
114 (defun riece-rdcc-server-sentinel (process status)
115 (with-current-buffer (process-buffer process)
116 (message "Sending %s...done" riece-rdcc-request-file))
117 (kill-buffer (process-buffer process))
118 (delete-process process))
120 (defun riece-command-dcc-send (user file)
122 (let ((completion-ignore-case t))
123 (list (completing-read
125 (mapcar #'list (riece-get-users-on-server)))
126 (expand-file-name (read-file-name "File: ")))))
128 (start-process "DCC" (generate-new-buffer " *DCC*")
130 (process-send-string process
132 (riece-rdcc-substitute-variables
133 (riece-rdcc-substitute-variables
134 riece-rdcc-send-program
136 (if riece-rdcc-server-address
137 (concat "'" riece-rdcc-server-address
141 (concat "'" file "'"))))
142 (process-send-eof process)
144 (set-buffer (process-buffer process))
145 (while (and (eq (process-status process) 'run)
147 (goto-char (point-min))
148 (not (looking-at "\\([0-9]+\\) \\([0-9]+\\)"))))
149 (accept-process-output))
150 (if (eq (process-status process) 'run)
151 (let ((address (match-string 1))
152 (port (match-string 2)))
154 (make-local-variable 'riece-rdcc-request-size)
155 (setq riece-rdcc-request-file file
156 riece-rdcc-request-size (nth 7 (file-attributes file)))
157 (set-buffer-modified-p nil)
158 (set-process-filter process #'riece-rdcc-server-filter)
159 (set-process-sentinel process #'riece-rdcc-server-sentinel)
161 (format "PRIVMSG %s :\1DCC SEND %s %s %s %d\1\r\n"
162 user (file-name-nondirectory file)
164 riece-rdcc-request-size)))))))
166 (defun riece-rdcc-filter (process input)
168 (set-buffer (process-buffer process))
169 (goto-char (point-max))
171 (message "Receiving %s from %s...(%d/%d)"
172 (file-name-nondirectory riece-rdcc-request-file)
173 riece-rdcc-request-user
175 riece-rdcc-request-size)))
177 (defun riece-rdcc-sentinel (process status)
179 (set-buffer (process-buffer process))
180 (unless (= (buffer-size) riece-rdcc-request-size)
181 (error "Premature end of file"))
182 (message "Receiving %s from %s...done"
183 (file-name-nondirectory riece-rdcc-request-file)
184 riece-rdcc-request-user)
185 (let ((coding-system-for-write 'binary)
186 jka-compr-compression-info-list jam-zcat-filename-list)
187 (write-region (point-min) (point-max) riece-rdcc-request-file)))
188 (kill-buffer (process-buffer process))
189 (delete-process process))
191 (defun riece-rdcc-decode-address (address)
193 (call-process riece-rdcc-ruby-command nil t nil "-e"
195 (riece-rdcc-substitute-variables
196 riece-rdcc-decode-address-program
199 (buffer-substring (point-min) (1- (point-max)))))
201 (defun riece-command-dcc-receive (request file)
204 (unless riece-rdcc-requests
205 (error "No request"))
207 (if (= (length riece-rdcc-requests) 1)
208 (car riece-rdcc-requests)
209 (with-output-to-temp-buffer "*Help*"
210 (let ((requests riece-rdcc-requests)
213 (princ (format "%2d: %s %s (%d bytes)\n"
216 (nth 1 (car requests))
217 (nth 4 (car requests))))
218 (setq index (1+ index)
219 requests (cdr requests)))))
220 (let ((number (read-string "Request#: ")))
221 (unless (string-match "^[0-9]+$" number)
222 (error "Not a number"))
223 (if (or (> (setq number (string-to-number number))
224 (length riece-rdcc-requests))
226 (error "Invalid number"))
227 (nth (1- number) riece-rdcc-requests))))
228 (default-name (expand-file-name
229 (nth 1 request) default-directory)))
233 (concat "Save as (default "
234 (file-name-nondirectory default-name) ") ")
235 (file-name-directory default-name)
237 (let* (selective-display
238 (coding-system-for-read 'binary)
239 (coding-system-for-write 'binary)
240 (process (open-network-stream
241 "DCC" (generate-new-buffer " *DCC*")
242 (riece-rdcc-decode-address (nth 2 request))
244 (setq riece-rdcc-requests (delq request riece-rdcc-requests))
245 (with-current-buffer (process-buffer process)
246 (if (fboundp 'set-buffer-multibyte)
247 (set-buffer-multibyte nil))
248 (buffer-disable-undo)
250 (make-local-variable 'riece-rdcc-request-user)
251 (setq riece-rdcc-request-user (car request))
252 (make-local-variable 'riece-rdcc-request-file)
253 (setq riece-rdcc-request-file file)
254 (make-local-variable 'riece-rdcc-request-size)
255 (setq riece-rdcc-request-size (nth 4 request)))
256 (set-process-filter process #'riece-rdcc-filter)
257 (set-process-sentinel process #'riece-rdcc-sentinel)))
259 (defun riece-handle-dcc-request (prefix target message)
260 (let ((case-fold-search t))
262 "SEND \\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\)"
264 (let ((file (match-string 1 message))
265 (address (match-string 2 message))
266 (port (string-to-number (match-string 3 message)))
267 (size (string-to-number (match-string 4 message)))
268 (buffer (if (riece-channel-p target)
269 (cdr (riece-identity-assoc-no-server
270 (riece-make-identity target)
271 riece-channel-buffer-alist))))
272 (user (riece-prefix-nickname prefix)))
273 (setq riece-rdcc-requests
274 (cons (list user file address port size)
275 riece-rdcc-requests))
276 (riece-insert-change buffer (format "DCC SEND from %s\n" user))
278 (if (and riece-channel-buffer-mode
279 (not (eq buffer riece-channel-buffer)))
280 (list riece-dialogue-buffer riece-others-buffer)
281 riece-dialogue-buffer)
283 (riece-concat-server-name
284 (format "DCC SEND from %s (%s) to %s"
286 (riece-strip-user-at-host
287 (riece-prefix-user-at-host prefix))
292 (defun riece-rdcc-requires ()
295 (defvar riece-dialogue-mode-map)
296 (defun riece-rdcc-insinuate ()
297 (add-hook 'riece-ctcp-dcc-request-hook 'riece-handle-dcc-request)
298 (define-key riece-dialogue-mode-map "\C-ds" 'riece-command-dcc-send)
299 (define-key riece-dialogue-mode-map "\C-dr" 'riece-command-dcc-receive))
301 (provide 'riece-rdcc)
303 ;;; riece-rdcc.el ends here