All pastes #28126 Raw Edit

Itrebal

public php v1 · immutable
#28126 ·published 2005-11-09 12:54 UTC
rendered paste body
<?/*                       **********************************************************************************Itrebal Creations PHP Server Class                                             **Copyright (C) 2005  Graham Christensen                                         **Email itrebal@gmail.com IRC: itrebal@freenode.net AIM: itrebal                 **Website: www.itrebal.com                                                       **                                                                               **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                 **of the License, or 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.***********************************************************************************/error_reporting('2047');ini_set('display_errors', '1');//DO NOT EDIT THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOINGinclude('srv.cls.php');// Settings$srv = new server('192.168.1.3',9000,50);$srv->hello_msg[] = 'Functions:';$srv->hello_msg[] = '#nick <nickname>';$srv->hello_msg[] = '#list';function evaluate($uid){  global $srv;  $allow_reset = TRUE;  $input = $srv->get_input($uid);  if(strncmp($input,'#',1)) {    if(get_nick($uid) !=''){      for($i=0;$i<=$srv->active_clients;$i++){	//if(get_nick($i) != ''){	  $srv->send($i,date('Y-d-m H:i:s').' ['.get_nick($uid).']: '.$srv->get_input($uid).$srv->crlf);	  //}      }    }else{      $srv->send($uid,'ERROR: You are not logged in. Use [#nick <nickname>] to join the chat.'.$srv->crlf);    }    $srv->reset_input($uid);  }else{    $params  = substr($input,5);    $matches  = array();    $function = substr($input,0,5);    switch($function){    case '#nick':      if(( $params == '') || preg_match('/^ +$/',$params)){	// Wrong syntax	$srv->send($uid,'ERROR: Parameter missing'.$srv->crlf);      }elseif(preg_match('/^ +([^ ]+)$/', $params, $matches)){	$nick_old = get_nick($uid);	$nick_new = preg_replace('/[,\\[\\]]/','',$matches[1]);	if($nick_new == '' ){	  $srv->send($uid,'ERROR: Invalid nickname'.$srv->crlf);       	}else{	  // Nick already used?	  $found = FALSE;	  for($i = 0;$i <= $srv->active_clients;$i++){	    if($i == $uid) continue;	    if(!strcasecmp(get_nick($i),$nick_new)){	      $found = TRUE;	      break; // Stop searching	    }	  }	  if($found){	    // Nick already used	    $srv->send($uid,'ERROR: Nickname already in use'.$srv->crlf);	  }else{	    set_nick($uid, $nick_new);	    // Notify others	    if($nick_old == ''){	      // User has just logged in	      for($i=0;$i <= $srv->active_clients;$i++){		if($i == $uid) continue;		if(get_nick($i) != ''){ //To logged-in only		  $srv->send($i,'['.$nick_new.'] joined the chat.'.$srv->crlf);		}	      }	  	      $srv->send($uid,'You joined the chat as ['.$nick_new.'].'.$srv->crlf);	      echo '['.$srv->get_addr($uid).'] is ['.$nick_new."]\n";	    }else{	      // User changed its nick	      for($i=0; $i <= $srv->active_clients; $i++){		if($i == $uid) continue;		if(get_nick($i) != ''){	// To logged-in only		  $srv->send($i,'['.$nick_old.'] is now ['.$nick_new.']'.$srv->crlf);		}	      }	      $srv->send($uid, 'Your nick was changed to ['.$nick_new.']'.$srv->crlf);	      echo '['.$srv->get_addr($uid).'] is now [' . $nick_new . ']\n';	    }	  }	}      }else{	// Wrong syntax	$srv->send($uid, 'ERROR: A nickname must NOT contain spaces.'.$srv->crlf);      }      break;    case '#list':      if(($params == '') || preg_match('/^ +$/', $params)){	if(get_nick($uid)==''){	  $srv->send($uid, 'ERROR: You are not logged in. Use [#nick <nickname>] to join the chat.'.$str->crlf);	}else{	  // List users	  $list	 = "";	  $users = 0;	  for($i=0;$i <= $srv->active_clients;$i++ ){	    $nick = get_nick($i);	    if($nick !=''){	      $list .= (($users++ > 0) ? ', ' : '' ).'['.$nick.']';	    }	  }	  if($list!=''){	    $srv->send($uid, $users.' user '.(($users == 1) ? '' : 's').':'. $list.$srv->crlf);	  }	}      }else{	// Wrong syntax	$srv->send( $uid,'ERROR: [#list] takes no parameters.'.$srv->crlf);      }      break;    case '#quit':      if(($params == '') || preg_match('/^ +$/', $params)){	// Notify others	for($i=0;$i <= $srv->active_clients;$i++){	  if($i == $uid) continue;	  if(get_nick($i) != ''){// To logged-in only	    $srv->send($i,'['.get_nick($uid).'] left the chat'.$srv->crlf);	  }	}		// Say goodbye	$srv->send($uid, "You left the chat".$srv->crlf);		// Kick client	$srv->kick($uid);		// DO NOT RESET BUFFER !!!	$allow_reset = FALSE;      }else{	// Wrong syntax	$srv->send($uid, 'ERROR: [#quit] takes no parameters.'.$srv->crlf);      }      break;    default:      $srv->send($uid,'ERROR: Unknown command'.$srv->crlf);      break;    }    // Reset buffer    if($allow_reset) $srv->reset_input($uid);  }}function set_nick($i, $name){  global $srv;  $srv->clients[$i]['nick'] = $name;}function get_nick($i){  global $srv;  return isset($srv->clients[$i]['nick']) ? $srv->clients[$i]['nick'] : '';}$srv->run();?>