1: <?php
2:
3: namespace Budabot\User\Modules;
4:
5: use Budabot\Core\AutoInject;
6:
7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26:
27: class SilenceController extends AutoInject {
28:
29: 30: 31: 32:
33: public $moduleName;
34:
35: const NULL_COMMAND_HANDLER = "SilenceController.nullCommand";
36:
37: 38: 39:
40: public function setup() {
41: $this->db->loadSQLFile($this->moduleName, "silence_cmd");
42: }
43:
44: 45: 46: 47:
48: public function silenceCommand($message, $channel, $sender, $sendto, $args) {
49: $sql = "SELECT * FROM silence_cmd_<myname> ORDER BY cmd, channel";
50: $data = $this->db->query($sql);
51: if (count($data) == 0) {
52: $msg = "No commands have been silenced.";
53: } else {
54: $blob = '';
55: forEach ($data as $row) {
56: $unsilenceLink = $this->text->makeChatcmd("Unsilence", "/tell <myname> unsilence $row->cmd $row->channel");
57: $blob .= "<highlight>$row->cmd<end> ($row->channel) - $unsilenceLink\n";
58: }
59: $msg = $this->text->makeBlob("Silenced Commands", $blob);
60: }
61: $sendto->reply($msg);
62: }
63:
64: 65: 66: 67:
68: public function silenceAddCommand($message, $channel, $sender, $sendto, $args) {
69: $command = strtolower($args[1]);
70: $channel = strtolower($args[2]);
71:
72: $data = $this->commandManager->get($command, $channel);
73: if (count($data) == 0) {
74: $msg = "Could not find command <highlight>$command<end> for channel <highlight>$channel<end>.";
75: } else if ($this->isSilencedCommand($data[0])){
76: $msg = "Command <highlight>$command<end> for channel <highlight>$channel<end> has already been silenced.";
77: } else {
78: $this->addSilencedCommand($data[0]);
79: $msg = "Command <highlight>$command<end> for channel <highlight>$channel<end> has been silenced.";
80: }
81: $sendto->reply($msg);
82: }
83:
84: 85: 86: 87:
88: public function unsilenceCommand($message, $channel, $sender, $sendto, $args) {
89: $command = strtolower($args[1]);
90: $channel = strtolower($args[2]);
91:
92: $data = $this->commandManager->get($command, $channel);
93: if (count($data) == 0) {
94: $msg = "Could not find command <highlight>$command<end> for channel <highlight>$channel<end>.";
95: } else if (!$this->isSilencedCommand($data[0])){
96: $msg = "Command <highlight>$command<end> for channel <highlight>$channel<end> has not been silenced.";
97: } else {
98: $this->removeSilencedCommand($data[0]);
99: $msg = "Command <highlight>$command<end> for channel <highlight>$channel<end> has been unsilenced.";
100: }
101: $sendto->reply($msg);
102: }
103:
104: public function nullCommand($message, $channel, $sender, $sendto, $args) {
105: $this->logger->log('DEBUG', "Silencing command '$message' for channel '$channel'");
106: }
107:
108: public function addSilencedCommand($row) {
109: $this->commandManager->activate($row->type, self::NULL_COMMAND_HANDLER, $row->cmd, 'all');
110: $sql = "INSERT INTO silence_cmd_<myname> (cmd, channel) VALUES (?, ?)";
111: $this->db->exec($sql, $row->cmd, $row->type);
112: }
113:
114: public function isSilencedCommand($row) {
115: $sql = "SELECT * FROM silence_cmd_<myname> WHERE cmd = ? AND channel = ?";
116: $row = $this->db->queryRow($sql, $row->cmd, $row->type);
117: return $row !== null;
118: }
119:
120: public function removeSilencedCommand($row) {
121: $this->commandManager->activate($row->type, $row->file, $row->cmd, $row->admin);
122: $sql = "DELETE FROM silence_cmd_<myname> WHERE cmd = ? AND channel = ?";
123: $this->db->exec($sql, $row->cmd, $row->type);
124: }
125:
126: 127: 128: 129:
130: public function overwriteCommandHandlersEvent($eventObj) {
131: $sql = "SELECT * FROM silence_cmd_<myname>";
132: $data = $this->db->query($sql);
133: forEach ($data as $row) {
134: $this->commandManager->activate($row->channel, self::NULL_COMMAND_HANDLER, $row->cmd, 'all');
135: }
136: }
137: }
138: