diff client/client.php @ 0:dd81c38b513a

Initial commit
author Ivo Smits <Ivo@UCIS.nl>
date Mon, 28 Feb 2011 00:49:07 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/client/client.php	Mon Feb 28 00:49:07 2011 +0100
@@ -0,0 +1,82 @@
+<?php
+/* Copyright 2010 Ivo Smits <Ivo@UCIS.nl>. All rights reserved.
+   Redistribution and use in source and binary forms, with or without modification, are
+   permitted provided that the following conditions are met:
+
+   1. Redistributions of source code must retain the above copyright notice, this list of
+      conditions and the following disclaimer.
+
+   2. Redistributions in binary form must reproduce the above copyright notice, this list
+      of conditions and the following disclaimer in the documentation and/or other materials
+      provided with the distribution.
+
+   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
+   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+   FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
+   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+   ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+   The views and conclusions contained in the software and documentation are those of the
+   authors and should not be interpreted as representing official policies, either expressed
+   or implied, of Ivo Smits.*/
+
+$server = '127.0.0.1:15387';
+$channel = 'chat/anonet';
+$name = 'user';
+
+$socket = stream_socket_client('tcp://'.$server) or die("Could not create socket\n");
+$buffer = '';
+srand();
+
+while (TRUE) {
+	$selread = array($socket, STDIN);
+	$selerror = array($socket);
+	$selwrite = NULL;
+	$count = stream_select(&$selread, &$selwrite, &$selerror, 10);
+	if (!$count) continue;
+	if (count($selerror)) die("Error: select error\n");
+	if (count($selread)) {
+		if (in_array($socket, $selread)) {
+			$msg = fread($socket, 1024);
+			if (!strlen($msg)) die("Error: end of file\n");
+			$buffer .= $msg;
+			while (strlen($buffer) > 2) {
+				$len = ord($buffer[0]) * 256 + ord($buffer[1]);
+				if ($len <= 0 || $len > 1024) die("Error: protocol error\n");
+				if (strlen($buffer) < 2 + $len) break;
+				$msg = substr($buffer, 2, $len);
+				$buffer = substr($buffer, 2 + $len);
+				$parts = explode("\0", $msg);
+				$ret = array();
+				for ($i = 0; $i < count($parts)-1; $i += 2) $ret[$parts[$i]] = $parts[$i+1];
+				if (!isset($ret['USR']) || !isset($ret['MSG']) || (isset($ret['CMD']) && $ret['CMD'] != 'MSG')) continue;
+				print(preg_replace('/[\x00\x07\x1B\x10-\x13]/', '', '<'.$ret['USR'].'> '.$ret['MSG'])."\n");
+			}
+		}
+		if (in_array(STDIN, $selread)) {
+			$msg = rtrim(fgets(STDIN), "\r\n");
+			udpmsg_send(array('DUMMY' => rand(10000, 99999), 'CMD' => 'MSG', 'CHN' => $channel, 'USR' => $name, 'MSG' => $msg));
+		}
+	}
+	unset($msg, $len, $parts, $ret);
+}
+
+function udpmsg_send($arr) {
+	global $socket;
+	$tmp = array();
+	foreach ($arr as $key => $value) { $tmp[] = $key; $tmp[] = $value; }
+	$tmp[] = '';
+	$msg = implode("\0", $tmp);
+	$len = strlen($msg);
+	if ($len > 1024) {
+		print("Error: message too long!\n");
+		return FALSE;
+	}
+	$lens = chr(floor($len / 256)).chr($len % 256);
+	fwrite($socket, $lens.$msg);
+}
+