9fd0f70bf8599409d6b14142bc812c1e6a0d5cb8
[PHP-P10.git] / BotLoader / BotLoader.class.php
1 <?php
2 /********************************* PHP-P10 ******************************
3  *    P10 uplink class by pk910   (c)2011 pk910                         *
4  ************************************************************************
5  *                          Version 2 (OOP)                             *
6  *                                                                      *
7  * PHP-P10 is free software; you can redistribute it and/or modify      *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or    *
10  * (at your option) any later version.                                  *
11  *                                                                      *
12  * This program is distributed in the hope that it will be useful,      *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
15  * GNU General Public License for more details.                         *
16  *                                                                      *
17  * You should have received a copy of the GNU General Public License    *
18  * along with PHP-P10; if not, write to the Free Software Foundation,   *
19  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.       *
20  *                                                                      *
21  ************************************************************************
22  * 
23  *  BotLoader/BotLoader.class.php
24  *
25  * This class loades / rehashs or unloads all Bots.
26  *
27  */
28 require_once("Bot.class.php");
29 require_once("saxdb.class.php");
30
31 class BotLoader {
32         const BOT_DIR = "Bots";
33         const TMP_DIR = "tmp";
34         private $uplink;
35         private $saxdb;
36         private $botdb;
37         private $loadedBots = array();
38         private $botDatabases = array();
39         private static $botloader;
40         
41         public function __construct($uplink) {
42                 $this->uplink = $uplink;
43                 $this->saxdb = new saxdb();
44                 $this->saxdb->loadDB("php_p10.db");
45                 $this->botdb = $this->saxdb->getSection("BotLoader");
46                 self::$botloader = $this;
47                 timer(60*10, array($this, "autosave"), array());
48         }
49         
50         public function loadBots() {
51                 if(array_key_exists("bots", $this->botdb) && is_array($this->botdb["bots"])) {
52                         foreach($this->botdb["bots"] as $name => $botfile) {
53                                 $this->loadBot($name, $botfile);
54                         }
55                 } else {
56                         $this->botdb["bots"] = array();
57                 }
58         }
59         
60         public function unloadBots() {
61                 foreach($this->loadedBots as $name => $bot) {
62                         $this->unloadBot($name, false);
63                 }
64         }
65         
66         public function save() {
67                 $this->saxdb->setSection("BotLoader", $this->botdb);
68                 $this->saxdb->writeDB("php_p10.db");
69         }
70         
71         public function autosave() {
72                 foreach($this->loadedBots as $name => $bot) {
73                         if(array_key_exists(strtolower($name), $this->botDatabases)) {
74                                 $db = $bot->writeDB();
75                                 $this->saxdb->setSection($this->botDatabases[strtolower($name)],$db);
76                         }
77                 }
78                 $this->save();
79                 timer(60*10, array($this, "autosave"), array());
80         }
81         
82         public function loop() {
83                 foreach($this->loadedBots as $name => $bot) {
84                         $bot->loop();
85                 }
86         }
87         
88         private function loadBot($name, $botfile) {
89                 if(array_key_exists(strtolower($name), $this->loadedBots)) return false;
90                 //load bot
91                 $bot = $this->loadClass($botfile, $name);
92                 if(!$bot) return false;
93                 $bot->load($this->uplink);
94                 $this->loadedBots[strtolower($name)] = $bot;
95                 if(!array_key_exists($name, $this->botdb["bots"])) {
96                         $this->botdb["bots"][$name] = $botfile;
97                 }
98                 return true;
99         }
100         
101         private function unloadBot($name, $delete = true) {
102                 if(!(array_key_exists(strtolower($name), $this->loadedBots))) return false;
103                 //unload bot
104                 $bot = $this->loadedBots[strtolower($name)];
105                 if(array_key_exists(strtolower($name), $this->botDatabases)) {
106                         $db = $bot->writeDB();
107                         $this->saxdb->setSection($this->botDatabases[strtolower($name)],$db);
108                         unset($this->botDatabases[strtolower($name)]);
109                 }
110                 ModCMD::unbindBot($bot);
111                 $bot->unload(false);
112                 unset($this->loadedBots[strtolower($name)]);
113                 if(array_key_exists($name, $this->botdb["bots"]) && $delete) {
114                         unset($this->botdb["bots"][$name]);
115                 }
116                 return true;
117         }
118         
119         private function rehashBot($name) {
120                 if(!(array_key_exists(strtolower($name), $this->loadedBots))) return false;
121                 $botfile = null;
122                 foreach($this->botdb["bots"] as $botname => $file) {
123                         if(strtolower($botname) == strtolower($name)) {
124                                 $botfile = $file;
125                                 break;
126                         }
127                 }
128                 if(!$botfile) break;
129                 //rehash bot
130                 $bot = $this->loadedBots[strtolower($name)];
131                 if(array_key_exists(strtolower($name), $this->botDatabases)) {
132                         $db = $bot->writeDB();
133                         $this->saxdb->setSection($this->botDatabases[strtolower($name)],$db);
134                         unset($this->botDatabases[strtolower($name)]);
135                 }
136                 ModCMD::unbindBot($bot);
137                 $data = $bot->unload(true);
138                 unset($this->loadedBots[strtolower($name)]);
139                 //ok  bot is unloaded... load it again...
140                 $bot = $this->loadClass($botfile, $name);
141                 if(!$bot) return false;
142                 $bot->load($this->uplink, $data);
143                 $this->loadedBots[strtolower($name)] = $bot;
144                 return true;
145         }
146         
147         private function listLoadedBots() {
148                 return $this->loadedBots;
149         }
150         
151         private function addDBsection($bot, $section) {
152                 if(!is_a($bot, "Bot")) return false;
153                 $name = null;
154                 foreach($this->loadedBots as $botname => $cbot) {
155                         if($cbot === $bot) {
156                                 $name = $botname;
157                                 break;
158                         }
159                 }
160                 if($name == null) return false;
161                 if(array_key_exists(strtolower($name), $this->botDatabases)) {
162                         return false;
163                 }
164                 $this->botDatabases[strtolower($name)] = $section;
165                 $bot->readDB($this->saxdb->getSection($section));
166         }
167         
168         private function loadClass($file, $classprefix) {
169                 $dir = self::BOT_DIR;
170                 $tmp = self::TMP_DIR;
171                 $maincode=file_get_contents($dir."/".$file);
172                 if(!$maincode) return;
173                 $class = rand(10000,99999);
174                 while(class_exists("bot_".$classprefix."_".$class)) {
175                         $class = rand(10000,99999);
176                 }
177                 $maincode = str_replace('{$_NAME}', "module_".$classprefix."_".$class, $maincode);
178                 $fp = fopen($tmp."/modules_".$classprefix."_".$class.".tmp.php", 'w');
179                 fwrite($fp, $maincode);
180                 fclose($fp);
181                 include($tmp."/modules_".$classprefix."_".$class.".tmp.php");
182                 $classname = "module_".$classprefix."_".$class;
183                 $newclass = new $classname();
184                 unlink($tmp."/modules_".$classprefix."_".$class.".tmp.php");
185                 return $newclass;
186         }
187         
188         public static function load($name, $botfile) {
189                 return self::$botloader->loadBot($name, $botfile);
190         }
191         
192         public static function unload($name) {
193                 return self::$botloader->unloadBot($name);
194         }
195         
196         public static function rehash($name) {
197                 return self::$botloader->rehashBot($name);
198         }
199         
200         public static function listBots() {
201                 return self::$botloader->listLoadedBots();
202         }
203         
204         public static function registerDB($bot, $name) {
205                 return self::$botloader->addDBsection($bot, $name);
206         }
207 }
208
209 ?>