2 /******************************* PHP-P10 v2 *****************************
3 * Copyright (C) 2011-2012 Philipp Kreil (pk910) *
5 * This program is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 3 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 ************************************************************************
20 * Bots/PulseBot.class.php
26 class {$_NAME} extends Bot {
29 private $cache = array();
30 private $db = array();
32 const TEAM_ID = 19418;
34 public function load($uplink, $old = false) {
35 $this->uplink = $uplink;
40 $host = "Services.WebGamesNet.net";
41 $realname = "WhatPulse Contest Bot (join us on #WhatPulse)";
42 $modes = "ioknISDrf WhatPulse WhatPulse.bot.WebGamesNet";
43 $this->pulsebot = $this->uplink->addUser($nick,$ident,$host,$ip,$modes,$realname);
44 if(is_a($this->pulsebot, "P10_User")) {
45 $this->uplink->join($this->pulsebot, "#WhatPulse", (P10_Channel::USERPRIV_OPED | P10_Channel::USERPRIV_VOICE));
46 $this->uplink->join($this->pulsebot, "#dev", P10_Channel::USERPRIV_VOICE);
49 $this->pulsebot = $old;
51 BotLoader::registerDB($this, "pulsebot");
52 ModCMD::bind($this, BIND_CHANMSG, "recive_privmsg");
53 ModCMD::bind($this, BIND_JOIN, "recive_join");
54 ModCMD::bind($this, BIND_QUIT, "recive_quit");
57 public function unload($rehash = false) {
59 return $this->pulsebot;
61 $this->uplink->delUser($this->pulsebot, "Bye.");
65 public function readDB($db) {
69 public function writeDB() {
73 private function getStats() {
74 if(array_key_exists("stats", $this->cache) && time() - $this->cache['stats_time'] < 300) {
76 $stats = $this->cache["stats"];
78 $xmlstr = file_get_contents("http://whatpulse.org/api/team.php?TeamID=".self::TEAM_ID);
79 $stats = new SimpleXMLElement($xmlstr);
80 $this->cache["stats"] = $stats;
81 $this->cache["stats_time"] = time();
86 private function getUserStats($name) {
87 if(array_key_exists("ustats_".strtolower($name), $this->cache) && time() - $this->cache['ustats_'.strtolower($name).'_time'] < 300) {
89 $ustats = $this->cache["ustats_".strtolower($name)];
92 $stats = $this->getStats();
93 $users = $stats->TeamStats->Members->Member;
94 foreach ($users as $user) {
95 if(strtolower($user->MemberName) == strtolower($name)) {
96 $xmlstr = file_get_contents("http://whatpulse.org/api/user.php?UserID=".$user->MemberUserID);
97 $ustats = new SimpleXMLElement($xmlstr);
98 $this->cache["ustats_".strtolower($name)] = $ustats;
99 $this->cache["ustats_".strtolower($name)."_time"] = time();
106 private function botOppedOnChannel($channel) {
107 $privs = $channel->getUserPrivs($this->pulsebot);
108 return ($privs & P10_Channel::USERPRIV_OPED);
111 function recive_privmsg($user, $channel, $message) {
112 if(!$this->botOppedOnChannel($channel)) return;
113 $privs = $channel->getUserPrivs($user);
114 $op = ($user->getModes()->hasMode('o') || ($privs & P10_Channel::USERPRIV_OPED));
115 $exp = explode(" ", $message);
116 switch(strtolower($exp[0])) {
119 if(count($exp) < 3) {
120 $this->uplink->notice($this->pulsebot, $user, "usage: .adduser <WhatPulseUser> <IRCUser/*IRCAuth>");
122 $pulseUserName = $exp[1];
123 $stats = $this->getStats();
125 $users = $stats->TeamStats->Members->Member;
126 foreach ($users as $puser) {
127 if(strtolower($puser->MemberName) == strtolower($pulseUserName)) {
132 if($pulseUser == null) {
133 $this->uplink->notice($this->pulsebot, $user, "WhatPulse user '".$pulseUserName."' is unknown or is not in Team '".$stats->TeamStats->Statistics->TeamName."'.");
136 if($exp[2][0] == "*") {
137 $auth = substr($exp[2], 1);
139 $IRCUser = P10_User::getUserByNick($exp[2]);
140 if($IRCUser == null || !($auth = $IRCUser->getModes()->hasMode('r'))) {
141 $this->uplink->notice($this->pulsebot, $user, "IRC User user '".$pulseUserName."' is unknown or is not authed.");
145 if(!array_key_exists("users", $this->db)) {
146 $this->db['users'] = array();
148 $this->db['users'][$auth] = strval($pulseUser->MemberName);
149 $this->uplink->notice($this->pulsebot, $user, "WhatPulse user '".$pulseUserName."' added (Keys: ".number_format(floatval($pulseUser->MemberKeys),0,',','.').", Clicks: ".number_format(floatval($pulseUser->MemberClicks),0,',','.').").");
154 if(!array_key_exists("users", $this->db)) {
155 $this->db['users'] = array();
158 foreach($this->db['users'] as $ircUser => $pulseUser) {
159 if(strtolower($pulseUser) == strtolower($exp[1])) {
160 unset($this->db['users'][strtolower($exp[1])]);
161 $this->uplink->notice($this->pulsebot, $user, "WhatPulse user '".$exp[1]."' deleted.");
167 $this->uplink->notice($this->pulsebot, $user, "WhatPulse user '".$exp[1]."' is not added.");
170 $table = new Table(5);
171 $table->add("User", "Keys", "Clicks", "Miles", "IRC");
172 $stats = $this->getStats();
173 $users = $stats->TeamStats->Members->Member;
174 if(!array_key_exists("users", $this->db)) {
175 $this->db['users'] = array();
177 foreach ($users as $puser) {
179 foreach($this->db['users'] as $ircUser => $pulseUser) {
180 if(strtolower($pulseUser) == strtolower($puser->MemberName)) {
181 $ircUserName = $ircUser;
185 $table->add($puser->MemberName, number_format(floatval($puser->MemberKeys),0,',','.'), number_format(floatval($puser->MemberClicks),0,',','.'), number_format(floatval($puser->MemberMiles),2,',','.'), $ircUserName);
187 $lines = $table->end();
188 foreach($lines as $line) {
190 $this->uplink->privmsg($this->pulsebot, $channel, $line);
192 $this->uplink->notice($this->pulsebot, $user, $line);
196 if(!($auth = $user->getModes()->hasMode('r'))) {
197 $this->uplink->notice($this->pulsebot, $user, "You are not authed with AuthServ.");
200 $pulseUserName = false;
201 foreach($this->db['users'] as $ircUser => $pulseUser) {
202 if(strtolower($ircUser) == strtolower($auth)) {
203 $pulseUserName = $pulseUser;
209 $stats = $this->getStats();
210 $users = $stats->TeamStats->Members->Member;
211 foreach ($users as $puser) {
212 if(strtolower($puser->MemberName) == strtolower($pulseUserName)) {
219 $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.");
222 $ustats = $this->getUserStats($pulseUser->MemberName);
224 $message[] = "User \002".$pulseUser->MemberName."\002 has \002".number_format(floatval($pulseUser->MemberKeys),0,',','.')."\002 keys (".number_format(floatval($ustats->AvKPS),2,',','.')." per second), \002".number_format(floatval($pulseUser->MemberClicks),0,',','.')."\002 clicks (".number_format(floatval($ustats->AvCPS),2,',','.')." per second) and ".number_format(floatval($pulseUser->MemberMiles),2,',','.')." miles.";
225 $message[] = "Last Pulse: ".$ustats->LastPulse;
226 if(count($exp) > 1) {
227 foreach($message as $line) {
228 $this->uplink->privmsg($this->pulsebot, $channel, $line);
231 foreach($message as $line) {
232 $this->uplink->notice($this->pulsebot, $user, $line);
239 public function recive_join($user, $channel, $isBurst) {
240 if(!$this->botOppedOnChannel($channel)) return false;
241 if(!($auth = $user->getModes()->hasMode('r'))) return;
242 $pulseUserName = false;
243 foreach($this->db['users'] as $ircUser => $pulseUser) {
244 if(strtolower($ircUser) == strtolower($auth)) {
245 $pulseUserName = $pulseUser;
251 $stats = $this->getStats();
252 $users = $stats->TeamStats->Members->Member;
253 foreach ($users as $puser) {
254 if(strtolower($puser->MemberName) == strtolower($pulseUserName)) {
261 $this->uplink->mode($this->pulsebot, $channel, "+v ".$user->getNumeric());
262 $this->uplink->privmsg($this->pulsebot, $channel, "WhatPulse User \002".$pulseUser->MemberName."\002 (\002".number_format(floatval($pulseUser->MemberKeys),0,',','.')."\002 keys and \002".number_format(floatval($pulseUser->MemberClicks),0,',','.')."\002 clicks) has joined the channel.");
266 public function recive_quit($user, $reason) {
267 if($user === $this->pulsebot) {
268 $this->load($this->uplink);