Another year is about to end... So we have to update these damn copyright information :P
[PHP-P10.git] / Bots / PulseBot.class.php
1 <?php
2 /******************************* PHP-P10 v2 *****************************
3  * Copyright (C) 2011-2012  Philipp Kreil (pk910)                       *
4  *                                                                      *
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.                                  *
9  *                                                                      * 
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.                         *
14  *                                                                      *
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/>. *
17  *                                                                      *
18  ************************************************************************
19  * 
20  *  Bots/PulseBot.class.php
21  *
22  * WhatPulse bot...
23  *
24  */
25
26 class {$_NAME} extends Bot {
27         private $uplink;
28         private $pulsebot;
29         private $cache = array();
30         private $db = array();
31         
32         const TEAM_ID = 19418;
33         
34         public function load($uplink, $old = false) {
35                 $this->uplink = $uplink;
36                 if(!$old) {
37                         $nick = "PulseBot";
38                         $ident = "pulsebot";
39                         $ip = "0::0";
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);
47                         }
48                 } else {
49                         $this->pulsebot = $old;
50                 }
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");
55         }
56         
57         public function unload($rehash = false) {
58                 if($rehash) {
59                         return $this->pulsebot;
60                 } else {
61                         $this->uplink->delUser($this->pulsebot, "Bye.");
62                 }
63         }
64         
65         public function readDB($db) {
66                 $this->db = $db;
67         }
68         
69         public function writeDB() {
70                 return $this->db;
71         }
72         
73         private function getStats() {
74                 if(array_key_exists("stats", $this->cache) && time() - $this->cache['stats_time'] < 300) {
75                         //use cached stats
76                         $stats = $this->cache["stats"];
77                 } else {
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();
82                 }
83                 return $stats;
84         }
85         
86         private function getUserStats($name) {
87                 if(array_key_exists("ustats_".strtolower($name), $this->cache) && time() - $this->cache['ustats_'.strtolower($name).'_time'] < 300) {
88                         //use cached stats
89                         $ustats = $this->cache["ustats_".strtolower($name)];
90                         return $ustats;
91                 }
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();
100                                 return $ustats;
101                         }
102                 }
103                 return null;
104         }
105         
106         private function botOppedOnChannel($channel) {
107                 $privs = $channel->getUserPrivs($this->pulsebot);
108                 return ($privs & P10_Channel::USERPRIV_OPED);
109         }
110         
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])) {
117                         case ".adduser":
118                                 if(!$op) break;
119                                 if(count($exp) < 3) {
120                                         $this->uplink->notice($this->pulsebot, $user, "usage: .adduser <WhatPulseUser> <IRCUser/*IRCAuth>");
121                                 } else {
122                                         $pulseUserName = $exp[1];
123                                         $stats = $this->getStats();
124                                         $pulseUser = null;
125                                         $users = $stats->TeamStats->Members->Member;
126                                         foreach ($users as $puser) {
127                                                 if(strtolower($puser->MemberName) == strtolower($pulseUserName)) {
128                                                         $pulseUser = $puser;
129                                                         break;
130                                                 }
131                                         }
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."'.");
134                                                 break;
135                                         }
136                                         if($exp[2][0] == "*") {
137                                                 $auth = substr($exp[2], 1);
138                                         } else {
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.");
142                                                         break;
143                                                 }
144                                         }
145                                         if(!array_key_exists("users", $this->db)) {
146                                                 $this->db['users'] = array();
147                                         }
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,',','.').").");
150                                 }
151                                 break;
152                         case ".deluser":
153                                 if(!$op) break;
154                                 if(!array_key_exists("users", $this->db)) {
155                                         $this->db['users'] = array();
156                                 }
157                                 $f = false;
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.");
162                                                 $f = true;
163                                                 break;
164                                         }
165                                 }
166                                 if(!$f)
167                                         $this->uplink->notice($this->pulsebot, $user, "WhatPulse user '".$exp[1]."' is not added.");
168                                 break;
169                         case ".users":
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();
176                                 }
177                                 foreach ($users as $puser) {
178                                         $ircUserName = "-";
179                                         foreach($this->db['users'] as $ircUser => $pulseUser) {
180                                                 if(strtolower($pulseUser) == strtolower($puser->MemberName)) {
181                                                         $ircUserName = $ircUser;
182                                                         break;
183                                                 }
184                                         }
185                                         $table->add($puser->MemberName, number_format(floatval($puser->MemberKeys),0,',','.'), number_format(floatval($puser->MemberClicks),0,',','.'), number_format(floatval($puser->MemberMiles),2,',','.'), $ircUserName);
186                                 }
187                                 $lines = $table->end();
188                                 foreach($lines as $line) {
189                                         if(count($exp) > 1)
190                                                 $this->uplink->privmsg($this->pulsebot, $channel, $line);
191                                         else
192                                                 $this->uplink->notice($this->pulsebot, $user, $line);
193                                 }
194                                 break;
195                         case ".mypulse":
196                                 if(!($auth = $user->getModes()->hasMode('r'))) {
197                                         $this->uplink->notice($this->pulsebot, $user, "You are not authed with AuthServ.");
198                                         break;
199                                 }
200                                 $pulseUserName = false;
201                                 foreach($this->db['users'] as $ircUser => $pulseUser) {
202                                         if(strtolower($ircUser) == strtolower($auth)) {
203                                                 $pulseUserName = $pulseUser;
204                                                 break;
205                                         }
206                                 }
207                                 $pulseUser = null;
208                                 if($pulseUserName) {
209                                         $stats = $this->getStats();
210                                         $users = $stats->TeamStats->Members->Member;
211                                         foreach ($users as $puser) {
212                                                 if(strtolower($puser->MemberName) == strtolower($pulseUserName)) {
213                                                         $pulseUser = $puser;
214                                                         break;
215                                                 }
216                                         }
217                                 }
218                                 if(!$pulseUser) {
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.");
220                                         break;
221                                 }
222                                 $ustats = $this->getUserStats($pulseUser->MemberName);
223                                 $message = array();
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);
229                                         }
230                                 } else {
231                                         foreach($message as $line) {
232                                                 $this->uplink->notice($this->pulsebot, $user, $line);
233                                         }
234                                 }
235                                 break;
236                 }
237         }
238         
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;
246                                 break;
247                         }
248                 }
249                 $pulseUser = null;
250                 if($pulseUserName) {
251                         $stats = $this->getStats();
252                         $users = $stats->TeamStats->Members->Member;
253                         foreach ($users as $puser) {
254                                 if(strtolower($puser->MemberName) == strtolower($pulseUserName)) {
255                                         $pulseUser = $puser;
256                                         break;
257                                 }
258                         }
259                 }
260                 if($pulseUser) {
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.");
263                 }
264         }
265         
266         public function recive_quit($user, $reason) {
267                 if($user === $this->pulsebot) {
268                         $this->load($this->uplink);
269                 }
270         }
271 }
272
273 ?>