All pastes #34932 Raw Edit

itrebal

public text v1 · immutable
#34932 ·published 2005-12-26 21:12 UTC
rendered paste body
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);
  }
}