ae16c9d61dc59d20f5e8a97d4300039e4c889b40
[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" (generate-new-buffer " *DCC*")
129                         "ruby" "-rsocket")))
130     (process-send-string process
131                          (apply #'concat
132                                 (riece-rdcc-substitute-variables
133                                  (riece-rdcc-substitute-variables
134                                   riece-rdcc-send-program
135                                   'address
136                                   (if riece-rdcc-server-address
137                                       (concat "'" riece-rdcc-server-address
138                                               "'")
139                                     "nil"))
140                                  'file
141                                  (concat "'" file "'"))))
142     (process-send-eof process)
143     (save-excursion
144       (set-buffer (process-buffer process))
145       (while (and (eq (process-status process) 'run)
146                   (progn
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)))
153             (erase-buffer)
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)
160             (riece-send-string
161              (format "PRIVMSG %s :\1DCC SEND %s %s %s %d\1\r\n"
162                      user (file-name-nondirectory file)
163                      address port
164                      riece-rdcc-request-size)))))))
165
166 (defun riece-rdcc-filter (process input)
167   (save-excursion
168     (set-buffer (process-buffer process))
169     (goto-char (point-max))
170     (insert input)
171     (message "Receiving %s from %s...(%d/%d)"
172              (file-name-nondirectory riece-rdcc-request-file)
173              riece-rdcc-request-user
174              (1- (point))
175              riece-rdcc-request-size)))
176
177 (defun riece-rdcc-sentinel (process status)
178   (save-excursion
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))
190
191 (defun riece-rdcc-decode-address (address)
192   (with-temp-buffer
193     (call-process riece-rdcc-ruby-command nil t nil "-e"
194                   (apply #'concat
195                          (riece-rdcc-substitute-variables
196                           riece-rdcc-decode-address-program
197                           'address
198                           address)))
199     (buffer-substring (point-min) (1- (point-max)))))
200
201 (defun riece-command-dcc-receive (request file)
202   (interactive
203    (progn
204      (unless riece-rdcc-requests
205        (error "No request"))
206      (let* ((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)
211                        (index 1))
212                    (while requests
213                      (princ (format "%2d: %s %s (%d bytes)\n"
214                                     index
215                                     (car (car requests))
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))
225                          (< number 1))
226                      (error "Invalid number"))
227                  (nth (1- number) riece-rdcc-requests))))
228             (default-name (expand-file-name
229                            (nth 1 request) default-directory)))
230        (list request
231              (expand-file-name
232               (read-file-name
233                (concat "Save as (default "
234                        (file-name-nondirectory default-name) ") ")
235                (file-name-directory default-name)
236                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))
243                    (nth 3 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)
249       (erase-buffer)
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)))
258
259 (defun riece-handle-dcc-request (prefix target message)
260   (let ((case-fold-search t))
261     (when (string-match
262            "SEND \\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\)"
263            message)
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))
277         (riece-insert-change
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)
282          (concat
283           (riece-concat-server-name
284            (format "DCC SEND from %s (%s) to %s"
285                    user
286                    (riece-strip-user-at-host
287                     (riece-prefix-user-at-host prefix))
288                    target))
289           "\n")))
290       t)))
291
292 (defun riece-rdcc-requires ()
293   '(riece-ctcp))
294
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))
300
301 (provide 'riece-rdcc)
302
303 ;;; riece-rdcc.el ends here