...

View Full Version : Issues with magic __call function



Dubz
04-14-2012, 09:58 PM
I have a few classes that i built up and put together for a chat bot but need some assistance with the __call function. When a function is called for the class, it's missing a salt so it isn't called straight away. The __cal function runs a check and then calls the function with the salt added. Here's the layout of the code for a better understanding:

Command class:


<?php
$command = new CommandClass();
class CommandClass
{
public $aliasArray; //Used to store command aliases
public $customArray; //Used to store custom commands

public function __call($funct, $args)
{
global $bot, $data;
if(method_exists($this, $funct.'_Command'))#
{
$minRank = EVERYONE;
switch($funct)
{
case 'c':
case 'c2':
$minRank = $bot->minRank['censor'];
break;
case 'sup':
$minRank = SUPPORT;
break;
case 'debug':
case 'dev':
$minRank = DEVELOPER;
break;
default:
$minRank = $bot->minRank[$funct];
}
if(!$data->minRank($args[0]['u'], $minRank))
$bot->respondP('You are too low of a rank for this command!', $t, $p['u']);
else
{
call_user_func_array($funct.'_Command', $args);#
$bot->last['command'][(($args['t'] == 0) ? 'main' : 'private')] = implode(' ', $m);
}
}else
throw new Exception('Unknown function \''.$funct.'\'');
/*
if(method_exists($this, $funct))
{
$minRank = EVERYONE;
switch($funct)
{
case 'c':
case 'c2':
$minRank = $bot->minRank['censor'];
break;
case 'sup':
$minRank = SUPPORT;
break;
case 'debug':
case 'dev':
$minRank = DEVELOPER;
break;
default:
$minRank = $bot->minRank[$funct];
}
if(!$data->minRank($args['packet']['u'], $minRank))
return;
else
$this->$funct($args);
}
else
throw new exception('Unknown command "'.$funct.'"');*/
}
function say_Command($p, $m, $t)
{
global $bot, $data;
if(!@$m[1]) return;
if($m[1]{0}=='/')
return $bot->respond('Not doing that!', $t, $p['u']);
switch($t)
{
case 0: //Main
$bot->sendMessage($m[1]);
break;
case 1: //PC
case 2: //PM
break;
}
}
}
?>


Handler class:


