1: <?php
2:
3: namespace Budabot\Core;
4:
5: 6: 7:
8: class CommandAlias {
9:
10:
11: public $db;
12:
13:
14: public $chatBot;
15:
16:
17: public $commandManager;
18:
19:
20: public $logger;
21:
22: const ALIAS_HANDLER = "CommandAlias.process";
23:
24: 25: 26: 27:
28: public function load() {
29: $this->logger->log('DEBUG', "Loading enabled command aliases");
30:
31: $data = $this->db->query("SELECT cmd, alias FROM cmd_alias_<myname> WHERE status = 1");
32: forEach ($data as $row) {
33: $this->activate($row->cmd, $row->alias);
34: }
35: }
36:
37: 38: 39: 40:
41: public function register($module, $command, $alias, $status = 1) {
42: $module = strtoupper($module);
43: $command = strtolower($command);
44: $alias = strtolower($alias);
45:
46: $this->logger->log('DEBUG', "Registering alias: '{$alias}' for command: '$command'");
47:
48: $row = $this->get($alias);
49: if ($row !== null) {
50:
51: if (!empty($row->module)) {
52: $sql = "UPDATE cmd_alias_<myname> SET module = ?, cmd = ? WHERE alias = ?";
53: $this->db->exec($sql, $module, $command, $alias);
54: }
55: } else {
56: $sql = "INSERT INTO cmd_alias_<myname> (module, cmd, alias, status) VALUES (?, ?, ?, ?)";
57: $this->db->exec($sql, $module, $command, $alias, $status);
58: }
59: }
60:
61: 62: 63: 64:
65: public function activate($command, $alias) {
66: $alias = strtolower($alias);
67:
68: $this->logger->log('DEBUG', "Activate Command Alias command:($command) alias:($alias)");
69:
70: $this->commandManager->activate('msg', self::ALIAS_HANDLER, $alias, 'all');
71: $this->commandManager->activate('priv', self::ALIAS_HANDLER, $alias, 'all');
72: $this->commandManager->activate('guild', self::ALIAS_HANDLER, $alias, 'all');
73: }
74:
75: 76: 77: 78:
79: public function deactivate($alias) {
80: $alias = strtolower($alias);
81:
82: $this->logger->log('DEBUG', "Deactivate Command Alias:($alias)");
83:
84: $this->commandManager->deactivate('msg', self::ALIAS_HANDLER, $alias);
85: $this->commandManager->deactivate('priv', self::ALIAS_HANDLER, $alias);
86: $this->commandManager->deactivate('guild', self::ALIAS_HANDLER, $alias);
87: }
88:
89: public function process($message, $channel, $sender, CommandReply $sendto) {
90: list($alias, $params) = explode(' ', $message, 2);
91: $alias = strtolower($alias);
92:
93:
94: $row = $this->get($alias);
95: if ($row === null) {
96: return false;
97: }
98:
99: $this->logger->log('DEBUG', "Command alias found command: '{$row->cmd}' alias: '{$row->alias}'");
100: $cmd = $row->cmd;
101: if ($params) {
102:
103:
104:
105:
106:
107:
108: preg_match_all("/{\\d+}/", $cmd, $matches);
109: $numMatches = count(array_unique($matches[0]));
110: if ($numMatches == 0) {
111: $cmd .= " {0}";
112: }
113:
114: $aliasParams = explode(' ', $params, $numMatches);
115:
116:
117: array_unshift($aliasParams, $params);
118:
119:
120: for ($i = 0; $i < count($aliasParams); $i++) {
121: $cmd = str_replace('{' . $i . '}', $aliasParams[$i], $cmd);
122: }
123: }
124:
125: if (preg_match("/{\\d+}/", $cmd)) {
126: return false;
127: } else {
128: $this->commandManager->process($channel, $cmd, $sender, $sendto);
129: }
130: }
131:
132: 133: 134: 135:
136: public function add($row) {
137: $this->logger->log('DEBUG', "Adding alias: '{$alias}' for command: '$command'");
138:
139: $sql = "INSERT INTO cmd_alias_<myname> (module, cmd, alias, status) VALUES (?, ?, ?, ?)";
140: return $this->db->exec($sql, $row->module, $row->cmd, $row->alias, $row->status);
141: }
142:
143: 144: 145: 146:
147: public function update($row) {
148: $this->logger->log('DEBUG', "Updating alias :($row->alias)");
149:
150: $sql = "UPDATE cmd_alias_<myname> SET module = ?, cmd = ?, status = ? WHERE alias = ?";
151: return $this->db->exec($sql, $row->module, $row->cmd, $row->status, $row->alias);
152: }
153:
154: public function get($alias) {
155: $alias = strtolower($alias);
156:
157: $sql = "SELECT cmd, alias, module, status FROM cmd_alias_<myname> WHERE alias = ?";
158: return $this->db->queryRow($sql, $alias);
159: }
160:
161: public function getBaseCommandForAlias($alias) {
162: $row = $this->get($alias);
163:
164:
165: if ($row === null || $row->status != 1) {
166: return null;
167: }
168: list($cmd) = explode(' ', $row->cmd, 2);
169: return $cmd;
170: }
171:
172: public function findAliasesByCommand($command) {
173: $sql = "SELECT cmd, alias, module, status FROM cmd_alias_<myname> WHERE cmd LIKE ?";
174: return $this->db->query($sql, $command);
175: }
176:
177: public function getEnabledAliases() {
178: return $this->db->query("SELECT cmd, alias, module, status FROM cmd_alias_<myname> WHERE status = 1 ORDER BY alias ASC");
179: }
180: }
181: