1: <?php
2:
3: namespace Budabot\Core\Modules;
4:
5: use stdClass;
6:
7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19: class AliasController {
20:
21:
22: public $commandAlias;
23:
24:
25: public $commandManager;
26:
27:
28: public $text;
29:
30: 31: 32: 33:
34: public function setup() {
35:
36: }
37:
38: 39: 40: 41: 42: 43:
44: public function aliasAddCommand($message, $channel, $sender, $sendto, $args) {
45: $alias = strtolower($args[1]);
46: $cmd = $args[2];
47:
48: $alias_obj = new stdClass;
49: $alias_obj->module = '';
50: $alias_obj->cmd = $cmd;
51: $alias_obj->alias = $alias;
52: $alias_obj->status = 1;
53:
54: $commands = $this->commandManager->get($alias);
55: $enabled = false;
56: forEach ($commands as $command) {
57: if ($command->status == 1) {
58: $enabled = true;
59: break;
60: }
61: }
62: $row = $this->commandAlias->get($alias);
63: if ($enabled) {
64: $msg = "Cannot add alias <highlight>{$alias}<end> since there is already an active command with that name.";
65: } else if ($row === null) {
66: $this->commandAlias->add($alias_obj);
67: $this->commandAlias->activate($cmd, $alias);
68: $msg = "Alias <highlight>{$alias}<end> for command <highlight>{$cmd}<end> added successfully.";
69: } else if ($row->status == 0 || ($row->status == 1 && $row->cmd == $cmd)) {
70: $this->commandAlias->update($alias_obj);
71: $this->commandAlias->activate($cmd, $alias);
72: $msg = "Alias <highlight>{$alias}<end> for command <highlight>{$cmd}<end> added successfully.";
73: } else if ($row->status == 1 && $row->cmd != $cmd) {
74: $msg = "Cannot add alias <highlight>{$alias}<end> since an alias with that name already exists.";
75: }
76: $sendto->reply($msg);
77: }
78:
79: 80: 81: 82: 83: 84:
85: public function aliasListCommand($message, $channel, $sender, $sendto, $args) {
86: $paddingSize = 30;
87:
88: $a = $this->padRow("Alias", $paddingSize);
89: $blob = "<header2>{$a}Command<end>\n\n";
90: $count = 0;
91: forEach ($this->commandAlias->getEnabledAliases() as $alias) {
92: if ($count++ % 2 == 0) {
93: $color = "white";
94: } else {
95: $color = "highlight";
96: }
97: $removeLink = $this->text->makeChatcmd('Remove', "/tell <myname> alias rem {$alias->alias}");
98: $a = $this->padRow($alias->alias, $paddingSize);
99: $blob .= "<{$color}>{$a}{$alias->cmd}<end> $removeLink\n";
100: }
101:
102: $msg = $this->text->makeBlob('Alias List', $blob);
103: $sendto->reply($msg);
104: }
105:
106: 107: 108: 109: 110: 111:
112: public function aliasRemCommand($message, $channel, $sender, $sendto, $args) {
113: $alias = strtolower($args[1]);
114:
115: $row = $this->commandAlias->get($alias);
116: if ($row === null || $row->status != 1) {
117: $msg = "Could not find alias <highlight>{$alias}<end>!";
118: } else {
119: $row->status = 0;
120: $this->commandAlias->update($row);
121: $this->commandAlias->deactivate($alias);
122:
123: $msg = "Alias <highlight>{$alias}<end> removed successfully.";
124: }
125: $sendto->reply($msg);
126: }
127:
128: private function padRow($str, $size) {
129: return str_pad($str, $size - strlen($str), ".");
130: }
131: }
132: