From: pk910 Date: Wed, 10 Aug 2011 03:56:23 +0000 (+0200) Subject: added PulseBot X-Git-Url: http://git.pk910.de/?p=PHP-P10.git;a=commitdiff_plain;h=ddffd103502359153b425495e8834ebcb1e157f0 added PulseBot --- diff --git a/Bots/PulseBot.class.php b/Bots/PulseBot.class.php new file mode 100644 index 0000000..997513a --- /dev/null +++ b/Bots/PulseBot.class.php @@ -0,0 +1,272 @@ +. * + * * + ************************************************************************ + * + * Bots/PulseBot.class.php + * + * WhatPulse bot... + * + */ + +class {$_NAME} extends Bot { + private $uplink; + private $pulsebot; + private $cache = array(); + private $db = array(); + + const TEAM_ID = 19418; + + public function load($uplink, $old = false) { + $this->uplink = $uplink; + if(!$old) { + $nick = "PulseBot"; + $ident = "pulsebot"; + $ip = "0::0"; + $host = "Services.WebGamesNet.net"; + $realname = "WhatPulse Contest Bot (join us on #WhatPulse)"; + $modes = "ioknISDrf WhatPulse WhatPulse.bot.WebGamesNet"; + $this->pulsebot = $this->uplink->addUser($nick,$ident,$host,$ip,$modes,$realname); + if(is_a($this->pulsebot, "P10_User")) { + $this->uplink->join($this->pulsebot, "#WhatPulse", (P10_Channel::USERPRIV_OPED | P10_Channel::USERPRIV_VOICE)); + $this->uplink->join($this->pulsebot, "#dev", P10_Channel::USERPRIV_VOICE); + } + } else { + $this->pulsebot = $old; + } + BotLoader::registerDB($this, "PulseBot"); + ModCMD::bind($this, BIND_CHANMSG, "recive_privmsg"); + ModCMD::bind($this, BIND_JOIN, "recive_join"); + ModCMD::bind($this, BIND_QUIT, "recive_quit"); + } + + public function unload($rehash = false) { + if($rehash) { + return $this->pulsebot; + } else { + $this->uplink->delUser($this->pulsebot, "Bye."); + } + } + + public function readDB($db) { + $this->db = $db; + } + + public function writeDB() { + return $this->db; + } + + private function getStats() { + if(array_key_exists("stats", $this->cache) && time() - $this->cache['stats_time'] < 300) { + //use cached stats + $stats = $this->cache["stats"]; + } else { + $xmlstr = file_get_contents("http://whatpulse.org/api/team.php?TeamID=".self::TEAM_ID); + $stats = new SimpleXMLElement($xmlstr); + $this->cache["stats"] = $stats; + $this->cache["stats_time"] = time(); + } + return $stats; + } + + private function getUserStats($name) { + if(array_key_exists("ustats_".strtolower($name), $this->cache) && time() - $this->cache['ustats_'.strtolower($name).'_time'] < 300) { + //use cached stats + $ustats = $this->cache["ustats_".strtolower($name)]; + return $ustats; + } + $stats = $this->getStats(); + $users = $stats->TeamStats->Members; + foreach ($users as $user) { + if(strtolower($user->MemberName) == strtolower($name)) { + $xmlstr = file_get_contents("http://whatpulse.org/api/user.php?UserID=".$user->MemberUserID); + $ustats = new SimpleXMLElement($xmlstr); + $this->cache["ustats_".strtolower($name)] = $stats; + $this->cache["ustats_".strtolower($name)."_time"] = time(); + return $ustats; + } + } + return null; + } + + private function botOppedOnChannel($channel) { + $privs = $channel->getUserPrivs($this->pulsebot); + return ($privs & P10_Channel::USERPRIV_OPED); + } + + function recive_privmsg($user, $channel, $message) { + if(!$this->botOppedOnChannel()) return; + $privs = $PHPChannel->getUserPrivs($user); + $op = ($user->getModes()->hasMode('o') || ($privs & P10_Channel::USERPRIV_OPED)); + $exp = explode(" ", $message); + switch(strtolower($exp[0])) { + case ".adduser": + if(!$op) break; + if(count($exp) < 3) { + $this->uplink->notice($this->pulsebot, $user, "usage: .adduser "); + } else { + $pulseUserName = $exp[1]; + $stats = $this->getStats(); + $pulseUser = null; + $users = $stats->TeamStats->Members; + foreach ($users as $puser) { + if(strtolower($puser->MemberName) == strtolower($pulseUserName)) { + $pulseUser = $puser; + break; + } + } + if($pulseUser == null) { + $this->uplink->notice($this->pulsebot, $user, "WhatPulse user '".$pulseUserName."' is unknown or is not in Team '".$stats->TeamStats->Statistics->TeamName."'."); + break; + } + if($exp[2][0] == "*") { + $auth = substr($exp[2], 1); + } else { + $IRCUser = P10_User::getUserByNick($exp[2]); + if($IRCUser == null || !($auth = $IRCUser->getModes()->hasMode('r'))) { + $this->uplink->notice($this->pulsebot, $user, "IRC User user '".$pulseUserName."' is unknown or is not authed."); + break; + } + } + if(!array_key_exists("users", $this->db)) { + $this->db['users'] = array(); + } + $this->db['users'][$auth] = $pulseUser->MemberName; + $this->uplink->notice($this->pulsebot, $user, "WhatPulse user '".$pulseUserName."' added (Keys: ".$pulseUser->MemberKeys.", Clicks: ".$pulseUser->MemberClicks.")."); + } + break; + case ".deluser": + if(!$op) break; + if(!array_key_exists("users", $this->db)) { + $this->db['users'] = array(); + } + $f = false; + foreach($this->db['users'] as $ircUser => $pulseUser) { + if(strtolower($pulseUser) == strtolower($exp[1])) { + unset($this->db['users'][strtolower($exp[1])]); + $this->uplink->notice($this->pulsebot, $user, "WhatPulse user '".$exp[1]."' deleted."); + $f = true; + break; + } + } + if(!$f) + $this->uplink->notice($this->pulsebot, $user, "WhatPulse user '".$exp[1]."' is not added."); + break; + case ".users": + $table = new Table(5); + $table->add("User", "Keys", "Clicks", "Miles", "IRC"); + $stats = $this->getStats(); + $users = $stats->TeamStats->Members; + if(!array_key_exists("users", $this->db)) { + $this->db['users'] = array(); + } + foreach ($users as $puser) { + $ircUserName = "-"; + foreach($this->db['users'] as $ircUser => $pulseUser) { + if(strtolower($pulseUser) == strtolower($puser->MemberName)) { + $ircUserName = $ircUser; + break; + } + } + $table->add($puser->MemberName, $puser->MemberKeys, $puser->MemberClicks, $puser->MemberMiles, $ircUserName); + } + $lines = $table->end(); + foreach($lines as $line) { + if(count($exp) > 1) + $this->uplink->privmsg($this->pulsebot, $channel, $line); + else + $this->uplink->notice($this->pulsebot, $user, $line); + } + break; + case ".mypulse": + if(!($auth = $user->getModes()->hasMode('r'))) { + $this->uplink->notice($this->pulsebot, $user, "You are not authed with AuthServ."); + break; + } + $pulseUserName = false; + foreach($this->db['users'] as $ircUser => $pulseUser) { + if(strtolower($ircUser) == strtolower($auth)) { + $pulseUserName = $pulseUser; + break; + } + } + $pulseUser = null; + if($pulseUserName) { + $stats = $this->getStats(); + $users = $stats->TeamStats->Members; + foreach ($users as $puser) { + if(strtolower($puser->MemberName) == strtolower($pulseUserName)) { + $pulseUser = $puser; + break; + } + } + } + if(!$pulseUser) { + $this->uplink->notice($this->pulsebot, $user, "There is no WhatPulse User connected with your AuthServ Username. If you have a WhatPulse Account, please ask an Op to do add you to the list."); + break; + } + $ustats = $this->getUserStats($pulseUser->MemberName); + $message = "User \002".$pulseUser->MemberName."\002 has \002".$pulseUser->MemberKeys."\002 keys (".$ustats->AvKPS." per second), \002".$pulseUser->MemberClicks."\002 clicks (".$ustats->AvCPS." per second) and ".$pulseUser->MemberMiles." miles.\n"; + $message .= "Last Pulse: ".$ustats->LastPulse; + if(count($exp) > 1) { + foreach($message as $line) { + $this->uplink->privmsg($this->pulsebot, $channel, $line); + } + } else { + foreach($message as $line) { + $this->uplink->notice($this->pulsebot, $user, $line); + } + } + break; + } + } + + public function recive_join($user, $channel, $isBurst) { + if(!$this->botOppedOnChannel($channel)) return false; + if(!($auth = $user->getModes()->hasMode('r'))) return; + $pulseUserName = false; + foreach($this->db['users'] as $ircUser => $pulseUser) { + if(strtolower($ircUser) == strtolower($auth)) { + $pulseUserName = $pulseUser; + break; + } + } + $pulseUser = null; + if($pulseUserName) { + $stats = $this->getStats(); + $users = $stats->TeamStats->Members; + foreach ($users as $puser) { + if(strtolower($puser->MemberName) == strtolower($pulseUserName)) { + $pulseUser = $puser; + break; + } + } + } + if($pulseUser) { + $this->uplink->mode($this->pulsebot, $channel, "+v ".$user->getNumeric()); + $this->uplink->privmsg($this->pulsebot, $channel, "WhatPulse User \002".$pulseUser->MemberName."\002 (\002".$pulseUser->MemberKeys."\002 keys and \002".$pulseUser->MemberClicks."\002 clicks) has joined the channel."); + } + } + + public function recive_quit($user, $reason) { + if($user === $this->pulsebot) { + $this->load($this->uplink); + } + } +} + +?> \ No newline at end of file diff --git a/Tools/Table.class.php b/Tools/Table.class.php new file mode 100644 index 0000000..3d01798 --- /dev/null +++ b/Tools/Table.class.php @@ -0,0 +1,53 @@ +table = array(); + $this->table['set'] = array(); + $this->table['data'] = array(); + $this->table['set']['col'] = $colums; + $this->table['set']['bold'] = array(); + for($i = 0; $i < $this->table['set']['col']; $i++) { + $this->table['set']['max'.$i] = 0; + $this->table['set']['bold'][$i] = false; + } + } + + public function setBold($colum) { + $this->table['set']['bold'][$colum] = true; + } + + public function add() { + $args = func_get_args(); + $row = array(); + for($i = 0; $i < $this->table['set']['col']; $i++) { + if(count($args) <= $i) $args[$i]= ""; + $row[] = $args[$i]; + if(count($args) >= $i) + if(strlen($args[$i]) > $this->table['set']['max'.$i]) $this->table['set']['max'.$i] = strlen($args[$i]); + } + $this->table['data'][] = $row; + return true; + } + + public function end() { + $space = " "; + $output = array(); + for($row = 0; $row < count($this->table['data']); $row++) { + $out = ""; + for($i = 0; $i < $this->table['set']['col']; $i++) { + if($i < $this->table['set']['col'] - 1) + $this->table['data'][$row][$i] .= substr($space,0,$this->table['set']['max'.$i] - strlen($this->table['data'][$row][$i]) + 1); + $bold = $this->table['set']['bold'][$i]; + $out .= ($bold ? "\002" : "").$this->table['data'][$row][$i].($bold ? "\002" : ""); + } + $output[] = $out; + } + return $output; + } + +} + +?> \ No newline at end of file diff --git a/main.php b/main.php index 2df531e..7ea2ed4 100644 --- a/main.php +++ b/main.php @@ -29,6 +29,7 @@ require_once("Uplink/Uplink.class.php"); require_once("ModCMD/ModCMD.class.php"); require_once("BotLoader/BotLoader.class.php"); require_once("Tools/timer.inc.php"); +require_once("Tools/Table.class.php"); if(function_exists("pcntl_signal")) { pcntl_signal(SIGINT, 'shutdown');