From 10799b051654c037ad614d2acd7bf6ad60608ba7 Mon Sep 17 00:00:00 2001 From: Brandon Date: Thu, 3 Nov 2016 20:31:09 -0500 Subject: [PATCH 1/2] Add timer based plugins --- IRC.php | 156 +++++++++++++++++++++++++++++------------------------ Misc.php | 14 +++++ Timer.php | 36 +++++++++++++ Timers.php | 22 ++++++++ utabot.php | 2 + 5 files changed, 160 insertions(+), 70 deletions(-) create mode 100644 Timer.php create mode 100644 Timers.php diff --git a/IRC.php b/IRC.php index 8ff6bb8..5446054 100644 --- a/IRC.php +++ b/IRC.php @@ -1,11 +1,11 @@ $val) { $this->$arg = $val; } } - + public function connect() { $address = gethostbyname($this->server); @@ -30,7 +30,7 @@ public function connect() { } else { echo "OK.\n"; } - + echo "Attempting to connect to '$this->server' ('$address') on port '$this->port'..."; $result = socket_connect($this->socket, $address, $this->port); if ($result === false) { @@ -38,11 +38,12 @@ public function connect() { } else { echo "OK.\n"; } - + $this->actions = new Actions(); - + $this->timers = new Timers($this); + } - + public function authorize() { $this->send("NICK " . $this->nick); $this->send("USER " . $this->nick . " " . $this->nick . " " . $this->nick . " " .$this->realname); @@ -50,76 +51,91 @@ public function authorize() { $this->send($this->authentication); } } - + public function join() { foreach($this->channels as $chan) { $this->send("JOIN " . $chan); } } - + public function handleRecv() { - while ($recv = socket_read($this->socket, 2048)) { - echo "<< " . $recv; - - $message = ""; - $channel = ""; - $parts = explode(" :", $recv); - $source_parts = explode(" ", substr($parts[0], 1)); - $user = $source_parts[0]; - $user_parts = explode("!", $user); - $nickname = $user_parts[0]; - - if ( isset($source_parts[2]) ) { $channel = $source_parts[2]; } - if ( $channel == $this->nick ) { $channel = $nickname; } - - if ( isset($parts[1]) ) { $message = $parts[1]; } - - if ( strstr($recv, " 433 ") ) { // Nickname is already in use. // add | and try again - $this->nick .= "|"; - $this->send("NICK " . $this->nick); - } - - if ( strstr($recv, " 451 ") ) { // You have not registered. // try joining again - $this->join(); - } - - // respond to PINGs - if ( substr($recv, 0, 4) == "PING" ) { - $this->send("PONG" . substr($recv, 4)); - } - - if ( isset($parts[1]) && substr($channel, 0, 1) == "#" ) { - // if message came from a channel, see if there is anything we can do with the whole message - $this->sendToChan( $channel, Misc::analyze($message) ); - } - - // first character is !, must be a command - if ( substr($message, 0, 1) == "!" ) { - if ( strstr($message, " ") ) { $params = explode(" ", $message); } - else { $params = array($message); } - $command = trim(strtolower(substr($params[0], 1))); - - if ( isset($this->actions->plugins[$command]) ) { - $args = explode(" ", trim($message)); - unset($args[0]); $args = array_values(array_filter($args)); - - $className = $this->actions->plugins[$command]; - $result = $className::$command($channel, $nickname, $args); - $this->sendToChan( $channel, $result ); - } - } - + socket_set_nonblock($this->socket); + while (true) { + $recv = socket_read($this->socket, 2048); + if ( $recv === false ){ + $err = socket_last_error($this->socket); + if ( $err != 11 ){ + echo "Error: (" . $err . ") " . socket_strerror($err) . "\r\n"; + break; + } + } + if ( $recv != "" ){ + echo "<< " . $recv; + + $message = ""; + $channel = ""; + $parts = explode(" :", $recv); + $source_parts = explode(" ", substr($parts[0], 1)); + $user = $source_parts[0]; + $user_parts = explode("!", $user); + $nickname = $user_parts[0]; + + if ( isset($source_parts[2]) ) { $channel = $source_parts[2]; } + if ( $channel == $this->nick ) { $channel = $nickname; } + + if ( isset($parts[1]) ) { $message = $parts[1]; } + + if ( strstr($recv, " 433 ") ) { // Nickname is already in use. // add | and try again + $this->nick .= "|"; + $this->send("NICK " . $this->nick); + } + + if ( strstr($recv, " 451 ") ) { // You have not registered. // try joining again + $this->join(); + } + + // respond to PINGs + if ( substr($recv, 0, 4) == "PING" ) { + $this->send("PONG" . substr($recv, 4)); + } + + if ( isset($parts[1]) && substr($channel, 0, 1) == "#" ) { + // if message came from a channel, see if there is anything we can do with the whole message + $this->sendToChan( $channel, Misc::analyze($message) ); + } + + // first character is !, must be a command + if ( substr($message, 0, 1) == "!" ) { + + if ( strstr($message, " ") ) { $params = explode(" ", $message); } + else { $params = array($message); } + + $command = trim(strtolower(substr($params[0], 1))); + + if ( isset($this->actions->plugins[$command]) ) { + $args = explode(" ", trim($message)); + unset($args[0]); $args = array_values(array_filter($args)); + + $className = $this->actions->plugins[$command]; + $result = $className::$command($channel, $nickname, $args); + $this->sendToChan( $channel, $result ); + } + } + } + + $this->timers->updateTimers(); + sleep(1); } socket_close($this->socket); echo "Attempting to reconnect in 30 seconds...\n"; sleep(30); } - - private function send($msg) { + + public function send($msg) { if ( !is_array($msg) ) { $msg = array($msg); } - + foreach ($msg as $line) { if ( strlen($line) > 0 ) { $line = trim($line) . "\n"; @@ -129,16 +145,16 @@ private function send($msg) { } } - + // accepts string and array of strings for $msg - private function sendToChan($channel, $msg) { + public function sendToChan($channel, $msg) { if ( !is_array($msg) ) { $msg = array($msg); } - + if ( isset($msg["chan"]) ) { $channel = $msg["chan"]; unset($msg["chan"]); } if ( isset($msg["server"]) ) { unset($msg["server"]); $this->send($msg); return NULL; } - + foreach ($msg as $line) { if ( strlen($line) > 0 ) { $line = "PRIVMSG " . $channel . " :" . trim($line) . "\n"; @@ -147,7 +163,7 @@ private function sendToChan($channel, $msg) { } } } - + } ?> \ No newline at end of file diff --git a/Misc.php b/Misc.php index 7da9dc0..344dfe9 100644 --- a/Misc.php +++ b/Misc.php @@ -22,6 +22,20 @@ public static function analyze($message) { return $result; } + + public static function getUrl($url){ + + $ch = curl_init(); + $timeout = 5; + curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); + $data = curl_exec($ch); + curl_close($ch); + return $data; + + } } ?> diff --git a/Timer.php b/Timer.php new file mode 100644 index 0000000..cf1b086 --- /dev/null +++ b/Timer.php @@ -0,0 +1,36 @@ +lastCheck = time(); + $this->timer = 0; + $this->irc = $irc; + $this->initialize(); + } + + public function updateTimer(){ + $this->timer += ( time() - $this->lastCheck ); + if ( $this->timer >= $this->timeout ){ + $this->execute(); + $this->timer = 0; + } + $this->lastCheck = time(); + } + + protected function initialize(){ + // put code you want executed after __construct here + // in case your plugin needs to initialize some stuff + } + + protected function execute(){ + // put the code you want to execute when the + // timeout is reached in the execute function + } +} + +?> \ No newline at end of file diff --git a/Timers.php b/Timers.php new file mode 100644 index 0000000..a215cea --- /dev/null +++ b/Timers.php @@ -0,0 +1,22 @@ +plugins = array(); + foreach (get_declared_classes() as $class) { + if (is_subclass_of($class, "Timer")) { + $this->plugins[] = new $class($irc); + } + } + } + + function updateTimers(){ + foreach ( $this->plugins as $k => $class ){ + $class->updateTimer(); + } + } +} + +?> \ No newline at end of file diff --git a/utabot.php b/utabot.php index 5752f19..5eabdfe 100755 --- a/utabot.php +++ b/utabot.php @@ -4,6 +4,8 @@ require_once("IRC.php"); require_once("Actions.php"); +require_once("Timer.php"); +require_once("Timers.php"); require_once("Misc.php"); foreach (glob( dirname(__FILE__) . "/plugins/*.php" ) as $filename) { require_once($filename); } From ca23b48d53df298f04a1c5f30e1196bf13710892 Mon Sep 17 00:00:00 2001 From: Brandon Date: Thu, 3 Nov 2016 20:36:53 -0500 Subject: [PATCH 2/2] Fix bug --- IRC.php | 1 + 1 file changed, 1 insertion(+) diff --git a/IRC.php b/IRC.php index 5446054..40933bc 100644 --- a/IRC.php +++ b/IRC.php @@ -5,6 +5,7 @@ class IRC { private $socket; private $actions; + private $timers; private $server; private $port;