Jacek Kowalski
2016-02-13 e722ff11af326f17abaf914581a340a5799ed9fa
commit | author | age
8bd4d9 1 <?php
JK 2 class bot_help_module implements BotModule {
3     const BOT_HELP_FILE = '/cache/help.cache';
4     
5     private $PDO;
6     
7     private function getHelp() {
8         if(is_file(BOT_TOPDIR.self::BOT_HELP_FILE)) {
9             return unserialize(file_get_contents(BOT_TOPDIR.self::BOT_HELP_FILE));
10         }
11         
12         $st = $this->PDO->prepare('SELECT DISTINCT dir, init FROM functions ORDER BY dir ASC');
13         $st->execute();
14         $data = $st->fetchAll();
15         
16         $help = new BotMsg();
17         
18         foreach($data as $element) {
19             $dir = $element['dir'];
20             
21             if(!is_file(BOT_TOPDIR.$dir.'/init.php')) {
22                 continue;
23             }
24             
25             $class = include_once(BOT_TOPDIR.$dir.'/init.php');
26             if($class === TRUE) {
27                 $class = $element['init'];
28             }
29             
30             if(!$class || !class_exists($class, FALSE)) {
31                 continue;
32             }
33             
34             $init = new $class;
35             if(!($init instanceof BotModuleInit)) {
36                 continue;
37             }
38             
39             $row = $init->help();
40             if(!($row instanceof BotMsg)) {
41                 continue;
42             }
43             
44             $help->append($row);
45         }
46         
47         $help->append('Objaśnienie:<br />'."\n"
48             . '   <i>argument</i> jest wymagany<br />'."\n"
49             . '   <i>[argument]</i> jest opcjonalny');
50         
51         file_put_contents(BOT_TOPDIR.self::BOT_HELP_FILE, serialize($help));
52         
53         return $help;
54     }
55     
56     function handle($msg, $params) {
57         try {
58             try {
59                 $this->PDO = new PDO('sqlite:'.BOT_TOPDIR.'/cache/functions.sqlite');
60                 $this->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
61                 $this->PDO->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_TO_STRING);
62             }
63             catch(Exception $e) {
64                 @unlink(BOT_TOPDIR.'/cache/functions.sqlite');
65                 throw $e;
66             }
67             
68             $args = trim($msg->args);
69             
70             if(empty($args)) {
71                 return $this->getHelp();
72             }
73             
74             $args = strtok($args, " \t\n\r\0\x0B");
75             
76             $st = $this->PDO->prepare('SELECT dir, init FROM functions WHERE name=? ORDER BY priority ASC');
77             $st->execute(array($args));
78             $data1 = $st->fetchAll();
79             $st->execute(array('*'));
80             $data2 = $st->fetchAll();
81             
82             $data = array_merge($data1, $data2);
83             unset($data1, $data2);
84             
85             foreach($data as $func) {
86                 if(!is_file(BOT_TOPDIR.$func['dir'].'/init.php')) {
87                     continue;
88                 }
89                 
90                 $class = require_once(BOT_TOPDIR.$func['dir'].'/init.php');
91                 if($class === TRUE) {
92                     $class = $func['init'];
93                 }
94                 
95                 if(!$class || !class_exists($class, FALSE)) {
96                     continue;
97                 }
98             
99                 $init = new $class;
100                 if(!($init instanceof BotModuleInit)) {
101                     continue;
102                 }
103                 
104                 $help = $init->help($args);
105                 if($help instanceof BotMsg) {
106                     return $help;
107                 }
108             }
109         }
110         catch(Exception $e) {
111             return new BotMsg('Wystąpił błąd podczas pobierania pomocy dla polecenia. Komunikat:<br />'.nl2br($e));
112         }
113     }
114 }
115 ?>