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 send" " *DCC send*" "ruby" "-rsocket")))
129 (process-send-string process
131 (riece-rdcc-substitute-variables
132 (riece-rdcc-substitute-variables
133 riece-rdcc-send-program
135 (if riece-rdcc-server-address
136 (concat "'" riece-rdcc-server-address
140 (concat "'" file "'"))))
141 (process-send-eof process)
143 (set-buffer (process-buffer process))
144 (while (and (eq (process-status process) 'run)
146 (goto-char (point-min))
147 (not (looking-at "\\([0-9]+\\) \\([0-9]+\\)"))))
148 (accept-process-output))
149 (if (eq (process-status process) 'run)
150 (let ((address (match-string 1))
151 (port (match-string 2)))
153 (make-local-variable 'riece-rdcc-request-size)
154 (setq riece-rdcc-request-file file
155 riece-rdcc-request-size (nth 7 (file-attributes file)))
156 (set-buffer-modified-p nil)
157 (set-process-filter process #'riece-rdcc-server-filter)
158 (set-process-sentinel process #'riece-rdcc-server-sentinel)
160 (format "PRIVMSG %s :\1DCC SEND %s %s %s %d\1\r\n"
161 user (file-name-nondirectory file)
163 riece-rdcc-request-size)))))))
165 (defun riece-rdcc-filter (process input)
167 (set-buffer (process-buffer process))
168 (goto-char (point-max))
170 (message "Receiving %s from %s...(%d/%d)"
171 (file-name-nondirectory riece-rdcc-request-file)
172 riece-rdcc-request-user
174 riece-rdcc-request-size)))
176 (defun riece-rdcc-sentinel (process status)
178 (set-buffer (process-buffer process))
179 (unless (= (buffer-size) riece-rdcc-request-size)
180 (error "Premature end of file"))
181 (message "Receiving %s from %s...done"
182 (file-name-nondirectory riece-rdcc-request-file)
183 riece-rdcc-request-user)
184 (let ((coding-system-for-write 'binary)
185 jka-compr-compression-info-list jam-zcat-filename-list)
186 (write-region (point-min) (point-max) riece-rdcc-request-file)))
187 (kill-buffer (process-buffer process))
188 (delete-process process))
190 (defun riece-rdcc-decode-address (address)
192 (call-process riece-rdcc-ruby-command nil t nil "-e"
194 (riece-rdcc-substitute-variables
195 riece-rdcc-decode-address-program
198 (buffer-substring (point-min) (1- (point-max)))))
200 (defun riece-command-dcc-receive (request file)
203 (unless riece-rdcc-requests
204 (error "No request"))
206 (if (= (length riece-rdcc-requests) 1)
207 (car riece-rdcc-requests)
208 (with-output-to-temp-buffer "*Help*"
209 (let ((requests riece-rdcc-requests)
212 (princ (format "%2d: %s %s (%d bytes)\n"
215 (nth 1 (car requests))
216 (nth 4 (car requests))))
217 (setq index (1+ index)
218 requests (cdr requests)))))
219 (let ((number (read-string "Request#: ")))
220 (unless (string-match "^[0-9]+$" number)
221 (error "Not a number"))
222 (if (or (> (setq number (string-to-number number))
223 (length riece-rdcc-requests))
225 (error "Invalid number"))
226 (nth (1- number) riece-rdcc-requests))))
227 (default-name (expand-file-name
228 (nth 1 request) default-directory)))
232 (concat "Save as (default "
233 (file-name-nondirectory default-name) ") ")
234 (file-name-directory default-name)
236 (let* (selective-display
237 (coding-system-for-read 'binary)
238 (coding-system-for-write 'binary)
239 (process (open-network-stream
240 "DCC receive" " *DCC receive*"
241 (riece-rdcc-decode-address (nth 2 request))
243 (setq riece-rdcc-requests (delq request riece-rdcc-requests))
244 (with-current-buffer (process-buffer process)
245 (if (fboundp 'set-buffer-multibyte)
246 (set-buffer-multibyte nil))
247 (buffer-disable-undo)
249 (make-local-variable 'riece-rdcc-request-user)
250 (setq riece-rdcc-request-user (car request))
251 (make-local-variable 'riece-rdcc-request-file)
252 (setq riece-rdcc-request-file file)
253 (make-local-variable 'riece-rdcc-request-size)
254 (setq riece-rdcc-request-size (nth 4 request)))
255 (set-process-filter process #'riece-rdcc-filter)
256 (set-process-sentinel process #'riece-rdcc-sentinel)))
258 (defun riece-handle-dcc-request (prefix target message)
259 (let ((case-fold-search t))
261 "SEND \\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\)"
263 (let ((file (match-string 1 message))
264 (address (match-string 2 message))
265 (port (string-to-number (match-string 3 message)))
266 (size (string-to-number (match-string 4 message)))
267 (buffer (if (riece-channel-p target)
268 (cdr (riece-identity-assoc-no-server
269 (riece-make-identity target)
270 riece-channel-buffer-alist))))
271 (user (riece-prefix-nickname prefix)))
272 (setq riece-rdcc-requests
273 (cons (list user file address port size)
274 riece-rdcc-requests))
275 (riece-insert-change buffer (format "DCC SEND from %s\n" user))
277 (if (and riece-channel-buffer-mode
278 (not (eq buffer riece-channel-buffer)))
279 (list riece-dialogue-buffer riece-others-buffer)
280 riece-dialogue-buffer)
282 (riece-concat-server-name
283 (format "DCC SEND from %s (%s) to %s"
285 (riece-strip-user-at-host
286 (riece-prefix-user-at-host prefix))
291 (defun riece-rdcc-requires ()
294 (defvar riece-dialogue-mode-map)
295 (defun riece-rdcc-insinuate ()
296 (add-hook 'riece-ctcp-dcc-request-hook 'riece-handle-dcc-request)
297 (define-key riece-dialogue-mode-map "\C-ds" 'riece-command-dcc-send)
298 (define-key riece-dialogue-mode-map "\C-dr" 'riece-command-dcc-receive))
300 (provide 'riece-rdcc)
302 ;;; riece-rdcc.el ends here