commit | author | age
|
8bd4d9
|
1 |
<?php |
JK |
2 |
/** |
|
3 |
* Klasa ładująca dla modułów napisanych dla poprzedniej wersji bota |
|
4 |
*/ |
|
5 |
class bot_legacy_init implements BotModuleInit { |
|
6 |
const BOT_FUNCTIONS_FILE = '/cache/legacy.functions'; |
|
7 |
const BOT_HELP_FILE = '/cache/legacy.help'; |
|
8 |
|
|
9 |
/** |
|
10 |
* Wypisuje listę komend obsługiwanych przez dany plik modułu |
|
11 |
* @param string $file Nazwa pliku |
|
12 |
* @return array |
|
13 |
*/ |
|
14 |
function load_funcs($file) { |
|
15 |
$name = substr(basename($file), 3, -4); |
|
16 |
|
|
17 |
$ret = @include_once($file); |
|
18 |
if(!$ret) return array(); |
|
19 |
|
|
20 |
$funcs = call_user_func(array($name, 'register_cmd')); |
|
21 |
|
|
22 |
$return = array(); |
|
23 |
|
|
24 |
foreach($funcs as $n => $method) { |
|
25 |
if(!isset($return[$n])) { |
|
26 |
$return[$n] = array(); |
|
27 |
} |
|
28 |
|
|
29 |
$return[$n][] = array( |
|
30 |
'file' => 'handler.php', |
|
31 |
'class' => 'bot_legacy_module', |
|
32 |
'method' => 'handle', |
|
33 |
'params' => array(basename($file), $name, $method) |
|
34 |
); |
|
35 |
} |
|
36 |
|
|
37 |
return $return; |
|
38 |
} |
|
39 |
|
|
40 |
/** |
|
41 |
* Zwraca (z użyciem cache'a) listę obsługiwanych komend ze starych modułów |
|
42 |
* @return array |
|
43 |
*/ |
|
44 |
function register() { |
|
45 |
if(is_file(BOT_TOPDIR.self::BOT_FUNCTIONS_FILE)) { |
|
46 |
return unserialize(file_get_contents(BOT_TOPDIR.self::BOT_FUNCTIONS_FILE)); |
|
47 |
} |
|
48 |
else |
|
49 |
{ |
|
50 |
$return = array(); |
|
51 |
$files = glob('./modules/*.php'); |
|
52 |
|
|
53 |
foreach($files as $file) { |
|
54 |
$return = array_merge_recursive($return, $this->load_funcs($file)); |
|
55 |
} |
|
56 |
|
|
57 |
file_put_contents(BOT_TOPDIR.self::BOT_FUNCTIONS_FILE, serialize($return)); |
|
58 |
|
|
59 |
return $return; |
|
60 |
} |
|
61 |
} |
|
62 |
|
|
63 |
/** |
|
64 |
* Ładuje skróconą listę poleceń i ich parametrów z pliku starego modułu |
|
65 |
* @param string $file Nazwa pliku |
|
66 |
*/ |
|
67 |
function load_help($file) { |
|
68 |
$name = substr(basename($file), 3, -4); |
|
69 |
|
|
70 |
$ret = @include_once($file); |
|
71 |
if(!$ret) return array(); |
|
72 |
|
|
73 |
call_user_func(array($name, 'help'), NULL); |
|
74 |
} |
|
75 |
|
|
76 |
/** |
|
77 |
* Zwraca (z użyciem cache'a) skróconą pomoc dla obsługiwanych poleceń |
|
78 |
* @return BotMsg |
|
79 |
*/ |
|
80 |
function cache_help() { |
|
81 |
if(is_file(BOT_TOPDIR.self::BOT_HELP_FILE)) { |
|
82 |
return unserialize(file_get_contents(BOT_TOPDIR.self::BOT_HELP_FILE)); |
|
83 |
} |
|
84 |
else |
|
85 |
{ |
|
86 |
$files = glob(BOT_TOPDIR.'/modules/*.php'); |
|
87 |
|
|
88 |
foreach($files as $file) { |
|
89 |
$this->load_help($file); |
|
90 |
} |
|
91 |
|
|
92 |
$return = GGapi::getResponse(); |
|
93 |
|
|
94 |
file_put_contents(BOT_TOPDIR.self::BOT_HELP_FILE, serialize($return)); |
|
95 |
|
|
96 |
return $return; |
|
97 |
} |
|
98 |
} |
|
99 |
|
|
100 |
/** |
|
101 |
* Zwraca pomoc dla określonej parametrem komendy |
|
102 |
* @param null|string $params Nazwa komendy |
|
103 |
* @return false|BotMsg Zwracana wiadomość |
|
104 |
*/ |
|
105 |
function help($params = NULL) { |
|
106 |
if($params === NULL) { |
|
107 |
return $this->cache_help(); |
|
108 |
} |
|
109 |
else |
|
110 |
{ |
|
111 |
$data = $this->register(); |
|
112 |
|
|
113 |
if(!$data[$params]) { |
|
114 |
return FALSE; |
|
115 |
} |
|
116 |
|
|
117 |
foreach($data[$params] as $module) { |
|
118 |
$ret = @include_once(BOT_TOPDIR.'/modules/'.$module['params'][0]); |
|
119 |
if(!$ret) continue; |
|
120 |
|
|
121 |
call_user_func(array($module['params'][1], 'help'), $params); |
|
122 |
} |
|
123 |
|
|
124 |
$data = GGapi::getResponse(); |
|
125 |
if($data instanceof BotMsg) { |
|
126 |
return $data; |
|
127 |
} |
|
128 |
else |
|
129 |
{ |
|
130 |
return FALSE; |
|
131 |
} |
|
132 |
} |
|
133 |
} |
|
134 |
} |
|
135 |
|
|
136 |
return 'bot_legacy_init'; |
|
137 |
?> |