1: <?php
2:
3: namespace Budabot\Core\Modules;
4:
5: use Budabot\Core\PrivateMessageCommandReply;
6:
7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59:
60: class SystemController {
61:
62: 63: 64: 65:
66: public $moduleName;
67:
68:
69: public $accessManager;
70:
71:
72: public $adminManager;
73:
74:
75: public $chatBot;
76:
77:
78: public $db;
79:
80:
81: public $commandManager;
82:
83:
84: public $eventManager;
85:
86:
87: public $commandAlias;
88:
89:
90: public $subcommandManager;
91:
92:
93: public $helpManager;
94:
95:
96: public $buddylistManager;
97:
98:
99: public $settingManager;
100:
101:
102: public $text;
103:
104:
105: public $util;
106:
107:
108: public $logger;
109:
110: 111: 112: 113: 114: 115: 116: 117:
118: public $defaultSymbol = "!";
119:
120: 121: 122: 123: 124: 125: 126: 127: 128:
129: public $defaultMaxBlobSize = "7500";
130:
131: 132: 133: 134: 135: 136: 137: 138:
139: public $defaultHttpTimeout = "10s";
140:
141: 142: 143: 144: 145: 146: 147: 148: 149:
150: public $defaultGuildChannelStatus = "1";
151:
152: 153: 154: 155: 156: 157: 158: 159: 160:
161: public $defaultGuildChannelCmdFeedback = "1";
162:
163: 164: 165: 166: 167: 168: 169: 170: 171:
172: public $defaultPrivateChannelCmdFeedback = "1";
173:
174: 175: 176: 177: 178: 179: 180:
181: public $defaultVersion = "0";
182:
183: 184: 185: 186: 187: 188: 189: 190: 191:
192: public $defaultAccessDeniedNotifyGuild = "1";
193:
194: 195: 196: 197: 198: 199: 200: 201: 202:
203: public $defaultAccessDeniedNotifyPriv = "1";
204:
205: 206: 207: 208:
209: public function setup() {
210: global $version;
211: $this->settingManager->save('version', $version);
212:
213: $this->helpManager->register($this->moduleName, "budatime", "budatime.txt", "all", "Format for budatime");
214:
215: $name = $this->chatBot->vars['name'];
216: $this->settingManager->add($this->moduleName, "default_private_channel", "Private channel to process commands from", "edit", "text", $name, $name);
217: }
218:
219: 220: 221: 222:
223: public function restartCommand($message, $channel, $sender, $sendto, $args) {
224: $msg = "Bot is restarting.";
225: $this->chatBot->sendTell($msg, $sender);
226: $this->chatBot->sendPrivate($msg, true);
227: $this->chatBot->sendGuild($msg, true);
228:
229: $this->chatBot->disconnect();
230: $this->logger->log('INFO', "The Bot is restarting.");
231: exit(-1);
232: }
233:
234: 235: 236: 237:
238: public function shutdownCommand($message, $channel, $sender, $sendto, $args) {
239: $msg = "The Bot is shutting down.";
240: $this->chatBot->sendTell($msg, $sender);
241: $this->chatBot->sendPrivate($msg, true);
242: $this->chatBot->sendGuild($msg, true);
243:
244: $this->chatBot->disconnect();
245: $this->logger->log('INFO', "The Bot is shutting down.");
246: exit(10);
247: }
248:
249: 250: 251: 252:
253: public function systemCommand($message, $channel, $sender, $sendto, $args) {
254: global $version;
255:
256: $sql = "SELECT count(*) AS count FROM players";
257: $row = $this->db->queryRow($sql);
258: $num_player_cache = $row->count;
259:
260: $num_buddylist = 0;
261: forEach ($this->buddylistManager->buddyList as $key => $value) {
262: if (!isset($value['name'])) {
263:
264: continue;
265: }
266:
267: $num_buddylist++;
268: }
269:
270: $blob = "Name: <highlight><myname><end>\n";
271: $blob .= "SuperAdmin: <highlight>'{$this->chatBot->vars['SuperAdmin']}'<end>\n";
272: $blob .= "Guild: <highlight>'<myguild>' (" . $this->chatBot->vars['my_guild_id'] . ")<end>\n\n";
273:
274: $blob .= "Budabot: <highlight>$version<end>\n";
275: $blob .= "PHP: <highlight>" . phpversion() . "<end>\n";
276: $blob .= "OS: <highlight>" . php_uname('s') . ' ' . php_uname('r') . ' ' . php_uname('m') . "<end>\n";
277: $blob .= "Database: <highlight>" . $this->db->getType() . "<end>\n\n";
278:
279: $blob .= "Current Memory Usage: <highlight>" . $this->util->bytesConvert(memory_get_usage()) . "<end>\n";
280: $blob .= "Current Memory Usage (Real): <highlight>" . $this->util->bytesConvert(memory_get_usage(1)) . "<end>\n";
281: if (version_compare(PHP_VERSION, '5.2.0', '>=')) {
282: $blob .= "Peak Memory Usage: <highlight>" . $this->util->bytesConvert(memory_get_peak_usage()) . "<end>\n";
283: $blob .= "Peak Memory Usage (Real): <highlight>" . $this->util->bytesConvert(memory_get_peak_usage(1)) . "<end>\n\n";
284: }
285:
286: $blob .= "Using Chat Proxy: <highlight>" . ($this->chatBot->vars['use_proxy'] == 1 ? "enabled" : "disabled") . "<end>\n";
287:
288: $date_string = $this->util->unixtimeToReadable(time() - $this->chatBot->vars['startup']);
289: $blob .= "Uptime: <highlight>$date_string<end>\n\n";
290:
291: $eventnum = 0;
292: forEach ($this->eventManager->events as $type => $events) {
293: $eventnum += count($events);
294: }
295:
296: $numAliases = count($this->commandAlias->getEnabledAliases());
297:
298: $blob .= "Active tell commands: <highlight>" . (count($this->commandManager->commands['msg']) - $numAliases) . "<end>\n";
299: $blob .= "Active private channel commands: <highlight>" . (count($this->commandManager->commands['priv']) - $numAliases) . "<end>\n";
300: $blob .= "Active guild channel commands: <highlight>" . (count($this->commandManager->commands['guild']) - $numAliases) . "<end>\n";
301: $blob .= "Active subcommands: <highlight>" . count($this->subcommandManager->subcommands) . "<end>\n";
302: $blob .= "Active command aliases: <highlight>" . $numAliases . "<end>\n";
303: $blob .= "Active events: <highlight>" . $eventnum . "<end>\n";
304: $blob .= "Active help commands: <highlight>" . count($this->helpManager->getAllHelpTopics(null)) . "<end>\n\n";
305:
306: $blob .= "Characters on the buddy list: <highlight>$num_buddylist / " . count($this->buddylistManager->buddyList) . "<end>\n";
307: $blob .= "Maximum buddy list size: <highlight>" . $this->chatBot->getBuddyListSize() . "<end>\n";
308: $blob .= "Characters in the private channel: <highlight>" . count($this->chatBot->chatlist) . "<end>\n";
309: $blob .= "Guild members: <highlight>" . count($this->chatBot->guildmembers) . "<end>\n";
310: $blob .= "Character infos in cache: <highlight>" . $num_player_cache . "<end>\n";
311: $blob .= "Messages in the chat queue: <highlight>" . count($this->chatBot->chatqueue->queue) . "<end>\n\n";
312:
313: $blob .= "Public Channels:\n";
314: forEach ($this->chatBot->grp as $gid => $status) {
315: $string = unpack("N", substr($gid, 1));
316: $blob .= "<tab><highlight>'{$this->chatBot->gid[$gid]}'<end> (" . ord(substr($gid, 0, 1)) . " " . $string[1] . ")\n";
317: }
318:
319: $msg = $this->text->makeBlob('System Info', $blob);
320: $sendto->reply($msg);
321: }
322:
323: 324: 325: 326: 327:
328: public function checkaccessCommand($message, $channel, $sender, $sendto, $args) {
329: if (isset($args[1])) {
330: $name = ucfirst(strtolower($args[1]));
331: if (!$this->chatBot->get_uid($name)) {
332: $sendto->reply("Character <highlight>{$name}<end> does not exist.");
333: return;
334: }
335: } else {
336: $name = $sender;
337: }
338:
339: $accessLevel = $this->accessManager->getDisplayName($this->accessManager->getAccessLevelForCharacter($name));
340:
341: $msg = "Access level for $name is <highlight>$accessLevel<end>.";
342: $sendto->reply($msg);
343: }
344:
345: 346: 347: 348: 349:
350: public function clearqueueCommand($message, $channel, $sender, $sendto, $args) {
351: $num = 0;
352: forEach ($this->chatBot->chatqueue->queue as $priority) {
353: $num += count($priority);
354: }
355: $this->chatBot->chatqueue->queue = array();
356:
357: $sendto->reply("Chat queue has been cleared of $num messages.");
358: }
359:
360: 361: 362: 363: 364: 365:
366: public function macroCommand($message, $channel, $sender, $sendto, $args) {
367: $commands = explode("|", $args[1]);
368: forEach ($commands as $commandString) {
369: $this->commandManager->process($channel, $commandString, $sender, $sendto);
370: }
371: }
372:
373: 374: 375: 376: 377:
378: public function refreshMySQLConnectionEvent($eventObj) {
379:
380: $this->logger->log('DEBUG', "Pinging database");
381: $sql = "SELECT * FROM settings_<myname>";
382: $this->db->query($sql);
383: }
384:
385: 386: 387: 388: 389:
390: public function onConnectEvent($eventObj) {
391:
392: forEach ($this->adminManager->admins as $name => $info) {
393: if ($info["level"] == 4 && $this->buddylistManager->isOnline($name) == 1) {
394: $this->chatBot->sendTell("<myname> is <green>online<end>. For updates or help use the Budabot Forums <highlight>http://budabot.com<end>", $name);
395: }
396: }
397:
398: global $version;
399: $msg = "Budabot <highlight>$version<end> now <green>online<end>.";
400:
401:
402: $this->chatBot->sendGuild($msg, true);
403: $this->chatBot->sendPrivate($msg, true);
404: }
405:
406: 407: 408: 409:
410: public function showCommandCommand($message, $channel, $sender, $sendto, $args) {
411: $name = ucfirst(strtolower($args[1]));
412: $cmd = $args[2];
413: $type = "msg";
414:
415: $showSendto = new PrivateMessageCommandReply($this->chatBot, $name);
416: $this->commandManager->process($type, $cmd, $sender, $showSendto);
417:
418: $sendto->reply("Command <highlight>$cmd<end> has been sent to <highlight>$name<end>.");
419: }
420: }
421: