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