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 (with-current-buffer (process-buffer process)
60 (message "Sending %s...done" riece-rdcc-request-file))
61 (kill-buffer (process-buffer process))
62 (delete-process process))
64 (defun riece-command-dcc-send (user file)
66 (let ((completion-ignore-case t))
67 (unless riece-rdcc-server-address
68 (error "Set riece-rdcc-server-address to your host"))
69 (list (completing-read
71 (mapcar #'list (riece-get-users-on-server)))
72 (expand-file-name (read-file-name "File: ")))))
74 (start-process "DCC" " *DCC*" "ruby" "-rsocket")))
75 (process-send-string process (concat "\
76 server = TCPServer.new('" riece-rdcc-server-address "', 0)
77 puts(\"#{server.addr[3].split(/\\./).collect{|c| c.to_i}.pack('cccc').unpack('N')[0]} #{server.addr[1]}\")
78 session = server.accept
81 File.open('" file "') {|file|
82 while (bytes = file.read(1024))
91 (process-send-eof process)
93 (set-buffer (process-buffer process))
94 (while (and (eq (process-status process) 'run)
96 (goto-char (point-min))
97 (not (looking-at "\\([0-9]+\\) \\([0-9]+\\)"))))
98 (accept-process-output))
99 (if (eq (process-status process) 'run)
100 (let ((address (match-string 1))
101 (port (match-string 2)))
103 (make-local-variable 'riece-rdcc-request-size)
104 (setq riece-rdcc-request-file file
105 riece-rdcc-request-size (nth 7 (file-attributes file)))
106 (set-buffer-modified-p nil)
107 (set-process-filter process #'riece-rdcc-server-filter)
108 (set-process-sentinel process #'riece-rdcc-server-sentinel)
110 (format "PRIVMSG %s :\1DCC SEND %s %s %s %d\1\r\n"
111 user (file-name-nondirectory file)
113 riece-rdcc-request-size)))))))
115 (defun riece-rdcc-filter (process input)
117 (set-buffer (process-buffer process))
118 (goto-char (point-max))
120 (message "Receiving %s from %s...(%d/%d)"
121 (file-name-nondirectory buffer-file-name)
122 riece-rdcc-request-user
124 riece-rdcc-request-size)))
126 (defun riece-rdcc-sentinel (process status)
128 (set-buffer (process-buffer process))
129 (unless (= (buffer-size) riece-rdcc-request-size)
130 (error "Premature end of file"))
131 (message "Receiving %s from %s...done"
132 (file-name-nondirectory buffer-file-name)
133 riece-rdcc-request-user)
134 (let ((coding-system-for-write 'binary))
137 (defun riece-rdcc-decode-address (address)
139 (call-process "ruby" nil t nil "-e" (concat "\
140 puts(\"#{" address " >> 24 & 0xFF}.#{" address " >> 16 & 0xFF}.#{" address " >> 8 & 0xFF}.#{" address " & 0xFF}\")"))
141 (buffer-substring (point-min) (1- (point-max)))))
143 (defun riece-command-dcc-receive (request file)
146 (unless riece-rdcc-requests
147 (error "No request"))
149 (if (= (length riece-rdcc-requests) 1)
150 (car riece-rdcc-requests)
151 (with-output-to-temp-buffer "*Help*"
152 (let ((requests riece-rdcc-requests)
155 (princ (format "%2d: %s %s (%d bytes)\n"
158 (nth 1 (car requests))
159 (nth 4 (car requests))))
160 (setq index (1+ index)
161 requests (cdr requests)))))
162 (let ((number (read-string "Request#: ")))
163 (unless (string-match "^[0-9]+$" number)
164 (error "Not a number"))
165 (if (or (> (setq number (string-to-number number))
166 (length riece-rdcc-requests))
168 (error "Invalid number"))
169 (nth (1- number) riece-rdcc-requests)))
170 (expand-file-name (read-file-name "Save as: ")))))
171 (let* (selective-display
172 (coding-system-for-read 'binary)
173 (coding-system-for-write 'binary)
174 (process (open-network-stream
176 (riece-rdcc-decode-address (nth 2 request))
178 (setq riece-rdcc-requests (delq request riece-rdcc-requests))
179 (with-current-buffer (process-buffer process)
180 (set-buffer-multibyte nil)
181 (buffer-disable-undo)
182 (setq buffer-file-name file)
183 (make-local-variable 'riece-rdcc-request-user)
184 (setq riece-rdcc-request-user (car request))
185 (make-local-variable 'riece-rdcc-request-size)
186 (setq riece-rdcc-request-size (nth 4 request)))
187 (set-process-filter process #'riece-rdcc-filter)
188 (set-process-sentinel process #'riece-rdcc-sentinel)))
190 (defun riece-handle-dcc-request (prefix target message)
191 (let ((case-fold-search t))
193 "SEND \\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\)"
195 (let ((file (match-string 1 message))
196 (address (match-string 2 message))
197 (port (string-to-number (match-string 3 message)))
198 (size (string-to-number (match-string 4 message)))
199 (buffer (if (riece-channel-p target)
200 (cdr (riece-identity-assoc-no-server
201 (riece-make-identity target)
202 riece-channel-buffer-alist))))
203 (user (riece-prefix-nickname prefix)))
204 (setq riece-rdcc-requests
205 (cons (list user file address port size)
206 riece-rdcc-requests))
207 (riece-insert-change buffer (format "DCC SEND from %s\n" user))
209 (if (and riece-channel-buffer-mode
210 (not (eq buffer riece-channel-buffer)))
211 (list riece-dialogue-buffer riece-others-buffer)
212 riece-dialogue-buffer)
214 (riece-concat-server-name
215 (format "DCC SEND from %s (%s) to %s"
217 (riece-strip-user-at-host
218 (riece-prefix-user-at-host prefix))
223 (defun riece-rdcc-requires ()
226 (defvar riece-dialogue-mode-map)
227 (defun riece-rdcc-insinuate ()
228 (add-hook 'riece-ctcp-dcc-request-hook 'riece-handle-dcc-request)
229 (define-key riece-dialogue-mode-map "\C-ds" 'riece-command-dcc-send)
230 (define-key riece-dialogue-mode-map "\C-dr" 'riece-command-dcc-receive))
232 (provide 'riece-rdcc)
234 ;;; riece-rdcc.el ends here