![Nokia Nokia](http://www.kacke.de/nokia.gif)
Scriptname :
Includes
socke.inc
by Toppi ( http://kacke.de/php_samples/source.php?f=socke.inc http://kacke.de/php_samples/source.php?f=pline.php)
<?
/*============================================================================*/
|
| Little Partyline example based on class socke()
|
| by Toppi
|
| forgett my poor english, if you can !
| and please ....
| if you like my work, respect me and dont remove my Notes :-)
|
| 2004
|
|
|
| Copyright (C) 2001-2004 by Toppi (toppi@kacke.de)
|
| This program is free software
| you can redistribute it and/or modify
| it under the terms of the GNU General Public License as published by
| the Free Software Foundation; either version 2, or (at your option)
| any later version.
|
| This program is distributed in the hope that it will be useful,
| but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
| GNU General Public License for more details.
|
| You should have received a copy of the GNU General Public License
| along with this program; if not, write to the Free Software
| Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
/*============================================================================*/
error_reporting (7);
set_time_limit (0);
ob_implicit_flush ();
/*============================================================================*/
Create Socket
/*============================================================================*/
require ("socke.inc");
if($socket = new socke()){
$io = $socket->listen("",23456);
}else {
echo "Open socket failed! Dying.../n";
exit;
}
/*============================================================================*/
MAIN LOOP
/*============================================================================*/
echo "<b>PLINE SERVER RUNNING at:<br>";
echo "<b>".$socket->myhost.":".$socket->myport."</b><br>";
echo "telnet this :)<br>";
while(1){
if (connection_status()!=0){
$socket->close();
die;
}
Echo "/n";
usleep(200);
foreach($socket->can_read() as $sock){
if($io == $sock){
if($mysock = $socket->accept()){
//Neue Connex
login($mysock);
}
} else {
//Anyone had something to say
$data = $socket->read($sock);
//Client pinged out
if($data === false){
logout($sock,"Connection reset by Beer");
continue;
}
//Only Enter Hit
if(!$data || $data == "/n" || $data == "/r/n"){
continue;
}
interact($sock, $data);
}
}
}
/*============================================================================*/
Functions
/*============================================================================*/
function interact ($mysock, $data){
GLOBAL $socket;
$cmd = explode(" ",$data);
switch(strtoupper($cmd[0])){
case "/QUIT":
logout($mysock);
return;
case "/NICK":
set_nickname($mysock,$cmd[1]);
return;
case "/WHOIS":
whois($mysock,$cmd[1]);
return;
case "/WHO":
who($mysock);
return;
case "/SHUTDOWN":
shutdown();
break;
}
if(!$socket->get_socketinfo($mysock,"nick")){
write_me($mysock,"*** Enter your Nickanem first !");
return;
}
write_all($mysock,"$data");
return;
}
function whois($mysock, $nick=""){
GLOBAL $socket;
if(!$nick){
write_me($mysock,"You need help !Usage: whois <nick>!");
return;
}
$sockinfos = $socket->get_socketinfo();
$nick = strtoupper($nick);
foreach($sockinfos as $sock){
if(strtoupper($sock['nick']) == $nick){
write_me($mysock,"Whois: /"$nick/"");
write_me($mysock,"Host: /"$sock[host]/"");
write_me($mysock,"IP: /"$sock[ip]/"");
write_me($mysock,"Joined: /"".date("Y-m-d H:i:s",$sock['started'])."/"");
$last = time()-$sock['last'];
write_me($mysock,"Last spoke: /"$last/" seconds ago");
return;
}
}
write_me($mysock,"Nick /"$nick/" not here !");
return;
}
function who($mysock){
GLOBAL $socket;
write_me($mysock,"Who is here ?");
$sockinfos = $socket->get_socketinfo();
foreach($sockinfos as $sock){
write_me($mysock," $sock[nick]");
}
return;
}
function set_nickname($mysock, $nick=""){
GLOBAL $socket;
if(!$nick){
write_me($mysock,"Nick /"$nick/" not valid !");
return;
}
$sockinfos = $socket->get_socketinfo();
$nick1 = strtoupper($nick);
foreach($sockinfos as $sock){
if(strtoupper($sock['nick']) == $nick1){
write_me($mysock,"Nick /"$nick/" already in use !");
return;
}
}
$socket->set_socketinfo($mysock,"nick",$nick);
write_me($mysock,"Your now known as /"$nick/"!");
return;
}
function write_all($mysock, $message){
//
GLOBAL $socket;
if(is_resource($mysock)){
$nick = $socket->get_socketinfo($mysock,"nick");
}else{
$nick = $mysock;
}
foreach($socket->clients as $client){
$socket->write ($client, date("H:i")."<$nick> $message/n");
}
return;
}
function write_all_but_me($mysock, $message){
GLOBAL $socket;
$nick = $socket->get_socketinfo($mysock,"nick");
foreach($socket->clients as $client){
if($client != $mysock){
$socket->write ($client, date("H:i")."<$nick> $message/n");
}
}
return;
}
function write_me($mysock, $message){
//
GLOBAL $socket;
$socket->write ($mysock, date("H:i")." $message/n");
return;
}
function login ($mysock){
GLOBAL $socket;
write_me($mysock, "Hello Stupid ! Welcome to PLINE/n");
write_me($mysock, "Commands beginning with /");
write_me($mysock, "I know: /quit, /nick, /shutdown, /who, /whois /n");
write_me($mysock, "Enter your Nickname !");
return;
}
function logout ($sock, $message = "" ){
GLOBAL $socket;
write_all_but_me($sock,"*** Left the partyline");
$socket->close($sock);
return;
}
function shutdown ($message = "Server goes shutdown now" ){
GLOBAL $socket;
echo "$message<br>";
write_all("SYSTEM", "*** $message");
$socket->close();
exit;
}
?>