1: <?php
2:
3: namespace Budabot\User\Modules;
4:
5: use Budabot\Core\StopExecutionException;
6:
7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20:
21: class BroadcastController {
22:
23: 24: 25: 26:
27: public $moduleName;
28:
29:
30: public $logger;
31:
32:
33: public $db;
34:
35:
36: public $chatBot;
37:
38:
39: public $settingManager;
40:
41:
42: public $whitelistController;
43:
44:
45: public $text;
46:
47:
48: public $util;
49:
50: private $broadcastList = array();
51:
52: 53: 54: 55:
56: public function setup() {
57: $this->db->loadSQLFile($this->moduleName, 'broadcast');
58:
59: $this->loadBroadcastListIntoMemory();
60:
61: $this->settingManager->add($this->moduleName, "broadcast_to_guild", "Send broadcast message to guild channel", "edit", "options", "1", "true;false", "1;0");
62: $this->settingManager->add($this->moduleName, "broadcast_to_privchan", "Send broadcast message to private channel", "edit", "options", "0", "true;false", "1;0");
63: }
64:
65: private function loadBroadcastListIntoMemory() {
66:
67: $data = $this->db->query("SELECT * FROM broadcast_<myname>");
68: $this->broadcastList = array();
69: forEach ($data as $row) {
70: $this->broadcastList[$row->name] = $row;
71: }
72: }
73:
74: 75: 76: 77:
78: public function broadcastListCommand($message, $channel, $sender, $sendto, $args) {
79: $blob = '';
80:
81: $sql = "SELECT * FROM broadcast_<myname> ORDER BY dt DESC";
82: $data = $this->db->query($sql);
83: forEach ($data as $row) {
84: $remove = $this->text->makeChatcmd('Remove', "/tell <myname> <symbol>broadcast rem $row->name");
85: $dt = $this->util->date($row->dt);
86: $blob .= "<highlight>{$row->name}<end> [added by {$row->added_by}] {$dt} {$remove}\n";
87: }
88:
89: if (count($data) == 0) {
90: $msg = "No bots are on the broadcast list.";
91: } else {
92: $msg = $this->text->makeBlob('Broadcast Bots', $blob);
93: }
94:
95: $sendto->reply($msg);
96: }
97:
98: 99: 100: 101:
102: public function broadcastAddCommand($message, $channel, $sender, $sendto, $args) {
103: $name = ucfirst(strtolower($args[1]));
104:
105: $charid = $this->chatBot->get_uid($name);
106: if ($charid == false) {
107: $sendto->reply("'$name' is not a valid character name.");
108: return;
109: }
110:
111: if (isset($this->broadcastList[$name])) {
112: $sendto->reply("'$name' is already on the broadcast bot list.");
113: return;
114: }
115:
116: $this->db->exec("INSERT INTO broadcast_<myname> (`name`, `added_by`, `dt`) VALUES (?, ?, ?)", $name, $sender, time());
117: $msg = "Broadcast bot added successfully.";
118:
119:
120: $this->loadBroadcastListIntoMemory();
121:
122: $this->whitelistController->add($name, $sender . " (bot)");
123:
124: $sendto->reply($msg);
125: }
126:
127: 128: 129: 130:
131: public function broadcastRemoveCommand($message, $channel, $sender, $sendto, $args) {
132: $name = ucfirst(strtolower($args[2]));
133:
134: if (!isset($this->broadcastList[$name])) {
135: $sendto->reply("'$name' is not on the broadcast bot list.");
136: return;
137: }
138:
139: $this->db->exec("DELETE FROM broadcast_<myname> WHERE name = ?", $name);
140: $msg = "Broadcast bot removed successfully.";
141:
142:
143: $this->loadBroadcastListIntoMemory();
144:
145: $this->whitelistController->remove($name);
146:
147: $sendto->reply($msg);
148: }
149:
150: 151: 152: 153:
154: public function incomingMessageEvent($eventObj) {
155: if ($this->isValidBroadcastSender($eventObj->sender)) {
156: $this->processIncomingMessage($eventObj->sender, $eventObj->message);
157:
158:
159: throw new StopExecutionException();
160: }
161: }
162:
163: public function isValidBroadcastSender($sender) {
164: return isset($this->broadcastList[$sender]);
165: }
166:
167: public function processIncomingMessage($sender, $message) {
168: $msg = "[$sender]: $message";
169:
170: if ($this->settingManager->get('broadcast_to_guild')) {
171: $this->chatBot->sendGuild($msg, true);
172: }
173: if ($this->settingManager->get('broadcast_to_privchan')) {
174: $this->chatBot->sendPrivate($msg, true);
175: }
176: }
177: }
178: