This will take any argument that the Google Calculator would take.
Ex:
simple math: /calc 5+8
conversions: /calc 28 C in F
constants: /calc Mass of Sun
currency: /calc 5 CAD in Euros
Create a file called calc.class.php and save the following in your chat/src/ directory. Copy the following into the file.
- Code: Select all
class Calculator {
var $command;
function check($text){
$this->errors = array();
$this->command= '';
$this->text = $text;
$google = file_get_contents('http://www.google.com/search?q=' . urlencode($this->text));
$pos = strpos($google,'calc_img.gif');
if(!$pos)
return false;
preg_match_all('{<b>.+= (.+?)</b>}', substr($google,$pos), $matches);
$this->command['calc'] = strip_tags($matches[0][0], '<sup><sub>');
return true;
}
function printCalc(){
$parts = explode('=',$this->command['calc']);
$result = $parts[0].' = <strong>'.$parts[1].'</strong>';
return $result;
}
function error_get(){
if(!count($this->errors)){
return '';
} else {
return join("<br />n", $this->errors);
}
}
}
On the page that calls the chat, before $chat->printChat(); copy:
- Code: Select all
class pfcCommand_calc extends pfcCommand
{
function run(&$xml_reponse, $clientid, $msg)
{
$c =& $this->c;
$nick = $c->nick;
$container =& $c->getContainerInstance();
$text = trim($msg);
// Call item
require_once 'chat/src/calc.class.php'; // EDIT THIS LINE TO SUIT YOUR SETUP
$calculate = new Calculator();
if (!$calculate->check($text))
{
$result = $calculate->error_get();
$cmd =& pfcCommand::Factory("error", $c);
$cmd->run($xml_reponse, $clientid, "Unable to calculate " . $result);
}
else
{
$result = $calculate->printCalc();
$container->writeMsg($nick, $result);
}
if ($c->debug) pxlog("Cmd_item[".$c->sessionid."]: msg=".$result, "chat", $c->getId());
}
}
Be sure to edit:
require_once 'chat/src/calc.class.php';
to match the path to class file.