1: <?php
2:
3: namespace Budabot\User\Modules;
4:
5: use Budabot\Core\AutoInject;
6: use ReflectionAnnotatedMethod;
7: use Budabot\Core\Registry;
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: class DevController extends AutoInject {
60:
61: 62: 63: 64:
65: public $moduleName;
66:
67: 68: 69:
70: public function setup() {
71: $this->commandAlias->register($this->moduleName, "querysql select", "select");
72: }
73:
74: 75: 76: 77:
78: public function showcmdregexCommand($message, $channel, $sender, $sendto, $args) {
79: $cmd = $args[1];
80:
81:
82: $handlers = $this->getAllCommandHandlers($cmd, $channel);
83:
84:
85: $accessManager = $this->accessManager;
86: $handlers = array_filter($handlers, function ($handler) use ($sender, $accessManager) {
87: return $accessManager->checkAccess($sender, $handler->admin);
88: });
89:
90:
91: $calls = array_reduce($handlers, function ($handlers, $handler) {
92: return array_merge($handlers, explode(',', $handler->file));
93: }, array());
94:
95:
96: $regexes = array();
97: forEach ($calls as $call) {
98: list($name, $method) = explode(".", $call);
99: $instance = Registry::getInstance($name);
100: try {
101: $reflectedMethod = new ReflectionAnnotatedMethod($instance, $method);
102: $regexes = array_merge($regexes, $this->commandManager->retrieveRegexes($reflectedMethod));
103: } catch (ReflectionException $e) {
104: continue;
105: }
106: }
107:
108: $count = count($regexes);
109: if ($count > 0) {
110: $blob = '';
111: forEach ($regexes as $regex) {
112: $blob .= $regex . "\n";
113: }
114: $msg = $this->text->makeBlob("Regexes for $cmd ($count)", $blob);
115: } else {
116: $msg = "No regexes found for command <highlight>$cmd<end>.";
117: }
118: $sendto->reply($msg);
119: }
120:
121: public function getAllCommandHandlers($cmd, $channel) {
122: $handlers = array();
123: if (isset($this->commandManager->commands[$channel][$cmd])) {
124: $handlers []= $this->commandManager->commands[$channel][$cmd];
125: }
126: if (isset($this->subcommandManager->subcommands[$cmd])) {
127: forEach ($this->subcommandManager->subcommands[$cmd] as $handler) {
128: if ($handler->type == $channel) {
129: $handlers []= $handler;
130: }
131: }
132: }
133: return $handlers;
134: }
135:
136: 137: 138: 139:
140: public function intransactionCommand($message, $channel, $sender, $sendto, $args) {
141: if ($this->db->inTransaction()) {
142: $msg = "There is an active transaction.";
143: } else {
144: $msg = "There is no active transaction.";
145: }
146: $sendto->reply($msg);
147: }
148:
149: 150: 151: 152:
153: public function rollbacktransactionCommand($message, $channel, $sender, $sendto, $args) {
154: $this->db->rollback();
155:
156: $msg = "The active transaction has been rolled back.";
157: $sendto->reply($msg);
158: }
159:
160: 161: 162: 163:
164: public function stacktraceCommand($message, $channel, $sender, $sendto, $args) {
165: $stacktrace = $this->util->getStackTrace();
166: $count = substr_count($stacktrace, "\n");
167: $msg = $this->text->makeBlob("Current Stacktrace ($count)", $stacktrace);
168: $sendto->reply($msg);
169: }
170:
171: 172: 173: 174:
175: public function cmdhandlersCommand($message, $channel, $sender, $sendto, $args) {
176: $cmdArray = explode(" ", $args[1], 2);
177: $cmd = $cmdArray[0];
178:
179: $blob = '';
180:
181:
182: forEach ($this->commandManager->commands as $channelName => $channel) {
183: if (isset($channel[$cmd])) {
184: $blob .= "<header2>$channelName ($cmd)<end>\n";
185: $blob .= $channel[$cmd]->file . "\n\n";
186: }
187: }
188:
189:
190: forEach ($this->subcommandManager->subcommands[$cmd] as $row) {
191: $blob .= "<header2>$row->type ($row->cmd)<end>\n";
192: $blob .= $row->file . "\n\n";
193: }
194:
195: $msg = $this->text->makeBlob("Command Handlers for '$cmd'", $blob);
196:
197: $sendto->reply($msg);
198: }
199:
200: 201: 202: 203:
204: public function makeitemCommand($message, $channel, $sender, $sendto, $args) {
205: $lowId = $args[1];
206: $highId = $args[2];
207: $ql = $args[3];
208: $name = $args[4];
209: $sendto->reply($this->text->makeItem($lowId, $highId, $ql, $name));
210: }
211:
212: 213: 214: 215: 216:
217: public function createblobCommand($message, $channel, $sender, $sendto, $args) {
218: $length = $args[1];
219: if (isset($args[2])) {
220: $numBlobs = $args[2];
221: } else {
222: $numBlobs = 1;
223: }
224:
225: for ($i = 0; $i < $numBlobs; $i++) {
226: $blob = $this->randString($length);
227: $msg = $this->text->makeBlob("Blob $i", $blob);
228: $sendto->reply($msg);
229: }
230: }
231:
232: public function randString($length, $charset='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 \n') {
233: $str = '';
234: $count = strlen($charset);
235: while ($length--) {
236: $str .= $charset[mt_rand(0, $count-1)];
237: }
238: return $str;
239: }
240: }
241: