711977abef98be37b8c24e4c16e09ffc0ecdb929
[riece] / lisp / riece-rdcc.el
1 ;;; riece-rdcc.el --- ruby implementation of DCC add-on
2 ;; Copyright (C) 1998-2003 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: IRC, riece
6
7 ;; This file is part of Riece.
8
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)
12 ;; any later version.
13
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.
18
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.
23
24 ;;; Code:
25
26 (require 'riece-globals)
27 (require 'riece-misc)
28 (require 'riece-channel)
29 (require 'riece-identity)
30
31 (defgroup riece-rdcc nil
32   "DCC implementation using ruby"
33   :prefix "riece-"
34   :group 'riece)
35
36 (defcustom riece-rdcc-server-address nil
37   "Local address of the DCC server.
38 Only used for sending files."
39   :type 'string
40   :group 'riece-rdcc)
41
42 (defcustom riece-rdcc-ruby-command "ruby"
43   "Command name for Ruby interpreter."
44   :type 'string
45   :group 'riece-rdcc)
46
47 (defcustom riece-rdcc-send-program
48   '("\
49 address = " address "
50 unless address
51   sock = UDPSocket.new
52   sock.connect('164.46.176.4', 7)               # www.unixuser.org/echo
53   address = sock.getsockname[4 .. 7].unpack('CCCC').join('.')
54 end
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
58 if session
59   total = 0
60   File.open(" file ") {|file|
61     while (bytes = file.read(4096))
62       total += bytes.length
63       puts(\"#{total}\")
64       session.write(bytes)
65     end
66   }
67   session.close
68 end
69 ")
70   "Ruby program to send file with DCC."
71   :type 'list
72   :group 'riece-rdcc)
73
74 (defcustom riece-rdcc-decode-address-program
75   '("\
76 puts(\"#{" address " >> 24 & 0xFF}.#{" address " >> 16 & 0xFF}.#{"
77     address " >> 8 & 0xFF}.#{" address " & 0xFF}\")")
78   "Ruby program to numeric IP address."
79   :type 'list
80   :group 'riece-rdcc)
81
82 (defvar riece-rdcc-requests nil)
83
84 (defvar riece-rdcc-request-user nil)
85 (defvar riece-rdcc-request-file nil)
86 (defvar riece-rdcc-request-size nil)
87
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))
93     (while pointer
94       (setq pointer (memq variable program))
95       (if pointer
96           (setcar pointer value)))
97     program))
98
99 (defun riece-rdcc-server-filter (process input)
100   (save-excursion
101     (set-buffer (process-buffer process))
102     (goto-char (point-max))
103     (insert input)
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)
110       (forward-line))
111     (unless (eobp)
112       (delete-region (point-min) (point)))))
113
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))
119
120 (defun riece-command-dcc-send (user file)
121   (interactive
122    (let ((completion-ignore-case t))
123      (list (completing-read
124             "User: "
125             (mapcar #'list (riece-get-users-on-server)))
126            (expand-file-name (read-file-name "File: ")))))
127   (let ((process
128          (start-process "DCC send" " *DCC send*" "ruby" "-rsocket")))
129     (process-send-string process
130                          (apply #'concat
131                                 (riece-rdcc-substitute-variables
132                                  (riece-rdcc-substitute-variables
133                                   riece-rdcc-send-program
134                                   'address
135                                   (if riece-rdcc-server-address
136                                       (concat "'" riece-rdcc-server-address
137                                               "'")
138                                     "nil"))
139                                  'file
140                                  (concat "'" file "'"))))
141     (process-send-eof process)
142     (save-excursion
143       (set-buffer (process-buffer process))
144       (while (and (eq (process-status process) 'run)
145                   (progn
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)))
152             (erase-buffer)
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)
159             (riece-send-string
160              (format "PRIVMSG %s :\1DCC SEND %s %s %s %d\1\r\n"
161                      user (file-name-nondirectory file)
162                      address port
163                      riece-rdcc-request-size)))))))
164
165 (defun riece-rdcc-filter (process input)
166   (save-excursion
167     (set-buffer (process-buffer process))
168     (goto-char (point-max))
169     (insert input)
170     (message "Receiving %s from %s...(%d/%d)"
171              (file-name-nondirectory riece-rdcc-request-file)
172              riece-rdcc-request-user
173              (1- (point))
174              riece-rdcc-request-size)))
175
176 (defun riece-rdcc-sentinel (process status)
177   (save-excursion
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))
189
190 (defun riece-rdcc-decode-address (address)
191   (with-temp-buffer
192     (call-process riece-rdcc-ruby-command nil t nil "-e"
193                   (apply #'concat
194                          (riece-rdcc-substitute-variables
195                           riece-rdcc-decode-address-program
196                           'address
197                           address)))
198     (buffer-substring (point-min) (1- (point-max)))))
199
200 (defun riece-command-dcc-receive (request file)
201   (interactive
202    (progn
203      (unless riece-rdcc-requests
204        (error "No request"))
205      (let* ((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)
210                        (index 1))
211                    (while requests
212                      (princ (format "%2d: %s %s (%d bytes)\n"
213                                     index
214                                     (car (car requests))
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))
224                          (< number 1))
225                      (error "Invalid number"))
226                  (nth (1- number) riece-rdcc-requests))))
227             (default-name (expand-file-name
228                            (nth 1 request) default-directory)))
229        (list request
230              (expand-file-name
231               (read-file-name
232                (concat "Save as (default "
233                        (file-name-nondirectory default-name) ") ")
234                (file-name-directory default-name)
235                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))
242                    (nth 3 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)
248       (erase-buffer)
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)))
257
258 (defun riece-handle-dcc-request (prefix target message)
259   (let ((case-fold-search t))
260     (when (string-match
261            "SEND \\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\)"
262            message)
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))
276         (riece-insert-change
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)
281          (concat
282           (riece-concat-server-name
283            (format "DCC SEND from %s (%s) to %s"
284                    user
285                    (riece-strip-user-at-host
286                     (riece-prefix-user-at-host prefix))
287                    target))
288           "\n")))
289       t)))
290
291 (defun riece-rdcc-requires ()
292   '(riece-ctcp))
293
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))
299
300 (provide 'riece-rdcc)
301
302 ;;; riece-rdcc.el ends here