changed file headers & added AUTHORS file
[PHP-P10.git] / BotLoader / BotLoader.class.php
1 <?php
2 /******************************* PHP-P10 v2 *****************************
3  * Copyright (C) 2011  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  *  BotLoader/BotLoader.class.php
21  *
22  * This class loades / rehashs or unloads all Bots.
23  *
24  */
25 require_once("Bot.class.php");
26 require_once("saxdb.class.php");
27
28 class BotLoader {
29         const BOT_DIR = "Bots";
30         const TMP_DIR = "tmp";
31         private $uplink;
32         private $saxdb;
33         private $botdb;
34         private $loadedBots = array();
35         private $botDatabases = array();
36         private static $botloader;
37         
38         public function __construct($uplink) {
39                 $this->uplink = $uplink;
40                 $this->saxdb = new saxdb();
41                 $this->saxdb->loadDB("php_p10.db");
42                 $this->botdb = $this->saxdb->getSection("BotLoader");
43                 self::$botloader = $this;
44                 timer(60*10, array($this, "autosave"), array());
45         }
46         
47         public function loadBots() {
48                 if(array_key_exists("bots", $this->botdb) && is_array($this->botdb["bots"])) {
49                         foreach($this->botdb["bots"] as $name => $botfile) {
50                                 $this->loadBot($name, $botfile);
51                         }
52                 } else {
53                         $this->botdb["bots"] = array();
54                 }
55         }
56         
57         public function unloadBots() {
58                 foreach($this->loadedBots as $name => $bot) {
59                         $this->unloadBot($name, false);
60                 }
61         }
62         
63         public function save() {
64                 $this->saxdb->setSection("BotLoader", $this->botdb);
65                 $this->saxdb->writeDB("php_p10.db");
66         }
67         
68         public function autosave() {
69                 foreach($this->loadedBots as $name => $bot) {
70                         if(array_key_exists(strtolower($name), $this->botDatabases)) {
71                                 $db = $bot->writeDB();
72                                 $this->saxdb->setSection($this->botDatabases[strtolower($name)],$db);
73                         }
74                 }
75                 $this->save();
76                 timer(60*10, array($this, "autosave"), array());
77         }
78         
79         public function loop() {
80                 foreach($this->loadedBots as $name => $bot) {
81                         $bot->loop();
82                 }
83         }
84         
85         private function loadBot($name, $botfile) {
86                 if(array_key_exists(strtolower($name), $this->loadedBots)) return false;
87                 //load bot
88                 $bot = $this->loadClass($botfile, $name);
89                 if(!$bot) return false;
90                 $bot->load($this->uplink);
91                 $this->loadedBots[strtolower($name)] = $bot;
92                 if(!array_key_exists($name, $this->botdb["bots"])) {
93                         $this->botdb["bots"][$name] = $botfile;
94                 }
95                 return true;
96         }
97         
98         private function unloadBot($name, $delete = true) {
99                 if(!(array_key_exists(strtolower($name), $this->loadedBots))) return false;
100                 //unload bot
101                 $bot = $this->loadedBots[strtolower($name)];
102                 if(array_key_exists(strtolower($name), $this->botDatabases)) {
103                         $db = $bot->writeDB();
104                         $this->saxdb->setSection($this->botDatabases[strtolower($name)],$db);
105                         unset($this->botDatabases[strtolower($name)]);
106                 }
107                 ModCMD::unbindBot($bot);
108                 $bot->unload(false);
109                 unset($this->loadedBots[strtolower($name)]);
110                 if(array_key_exists($name, $this->botdb["bots"]) && $delete) {
111                         unset($this->botdb["bots"][$name]);
112                 }
113                 return true;
114         }
115         
116         private function rehashBot($name) {
117                 if(!(array_key_exists(strtolower($name), $this->loadedBots))) return false;
118                 $botfile = null;
119                 foreach($this->botdb["bots"] as $botname => $file) {
120                         if(strtolower($botname) == strtolower($name)) {
121                                 $botfile = $file;
122                                 break;
123                         }
124                 }
125                 if(!$botfile) break;
126                 //rehash bot
127                 $bot = $this->loadedBots[strtolower($name)];
128                 if(array_key_exists(strtolower($name), $this->botDatabases)) {
129                         $db = $bot->writeDB();
130                         $this->saxdb->setSection($this->botDatabases[strtolower($name)],$db);
131                         unset($this->botDatabases[strtolower($name)]);
132                 }
133                 ModCMD::unbindBot($bot);
134                 $data = $bot->unload(true);
135                 unset($this->loadedBots[strtolower($name)]);
136                 //ok  bot is unloaded... load it again...
137                 $bot = $this->loadClass($botfile, $name);
138                 if(!$bot) return false;
139                 $bot->load($this->uplink, $data);
140                 $this->loadedBots[strtolower($name)] = $bot;
141                 return true;
142         }
143         
144         private function listLoadedBots() {
145                 return $this->loadedBots;
146         }
147         
148         private function addDBsection($bot, $section) {
149                 if(!is_a($bot, "Bot")) return false;
150                 $name = null;
151                 foreach($this->loadedBots as $botname => $cbot) {
152                         if($cbot === $bot) {
153                                 $name = $botname;
154                                 break;
155                         }
156                 }
157                 if($name == null) return false;
158                 if(array_key_exists(strtolower($name), $this->botDatabases)) {
159                         return false;
160                 }
161                 $this->botDatabases[strtolower($name)] = $section;
162                 $bot->readDB($this->saxdb->getSection($section));
163         }
164         
165         private function loadClass($file, $classprefix) {
166                 $dir = self::BOT_DIR;
167                 $tmp = self::TMP_DIR;
168                 $maincode=file_get_contents($dir."/".$file);
169                 if(!$maincode) return;
170                 $class = rand(10000,99999);
171                 while(class_exists("bot_".$classprefix."_".$class)) {
172                         $class = rand(10000,99999);
173                 }
174                 $maincode = str_replace('{$_NAME}', "module_".$classprefix."_".$class, $maincode);
175                 $fp = fopen($tmp."/modules_".$classprefix."_".$class.".tmp.php", 'w');
176                 fwrite($fp, $maincode);
177                 fclose($fp);
178                 include($tmp."/modules_".$classprefix."_".$class.".tmp.php");
179                 $classname = "module_".$classprefix."_".$class;
180                 $newclass = new $classname();
181                 unlink($tmp."/modules_".$classprefix."_".$class.".tmp.php");
182                 return $newclass;
183         }
184         
185         public static function load($name, $botfile) {
186                 return self::$botloader->loadBot($name, $botfile);
187         }
188         
189         public static function unload($name) {
190                 return self::$botloader->unloadBot($name);
191         }
192         
193         public static function rehash($name) {
194                 return self::$botloader->rehashBot($name);
195         }
196         
197         public static function listBots() {
198                 return self::$botloader->listLoadedBots();
199         }
200         
201         public static function registerDB($bot, $name) {
202                 return self::$botloader->addDBsection($bot, $name);
203         }
204 }
205
206 ?>