b54eb3bd4f4dd97542a66a1acf35ccf797646562
[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 (require 'riece-ctcp)                   ;for riece-ctcp-additional-clientinfo
31
32 (defgroup riece-rdcc nil
33   "DCC implementation using ruby"
34   :prefix "riece-"
35   :group 'riece)
36
37 (defcustom riece-rdcc-server-address nil
38   "Local address of the DCC server.
39 Only used for sending files."
40   :type 'string
41   :group 'riece-rdcc)
42
43 (defcustom riece-rdcc-ruby-command "ruby"
44   "Command name for Ruby interpreter."
45   :type 'string
46   :group 'riece-rdcc)
47
48 (defcustom riece-rdcc-send-program
49   '("\
50 address = " address "
51 unless address
52   sock = UDPSocket.new
53   sock.connect('164.46.176.4', 7)               # www.unixuser.org/echo
54   address = sock.getsockname[4 .. 7].unpack('CCCC').join('.')
55 end
56 server = TCPServer.new(address, 0)
57 puts(\"#{server.addr[3].split(/\\./).collect{|c| c.to_i}.pack('CCCC').unpack('N')[0]} #{server.addr[1]}\")
58 $stdout.flush
59 session = server.accept
60 if session
61   total = 0
62   File.open(" file ") {|file|
63     while (bytes = file.read(4096))
64       total += bytes.length
65       puts(\"#{total}\")
66       session.write(bytes)
67     end
68   }
69   session.close
70 end
71 ")
72   "Ruby program to send file with DCC."
73   :type 'list
74   :group 'riece-rdcc)
75
76 (defcustom riece-rdcc-decode-address-program
77   '("\
78 puts(\"#{" address " >> 24 & 0xFF}.#{" address " >> 16 & 0xFF}.#{"
79     address " >> 8 & 0xFF}.#{" address " & 0xFF}\")")
80   "Ruby program to numeric IP address."
81   :type 'list
82   :group 'riece-rdcc)
83
84 (defcustom riece-rdcc-save-directory nil
85   "Default directory where received files are saved in."
86   :type 'directory
87   :group 'riece-rdcc)
88
89 (defvar riece-rdcc-requests nil)
90
91 (defvar riece-rdcc-request-user nil)
92 (defvar riece-rdcc-request-file nil)
93 (defvar riece-rdcc-request-size nil)
94
95 (defvar jka-compr-compression-info-list)
96 (defvar jam-zcat-filename-list)
97 (defun riece-rdcc-substitute-variables (program variable value)
98   (setq program (copy-sequence program))
99   (let ((pointer program))
100     (while pointer
101       (setq pointer (memq variable program))
102       (if pointer
103           (setcar pointer value)))
104     program))
105
106 (defun riece-rdcc-server-filter (process input)
107   (save-excursion
108     (set-buffer (process-buffer process))
109     (goto-char (point-max))
110     (insert input)
111     (goto-char (point-min))
112     (while (and (not (eobp))
113                 (looking-at "\\([0-9]+\\)\n"))
114       (message "Sending %s...(%s/%d)"
115                riece-rdcc-request-file
116                (match-string 1) riece-rdcc-request-size)
117       (forward-line))
118     (unless (eobp)
119       (delete-region (point-min) (point)))))
120
121 (defun riece-rdcc-server-sentinel (process status)
122   (with-current-buffer (process-buffer process)
123     (message "Sending %s...done" riece-rdcc-request-file))
124   (kill-buffer (process-buffer process)))
125
126 (defun riece-command-dcc-send (user file)
127   (interactive
128    (let ((completion-ignore-case t))
129      (list (riece-completing-read-identity
130             "User: "
131             (riece-get-users-on-server (riece-current-server-name)))
132            (expand-file-name (read-file-name "File: ")))))
133   (let* ((process-connection-type nil)
134          (process (start-process "DCC" (generate-new-buffer " *DCC*")
135                                  "ruby" "-rsocket")))
136     (process-send-string process
137                          (apply #'concat
138                                 (riece-rdcc-substitute-variables
139                                  (riece-rdcc-substitute-variables
140                                   riece-rdcc-send-program
141                                   'address
142                                   (if riece-rdcc-server-address
143                                       (concat "'" riece-rdcc-server-address
144                                               "'")
145                                     "nil"))
146                                  'file
147                                  (concat "'" file "'"))))
148     (process-send-eof process)
149     (save-excursion
150       (set-buffer (process-buffer process))
151       (while (and (eq (process-status process) 'run)
152                   (progn
153                     (goto-char (point-min))
154                     (not (looking-at "\\([0-9]+\\) \\([0-9]+\\)"))))
155         (accept-process-output process))
156       (if (eq (process-status process) 'run)
157           (let ((address (match-string 1))
158                 (port (match-string 2)))
159             (erase-buffer)
160             (make-local-variable 'riece-rdcc-request-size)
161             (setq riece-rdcc-request-file file
162                   riece-rdcc-request-size (nth 7 (file-attributes file)))
163             (set-buffer-modified-p nil)
164             (set-process-filter process #'riece-rdcc-server-filter)
165             (set-process-sentinel process #'riece-rdcc-server-sentinel)
166             (riece-send-string
167              (format "PRIVMSG %s :\1DCC SEND %s %s %s %d\1\r\n"
168                      (riece-identity-prefix user)
169                      (file-name-nondirectory file)
170                      address port
171                      riece-rdcc-request-size)))))))
172
173 (defun riece-rdcc-filter (process input)
174   (save-excursion
175     (set-buffer (process-buffer process))
176     (goto-char (point-max))
177     (insert input)
178     (message "Receiving %s from %s...(%d/%d)"
179              (file-name-nondirectory riece-rdcc-request-file)
180              riece-rdcc-request-user
181              (1- (point))
182              riece-rdcc-request-size)))
183
184 (defun riece-rdcc-sentinel (process status)
185   (save-excursion
186     (set-buffer (process-buffer process))
187     (unless (= (buffer-size) riece-rdcc-request-size)
188       (error "Premature end of file"))
189     (message "Receiving %s from %s...done"
190              (file-name-nondirectory riece-rdcc-request-file)
191              riece-rdcc-request-user)
192     (let ((coding-system-for-write 'binary)
193           jka-compr-compression-info-list jam-zcat-filename-list)
194       (write-region (point-min) (point-max) riece-rdcc-request-file)))
195   (kill-buffer (process-buffer process)))
196
197 (defun riece-rdcc-decode-address (address)
198   (with-temp-buffer
199     (call-process riece-rdcc-ruby-command nil t nil "-e"
200                   (apply #'concat
201                          (riece-rdcc-substitute-variables
202                           riece-rdcc-decode-address-program
203                           'address
204                           address)))
205     (buffer-substring (point-min) (1- (point-max)))))
206
207 (defun riece-command-dcc-receive (request file)
208   (interactive
209    (progn
210      (unless riece-rdcc-requests
211        (error "No request"))
212      (let* ((request
213              (if (= (length riece-rdcc-requests) 1)
214                  (car riece-rdcc-requests)
215                (with-output-to-temp-buffer "*Help*"
216                  (let ((requests riece-rdcc-requests)
217                        (index 1))
218                    (while requests
219                      (princ (format "%2d: %s %s (%d bytes)\n"
220                                     index
221                                     (car (car requests))
222                                     (nth 1 (car requests))
223                                     (nth 4 (car requests))))
224                      (setq index (1+ index)
225                            requests (cdr requests)))))
226                (let ((number (read-string "Request#: ")))
227                  (unless (string-match "^[0-9]+$" number)
228                    (error "Not a number"))
229                  (if (or (> (setq number (string-to-number number))
230                             (length riece-rdcc-requests))
231                          (< number 1))
232                      (error "Invalid number"))
233                  (nth (1- number) riece-rdcc-requests))))
234             (default-name (expand-file-name
235                            (nth 1 request) (or riece-rdcc-save-directory
236                                                default-directory))))
237        (list request
238              (expand-file-name
239               (read-file-name
240                (concat "Save as (default "
241                        (file-name-nondirectory default-name) ") ")
242                (file-name-directory default-name)
243                default-name))))))
244   (let* (selective-display
245          (coding-system-for-read 'binary)
246          (coding-system-for-write 'binary)
247          (process (open-network-stream
248                    "DCC" (generate-new-buffer " *DCC*")
249                    (riece-rdcc-decode-address (nth 2 request))
250                    (nth 3 request))))
251     (setq riece-rdcc-requests (delq request riece-rdcc-requests))
252     (with-current-buffer (process-buffer process)
253       (if (fboundp 'set-buffer-multibyte)
254           (set-buffer-multibyte nil))
255       (buffer-disable-undo)
256       (erase-buffer)
257       (make-local-variable 'riece-rdcc-request-user)
258       (setq riece-rdcc-request-user (car request))
259       (make-local-variable 'riece-rdcc-request-file)
260       (setq riece-rdcc-request-file file)
261       (make-local-variable 'riece-rdcc-request-size)
262       (setq riece-rdcc-request-size (nth 4 request)))
263     (set-process-filter process #'riece-rdcc-filter)
264     (set-process-sentinel process #'riece-rdcc-sentinel)))
265
266 (defun riece-rdcc-format-size (size)
267   (if (< size 1024)
268       (format "%0.1fB" size)
269     (setq size (/ size 1024.0))
270     (if (< size 1024)
271         (format "%0.1fKB" size)
272       (setq size (/ size 1024.0))
273       (if (< size 1024)
274           (format "%0.1fMB" size)
275         (format "%0.1fGB" (/ size 1024.0))))))
276
277 (defun riece-handle-dcc-request (prefix target message)
278   (let ((case-fold-search t))
279     (when (string-match
280            "SEND \\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\)"
281            message)
282       (let ((file (match-string 1 message))
283             (address (match-string 2 message))
284             (port (string-to-number (match-string 3 message)))
285             (size (string-to-number (match-string 4 message)))
286             (buffer (if (riece-channel-p target)
287                         (riece-channel-buffer (riece-make-identity
288                                                target riece-server-name))))
289             (user (riece-prefix-nickname prefix)))
290         (setq riece-rdcc-requests
291               (cons (list user file address port size)
292                     riece-rdcc-requests))
293         (message "%s"
294                  (substitute-command-keys
295                   (format
296                    "DCC SEND from %s, \\[riece-command-dcc-receive] to receive"
297                    user)))
298         (riece-insert-change buffer (format "DCC SEND from %s: %s (%s)\n"
299                                             user file
300                                             (riece-rdcc-format-size size)))
301         (riece-insert-change
302          (if (and riece-channel-buffer-mode
303                   (not (eq buffer riece-channel-buffer)))
304              (list riece-dialogue-buffer riece-others-buffer)
305            riece-dialogue-buffer)
306          (concat
307           (riece-concat-server-name
308            (format "DCC SEND from %s (%s) to %s: %s (%s)"
309                    user
310                    (riece-strip-user-at-host
311                     (riece-prefix-user-at-host prefix))
312                    (riece-decode-coding-string target)
313                    file
314                    (riece-rdcc-format-size size)))
315           "\n")))
316       t)))
317
318 (defun riece-rdcc-requires ()
319   '(riece-ctcp))
320
321 (defvar riece-dialogue-mode-map)
322 (defun riece-rdcc-insinuate ()
323   (add-to-list 'riece-ctcp-additional-clientinfo "DCC")
324   (add-hook 'riece-ctcp-dcc-request-hook 'riece-handle-dcc-request)
325   (define-key riece-dialogue-mode-map "\C-ds" 'riece-command-dcc-send)
326   (define-key riece-dialogue-mode-map "\C-dr" 'riece-command-dcc-receive))
327
328 (provide 'riece-rdcc)
329
330 ;;; riece-rdcc.el ends here