0
|
1 <?php |
|
2 /* Copyright 2010 Ivo Smits <Ivo@UCIS.nl>. All rights reserved. |
|
3 Redistribution and use in source and binary forms, with or without modification, are |
|
4 permitted provided that the following conditions are met: |
|
5 |
|
6 1. Redistributions of source code must retain the above copyright notice, this list of |
|
7 conditions and the following disclaimer. |
|
8 |
|
9 2. Redistributions in binary form must reproduce the above copyright notice, this list |
|
10 of conditions and the following disclaimer in the documentation and/or other materials |
|
11 provided with the distribution. |
|
12 |
|
13 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
|
14 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
|
15 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR |
|
16 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
17 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
18 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
|
19 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
20 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|
21 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
22 |
|
23 The views and conclusions contained in the software and documentation are those of the |
|
24 authors and should not be interpreted as representing official policies, either expressed |
|
25 or implied, of Ivo Smits.*/ |
|
26 |
|
27 $server = '127.0.0.1:15387'; |
|
28 $channel = 'chat/anonet'; |
|
29 $name = 'user'; |
|
30 |
|
31 $socket = stream_socket_client('tcp://'.$server) or die("Could not create socket\n"); |
|
32 $buffer = ''; |
|
33 srand(); |
|
34 |
|
35 while (TRUE) { |
|
36 $selread = array($socket, STDIN); |
|
37 $selerror = array($socket); |
|
38 $selwrite = NULL; |
|
39 $count = stream_select(&$selread, &$selwrite, &$selerror, 10); |
|
40 if (!$count) continue; |
|
41 if (count($selerror)) die("Error: select error\n"); |
|
42 if (count($selread)) { |
|
43 if (in_array($socket, $selread)) { |
|
44 $msg = fread($socket, 1024); |
|
45 if (!strlen($msg)) die("Error: end of file\n"); |
|
46 $buffer .= $msg; |
|
47 while (strlen($buffer) > 2) { |
|
48 $len = ord($buffer[0]) * 256 + ord($buffer[1]); |
|
49 if ($len <= 0 || $len > 1024) die("Error: protocol error\n"); |
|
50 if (strlen($buffer) < 2 + $len) break; |
|
51 $msg = substr($buffer, 2, $len); |
|
52 $buffer = substr($buffer, 2 + $len); |
|
53 $parts = explode("\0", $msg); |
|
54 $ret = array(); |
|
55 for ($i = 0; $i < count($parts)-1; $i += 2) $ret[$parts[$i]] = $parts[$i+1]; |
|
56 if (!isset($ret['USR']) || !isset($ret['MSG']) || (isset($ret['CMD']) && $ret['CMD'] != 'MSG')) continue; |
|
57 print(preg_replace('/[\x00\x07\x1B\x10-\x13]/', '', '<'.$ret['USR'].'> '.$ret['MSG'])."\n"); |
|
58 } |
|
59 } |
|
60 if (in_array(STDIN, $selread)) { |
|
61 $msg = rtrim(fgets(STDIN), "\r\n"); |
|
62 udpmsg_send(array('DUMMY' => rand(10000, 99999), 'CMD' => 'MSG', 'CHN' => $channel, 'USR' => $name, 'MSG' => $msg)); |
|
63 } |
|
64 } |
|
65 unset($msg, $len, $parts, $ret); |
|
66 } |
|
67 |
|
68 function udpmsg_send($arr) { |
|
69 global $socket; |
|
70 $tmp = array(); |
|
71 foreach ($arr as $key => $value) { $tmp[] = $key; $tmp[] = $value; } |
|
72 $tmp[] = ''; |
|
73 $msg = implode("\0", $tmp); |
|
74 $len = strlen($msg); |
|
75 if ($len > 1024) { |
|
76 print("Error: message too long!\n"); |
|
77 return FALSE; |
|
78 } |
|
79 $lens = chr(floor($len / 256)).chr($len % 256); |
|
80 fwrite($socket, $lens.$msg); |
|
81 } |
|
82 |