Skip to content

Commit

Permalink
Added proper empty command handling, new math module
Browse files Browse the repository at this point in the history
  • Loading branch information
krakerag committed Dec 1, 2010
1 parent 19c6148 commit 95f2223
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 12 deletions.
29 changes: 23 additions & 6 deletions class.ircBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ function __construct($config, $adminList) {
$this->config = $config;
$this->admins = $adminList;
if($this->config['serverPassword'] == "") $this->config['serverPassword'] = "NOPASS";
$this->log("ircBot - starting up...");
$this->log("ircBot - starting up...\n");

# Load modules
require_once("class.ircModule.php");
$this->log("[INIT]: ".print_r($config,true));
$this->log("[INIT]: Loading modules...");
foreach($config['modules'] as $module) {
$this->loadModule($module);
Expand Down Expand Up @@ -68,23 +67,25 @@ private function controller() {
if(strpos($this->inbound, $this->config['server']." 376") || strpos($this->inbound, $this->config['server']." 422")) {
# Join channel then...
$this->sendCommand("JOIN ".$this->config['destinationChannel']);
$this->log("[INIT]: Joined destination channel");
}
# If successfully joined the channel, mark bot as ready (names list message id 353)
if(strpos($this->inbound, $this->config['server']." 353")) {
$this->ready = true;
$this->log("[INIT]: Ready for commands!");
}

if($this->ready) {
# Parse the inbound message and scan for a command
$this->parseMessage();
if(strlen($this->lastMessage['command'])) {
if(strlen($this->lastMessage['command']) > 0) {
# See if this command can be found in the command list
# Command list is established by the loaded modules
foreach($this->modules as $moduleName => $moduleObj) {
if($moduleObj->findCommand($this->lastMessage['command'])) {
# If we've found a matching command, fire it up with the arguments passed over
$this->log(" -> Found command '".$this->lastMessage['command']."' in module '".$moduleName."' called by user: '".$this->lastMessage['nickname']."'");
$reply = $this->modules[$moduleName]->launch($this->lastMessage['command'], $this->lastMessage['args']);
$reply = $this->modules[$moduleName]->launch($this->lastMessage['command'], $this->lastMessage);
if(strlen($reply) > 0) {
$this->sendMessage($reply);
}
Expand Down Expand Up @@ -142,6 +143,18 @@ private function parseMessage() {
);
}
}

if(substr($this->lastMessage['chatter'],0,1) == '%') {
# Correctly change up the parser to handle a command with no args
$chatter = explode(" ", $this->lastMessage['chatter']);
$this->lastMessage['command'] = ltrim($chatter[0],'%');
$chatter = array_shift($chatter);
if(is_array($chatter) && count($chatter) > 0) {
$this->lastMessage['chatter'] = implode(" ", $chatter);
} else {
$this->lastMessage['chatter'] = "";
}
}
}

private function loadModule($module) {
Expand Down Expand Up @@ -182,15 +195,19 @@ private function connectToServer($config) {
private function getTransmission() {
$this->inbound = fgets($this->socket, 1024);
# Log the inbound message
$this->log("[RECV]: ".rtrim($this->inbound,"\r\n"));
if($this->config['logging'] == 2) {
$this->log("[RECV]: ".rtrim($this->inbound,"\r\n"));
}
}

/**
* Send a command to the remote server
*/
private function sendCommand($command) {
# Log the outbound message
$this->log("[SEND]: ".rtrim($command,"\r\n"));
if($this->config['logging'] == 2) {
$this->log("[SEND]: ".rtrim($command,"\r\n"));
}
if(substr($command,-4) != "\n\r") {
$command = $command . "\n\r";
}
Expand Down
14 changes: 12 additions & 2 deletions class.ircModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,19 @@ function findCommand($requestedCommand) {
/**
* Launch the requested command with the provided args
*/
function launch($command, $args) {
function launch($command, $parsedArgs) {
if(method_exists($this, $command)) {
return $this->$command($args);
return $this->$command($parsedArgs);
}
}

/**
* Scan the arguments to determine a help string
*/
function help($parsedArgs) {
if(strtolower(substr($parsedArgs['args'], 0, 4)) == 'help' || strlen($parsedArgs['args']) == 0) {
return true;
}
return false;
}
}
3 changes: 2 additions & 1 deletion init.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
'username' => "trivbot", # Username for the bot to have
'realname' => "PHP Trivia Bot", # Realname for bot to have (IRC RFC2812 3.1.3)
'timeout' => 10, # Timeout in seconds for initializing connection
'logging' => 1, # 2 = all messages, 1 = only commands etc.
'modules' => array( # List of modules to load
'parrot',
'parrot','math'
)
);

Expand Down
64 changes: 64 additions & 0 deletions math/module.math.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/**
* Module: math
* Simple math commands caught by a simple %eq command modifier.
* I'd like to find a way to just have %= be the command, but i don't know
* if php handles something like 'function =()' :)
* We can elaborate upon it later on for more functionality
*
*/

class math extends ircModule {

/**
* Constructor to establish command list (required)
*/
function __construct() {
$this->commands = array(
'eq',
);
}

/**
* NOTE:
* $parsedArgs should be of the format:
* $parsedArgs = array(
'nickname' => user's nick who called command
'realname' => user's realname who called command
'hostname' => user's hostmask as known by server
'command' => the command (same as function name)
'args' => arguments post command
'chatter' => should be empty!
/**
* Basic math based on the args and the operator in the middle
*/
function eq($parsedArgs) {
if($this->help($parsedArgs)) {
# Return some help text
return $parsedArgs['nickname'].": Use %eq to generate math results. i.e. '%eq 7 * 6' or '%eq 81 / 7' etc.";
}
$return = $this->_matheval($parsedArgs['args']);
return $parsedArgs['nickname'].": ".$return;
}

/**
* Operational function to eval a math string
* Shamelessly pinched from: http://www.php.net/manual/en/function.eval.php#92603
*/
function _matheval($equation) {

$equation = preg_replace("/[^0-9+\-.*\/()%]/","",$equation);
$equation = preg_replace("/([+-])([0-9]{1})(%)/","*(1\$1.0\$2)",$equation);
$equation = preg_replace("/([+-])([0-9]+)(%)/","*(1\$1.\$2)",$equation);
$equation = preg_replace("/([0-9]+)(%)/",".\$1",$equation);

if($equation == "") {
$return = 0;
} else {
eval("\$return=" . $equation . ";" );
}
return $return;
}
}
36 changes: 33 additions & 3 deletions parrot/module.parrot.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,44 @@ class parrot extends ircModule {
*/
function __construct() {
$this->commands = array(
'repeat'
'repeat', 'inverse'
);
}

/**
* NOTE:
* $parsedArgs should be of the format:
* $parsedArgs = array(
'nickname' => user's nick who called command
'realname' => user's realname who called command
'hostname' => user's hostmask as known by server
'command' => the command (same as function name)
'args' => arguments post command
'chatter' => should be empty!
/**
* Just repeat what was said to the bot with some funky rubbish
*/
function repeat($args) {
return $args." SQUAWK!!";
function repeat($parsedArgs) {
if($this->help($parsedArgs)) {
# Return some help text
return $parsedArgs['nickname'].": Use %repeat to just echo out whatever text you provide to the function.";
}
return $parsedArgs['nickname'].": ".$parsedArgs['args']." SQUAWK!!";
}

/**
* Inverse the chatter to demonstrate that we just don't have to return
* normal text. Basically any module can work on a command and args and do
* processing, and return a textual result. This result can be formatted
* as well using IRC formatting if required
*/
function inverse($parsedArgs) {
if($this->help($parsedArgs)) {
# Return some help text
return $parsedArgs['nickname'].": Use %inverse to echo out the reverse of what text you have provided.";
}
return $parsedArgs['nickname'].": ".strrev($parsedArgs['args']);
}

}

0 comments on commit 95f2223

Please sign in to comment.