Merge branch 'master' of ssh://git.pk910.de:16110/PHP-P10
[PHP-P10.git] / BotLoader / BotLoader.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  *  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($bot->getDBSection()) {
71                                 $db = $bot->writeDB();
72                                 $this->saxdb->setSection($bot->getDBSection(),$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($bot->getDBSection()) {
103                         $db = $bot->writeDB();
104                         $this->saxdb->setSection($bot->getDBSection(),$db);
105                 }
106                 ModCMD::unbindBot($bot);
107                 $bot->unload(false);
108                 unset($this->loadedBots[strtolower($name)]);
109                 if(array_key_exists($name, $this->botdb["bots"]) && $delete) {
110                         unset($this->botdb["bots"][$name]);
111                 }
112                 return true;
113         }
114
115         private function rehashBot($name) {
116                 if(!(array_key_exists(strtolower($name), $this->loadedBots))) return false;
117                 $botfile = null;
118                 foreach($this->botdb["bots"] as $botname => $file) {
119                         if(strtolower($botname) == strtolower($name)) {
120                                 $botfile = $file;
121                                 break;
122                         }
123                 }
124                 if(!$botfile) break;
125                 //rehash bot
126                 $bot = $this->loadedBots[strtolower($name)];
127                 if($bot->getDBSection()) {
128                         $db = $bot->writeDB();
129                         $this->saxdb->setSection($bot->getDBSection(),$db);
130                 }
131                 ModCMD::unbindBot($bot);
132                 $data = $bot->unload(true);
133                 unset($this->loadedBots[strtolower($name)]);
134                 //ok  bot is unloaded... load it again...
135                 $bot = $this->loadClass($botfile, $name);
136                 if(!$bot) return false;
137                 $bot->load($this->uplink, $data);
138                 $this->loadedBots[strtolower($name)] = $bot;
139                 return true;
140         }
141
142         private function listLoadedBots() {
143                 return $this->loadedBots;
144         }
145
146         private function addDBsection($bot, $section) {
147                 if(!is_a($bot, "Bot")) return false;
148                 $bot->setDBSection($section);
149                 $bot->readDB($this->saxdb->getSection($section));
150         }
151
152         private function loadClass($file, $classprefix) {
153                 $dir = self::BOT_DIR;
154                 $tmp = self::TMP_DIR;
155                 $maincode=file_get_contents($dir."/".$file);
156                 if(!$maincode) return;
157                 $class = rand(10000,99999);
158                 while(class_exists("bot_".$classprefix."_".$class)) {
159                         $class = rand(10000,99999);
160                 }
161                 $maincode = str_replace('{$_NAME}', "module_".$classprefix."_".$class, $maincode);
162                 $fp = fopen($tmp."/modules_".$classprefix."_".$class.".tmp.php", 'w');
163                 fwrite($fp, $maincode);
164                 fclose($fp);
165                 include($tmp."/modules_".$classprefix."_".$class.".tmp.php");
166                 $classname = "module_".$classprefix."_".$class;
167                 $newclass = new $classname();
168                 unlink($tmp."/modules_".$classprefix."_".$class.".tmp.php");
169                 return $newclass;
170         }
171
172         public static function load($name, $botfile) {
173                 return self::$botloader->loadBot($name, $botfile);
174         }
175
176         public static function unload($name) {
177                 return self::$botloader->unloadBot($name);
178         }
179
180         public static function rehash($name) {
181                 return self::$botloader->rehashBot($name);
182         }
183
184         public static function listBots() {
185                 return self::$botloader->listLoadedBots();
186         }
187
188         public static function registerDB($bot, $name) {
189                 return self::$botloader->addDBsection($bot, $name);
190         }
191 }
192
193 ?>