<?php
$handle = new HandleClass();
class HandleClass
{
public function __call($funct, $p)
{
global $bot, $packet;
if(method_exists($this, $funct.'Handler'))#
{
if(!key_exists('u', $p)) return; //$p['u'] = $p['d'];
$p['u'] = $packet->parseU($p['u']);
call_user_func_array($funct.'Handler', $p);#
}
else
{
//$bot->sendMessage($funct." was called but doesn't exist");
}
}
function apHandler($p) #Assign Power
{
global $bot;
switch($p['r'])
{
case '0':
/* Unassigned ok */
break;
case '1':
/* Assigned ok */
break;
case '2':
$bot->powerError = "Power unassign failed (power isn't assigned)";
break;
case '3':
$bot->powerError = "Power assign failed (not enough free ones)";
break;
case '9':
$bot->powerError = "Invalid power name or not a group power!";
break;
default:
$bot->powerError = false;
}
}
function doneHandler($p) #Done
{
global $bot, $data;
$bot->chat['name'] = $data->id2room($bot->chat['id']);
$bot->done = true;
}
function iHandler($p) #Chat Loaded
{
global $bot, $command, $con, $data;
$bot->chat['name'] = $data->id2room($bot->chat['id']);

$p['b'] = explode(';', $p['b']);//Split the packet by the semicolon
/*
$p['b']
[0] = Background URL
[1] = Secondary Group Name
[2] = Secondary Group ID
[3] = Chat Language
[4] = Radio
[5] = Button Color
*/
$bot->chat['background'] = str_replace(' ','%20',$p['b'][0]);
$bot->chat['radio'] = str_replace('=','',$p['b'][4]);
$bot->chat['color'] = str_replace(array('=', '#'),'',$p['b'][5]);

#Startup message
$startMessages = array(
0 => array(
'Hello!',
'Hello! DuBot here to serve in the chatbox\'s military!',
'Howdy!'
),
1 => array(
'Hello! I\'m sure '.$bot->chat['name'].' will do well with species like me.',
'You have done well in choosing your shield, SHIELDS UP CAPTAIN!',
'We make sure DuBot is upgraded to meet modern xat standards and update when stuff come up on xat.'
),
2 => array(
'I will do as you please, master.',
'I will not fail.',
'I will fail! (smirk) I\'m kidding, no DuBot will ever fail!'
),
3 => array(
//'Did you know about the new command assists? Try '.$commandCode.'assist cmd [your commandCode]',
'Did you know about group pc? Try '.$command->code.'group '.$command->code.'join [group] then type '.$command->code.'group [message]',
'You can now see which groups are promoted. Type '.$command->code.'chat promoted to see all promoted groups.'
),
4 => array(
0
)
);
if($bot->switch['startMessage'])
$bot->sendMessage($startMessages[rand(0,2)][rand(0,2)].' Command Code: '.$command->code);
}
function lHandler($p) #User left
{
global $bot, $data;
#Remove from cache
@$bot->rankCount[$data->rank2string($data->f2rank($bot->userCache[$p['u']]['f']))]--;
unset($bot->userCache[$p['u']]);
}
function mHandler($p) #Message
{
global $bot, $command, $packet;
if($p['u'] == 320467393) $bot->sendMessage('mHandler recieved');
if(!key_exists('t', $p)) return;
if(isset($bot->done))
{
switch(substr($p['t'], 0, 2))
{
#Deleted Message
case '/d':
break;
#Scroll Changed
case '/s':
if($bot->chat['scroll']['lock'] && $bot->chat['scroll']['message']!==false)
$bot->sendMessage('/s'.$bot->chat['scroll']['message']);
break;
#User Kicked
case '/k':
break;
#User Banned
case '/g':
$bot->last['ban'] = $packet->parseU($p['u']);
break;
#User Unbanned
case '/u':
break;
}
if(isset($p['p']))
{
#Rank Change
switch($p['p'])
{
#Guest
case 'r':
break;
#Member
case 'e':
break;
#Moderator
case 'm':
break;
#Owner
case 'M':
break;
}
}
#Link in message
if(key_exists('l', $p))
{
//Do nothing yet
}
}
if(key_exists('i', $p))
if($p['i'] > 0)
{
$bot->chat['messages'][] = $p['i'];
return;
}
@$bot->chat['messages'][] = $bot->chat['messages'][count($bot->chat['messages'])-1]+1;
$p['t'] = htmlspecialchars_decode($p['t']);
if(@$p['t']{0}!=$command->code)
return;
$m = explode(' ', substr($p['t'], 1), 2);
$command->{strtolower($m[0])}($p, $m, 0);
}
function pHandler($p) #Message
{
global $bot, $command, $packet;
$p['t'] = htmlspecialchars_decode($p['t']);
if($p['t']{0}!=$command->code)
{
#AI
if(key_exists('s', $p)){}
}
}
function uHandler($p) #User Joined
{
global $bot, $command, $con, $data;
$name = explode('##', $p['n'], 1);
#Add to user cache
$bot->userCache[$p['u']]['id'] = $p['u'];
$bot->userCache[$p['u']]['user'] = (key_exists('N', $p)) ? $p['N'] : false;
$bot->userCache[$p['u']]['name'] = $name[0];
$bot->userCache[$p['u']]['avatar'] = $p['a'];
$bot->userCache[$p['u']]['home'] = $p['h'];
$bot->userCache[$p['u']]['status'] = (!empty($name[1])) ? $name[1] : false;
$bot->userCache[$p['u']]['f'] = @$p['f'];
$bot->userCache[$p['u']]['d2'] = @$p['d2'];
$bot->userCache[$p['u']]['rank'] = $data->f2rank(@$p['f']);
if(key_exists($p['u'], $bot->accessList))
$bot->userCache[$p['u']]['rank'] = $bot->accessList[$p['u']];
@$bot->rankCount[$data->rank2string($data->f2rank(@$p['f']))]++;

$ua = $data->getUserArray($p['u']);
$room = $bot->chat['id'];
$rank = $data->rank2string($data->f2rank($ua['f']));
$con->sql->replace('usercache', array(
'id' => $p['u'],
'user' => $bot->userCache[$p['u']]['user'],
'name' => $name[0],
'avatar' => $p['a'],
'home' => $p['h'],
'status' => (!empty($name[1])) ? $name[1] : false,
'room' => $room,
'rank' => $rank,
'mate' => $bot->userCache[$p['u']]['d2']
)
);
print "\r\n\r\nAdded usercache";
#Rank
if(key_exists($p['u'], $bot->userList))
{
if($rank != $bot->userList[$p['u']])
{
if($rank == 'banned')//Unban if banned
$bot->unban($p['u']);
$bot->$bot->userArray[$p['u']]($p['u']);//Make rank
//return;//Try to avoid welcom flooding
}
}
print "\r\n\r\nSending welcome message...";
#Welcome message
if(@$p['s']!='1')
{
$resp = array(
'[name]' => $name[0],
'[status]' => $name[1],
'[user]' => $ua['user'],
'[id]' => $p['u'],
'[chat]' => $bot->chat['name'],
'[cc]' => $command->code,
'[ucount]' => count($bot->userCache)
);
$welcomeMessage = $bot->welcomeMessage;
foreach($resp as $k => $v)
{
$welcomeMessage = str_replace(strtolower($k), $v, $welcomeMessage);
}
$bot->sendPM($p['u'], $bot->welcomeMessage);
}
}
function zHandler($p) #Tickle / cross chat message
{
global $bot, $command;
#Tickle Handle
if($p['t']=='/l')
{
if($bot->switch['showChat'])
$bot->tickle($p['u'], true);
else
$bot->tickle($p['u'], false);
}
#Commands
if($p['t']{0}!=!$command->code) return;
$m = explode(' ', substr($p['t'], 1), 2);
$command->{strtolower($m[0])}($p, $m, (key_exists('s', $p) ? 1 : 2));

}
#Handled in packet handle function
function gpHandler($p)
{
}
function qHandler($p)
{
}
}
?>


The lines that I need assistance with are the two that end with a # in the __call class.
The commands are called in the mHandler of the handler class (If needed)

Also, the handlers are called as followed:


//type = the letter or two that comes up before Handler in each function of the handler class
//Ex: a would be aHandler
//p = packet recieved
$handler->{$type}($p);



Fixed it after playing around
used method_exist and then took the array sent in the second argument of the __call function and put the values back to their normal variable and it worked out



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum