1: <?php
2:
3: namespace Budabot\Core\Modules;
4:
5: 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: class BanController {
32:
33: 34: 35: 36:
37: public $moduleName;
38:
39:
40: public $accessManager;
41:
42:
43: public $util;
44:
45:
46: public $chatBot;
47:
48:
49: public $settingManager;
50:
51:
52: public $text;
53:
54:
55: public $db;
56:
57: private $banlist = array();
58:
59: 60: 61: 62: 63: 64: 65: 66: 67:
68: public $defaultNotifyBannedPlayer = "1";
69:
70: 71: 72: 73:
74: public function setup() {
75: $this->db->loadSQLFile($this->moduleName, "banlist");
76: }
77:
78: 79: 80: 81: 82:
83: public function initializeBanList($eventObj) {
84: $this->uploadBanlist();
85: }
86:
87: 88: 89: 90: 91: 92: 93: 94: 95:
96: public function banPlayerWithTimeAndReasonCommand($message, $channel, $sender, $sendto, $args) {
97: $who = ucfirst(strtolower($args[1]));
98: $length = $this->util->parseTime($args[2]);
99: $reason = $args[4];
100:
101: if (!$result = $this->banPlayer($who, $sender, $length, $reason, $sendto)) {
102: return $result;
103: }
104:
105: $timeString = $this->util->unixtimeToReadable($length);
106: $sendto->reply("You have banned <highlight>$who<end> from this bot for $timeString.");
107: if ($this->settingManager->get('notify_banned_player') == 1) {
108: $this->chatBot->sendTell("You have been banned from this bot by <highlight>$sender<end> for $timeString. Reason: $reason", $who);
109: }
110: }
111:
112: 113: 114: 115: 116: 117: 118: 119: 120: 121:
122: public function banPlayerWithTimeCommand($message, $channel, $sender, $sendto, $args) {
123: $who = ucfirst(strtolower($args[1]));
124: $length = $this->util->parseTime($args[2]);
125:
126: if (!$result = $this->banPlayer($who, $sender, $length, '', $sendto)) {
127: return $result;
128: }
129:
130: $timeString = $this->util->unixtimeToReadable($length);
131: $sendto->reply("You have banned <highlight>$who<end> from this bot for $timeString.");
132: if ($this->settingManager->get('notify_banned_player') == 1) {
133: $this->chatBot->sendTell("You have been banned from this bot by <highlight>$sender<end> for $timeString.", $who);
134: }
135: }
136:
137: 138: 139: 140: 141: 142: 143: 144: 145: 146:
147: public function banPlayerWithReasonCommand($message, $channel, $sender, $sendto, $args) {
148: $who = ucfirst(strtolower($args[1]));
149: $reason = $args[3];
150:
151: if (!$result = $this->banPlayer($who, $sender, null, $reason, $sendto)) {
152: return $result;
153: }
154:
155: $sendto->reply("You have permanently banned <highlight>$who<end> from this bot.");
156: if ($this->settingManager->get('notify_banned_player') == 1) {
157: $this->chatBot->sendTell("You have been permanently banned from this bot by <highlight>$sender<end>. Reason: $reason", $who);
158: }
159: }
160:
161: 162: 163: 164: 165: 166: 167: 168: 169:
170: public function banPlayerCommand($message, $channel, $sender, $sendto, $args) {
171: $who = ucfirst(strtolower($args[1]));
172:
173: if (!$result = $this->banPlayer($who, $sender, null, '', $sendto)) {
174: return $result;
175: }
176:
177: $sendto->reply("You have permanently banned <highlight>$who<end> from this bot.");
178: if ($this->settingManager->get('notify_banned_player') == 1) {
179: $this->chatBot->sendTell("You have been permanently banned from this bot by <highlight>$sender<end>.", $who);
180: }
181: }
182:
183: 184: 185: 186: 187: 188:
189: public function banlistCommand($message, $channel, $sender, $sendto, $args) {
190: $banlist = $this->getBanlist();
191: $count = count($banlist);
192:
193: if ($count == 0) {
194: $msg = "No one is currently banned from this bot.";
195: } else {
196: $blob = '';
197: forEach ($banlist as $ban) {
198: $blob .= "Name: <highlight>{$ban->name}<end>\n";
199: $blob .= "<tab>Date: <highlight>" . $this->util->date($ban->time) . "<end>\n";
200: $blob .= "<tab>By: <highlight>{$ban->admin}<end>\n";
201: if ($ban->banend != 0) {
202: $blob .= "<tab>Ban ends: <highlight>" . $this->util->unixtimeToReadable($ban->banend - time(), false) . "<end>\n";
203: } else {
204: $blob .= "<tab>Ban ends: <highlight>Never<end>\n";
205: }
206:
207: if ($ban->reason != '') {
208: $blob .= "<tab>Reason: <highlight>{$ban->reason}<end>\n";
209: }
210: $blob .= "\n";
211: }
212: $msg = $this->text->makeBlob("Banlist ($count)", $blob);
213: }
214: $sendto->reply($msg);
215: }
216:
217: 218: 219: 220: 221: 222: 223: 224: 225:
226: public function unbanCommand($message, $channel, $sender, $sendto, $args) {
227: $who = ucfirst(strtolower($args[1]));
228:
229: $charId = $this->chatBot->get_uid($who);
230: if (!$this->isBanned($charId)) {
231: $sendto->reply("<highlight>$who<end> is not banned on this bot.");
232: return;
233: }
234:
235: $this->remove($charId);
236:
237: $sendto->reply("You have unbanned <highlight>$who<end> from this bot.");
238: if ($this->settingManager->get('notify_banned_player') == 1) {
239: $this->chatBot->sendTell("You have been unbanned from this bot by $sender.", $who);
240: }
241: }
242:
243: 244: 245: 246: 247:
248: public function checkTempBan($eventObj) {
249: $numRows = $this->db->exec("DELETE FROM banlist_<myname> WHERE banend != 0 AND banend < ?", time());
250:
251: if ($numRows > 0) {
252: $this->uploadBanlist();
253: }
254: }
255:
256: 257: 258:
259: private function banPlayer($who, $sender, $length, $reason, $sendto) {
260: $charId = $this->chatBot->get_uid($who);
261: if ($charId == null) {
262: $sendto->reply("Character <highlight>$who<end> does not exist.");
263: return;
264: }
265:
266: if ($this->isBanned($charId)) {
267: $sendto->reply("Character <highlight>$who<end> is already banned.");
268: return;
269: }
270:
271: if ($this->accessManager->compareCharacterAccessLevels($sender, $who) <= 0) {
272: $sendto->reply("You must have an access level higher than <highlight>$who<end> to perform this action.");
273: return;
274: }
275:
276: if ($length === 0) {
277: return false;
278: }
279:
280: $this->add($charId, $sender, $length, $reason);
281: return true;
282: }
283:
284: public function add($charId, $sender, $length, $reason) {
285:
286: if ($length == null) {
287: $ban_end = "0";
288: } else {
289: $ban_end = time() + $length;
290: }
291:
292: $sql = "INSERT INTO banlist_<myname> (`charid`, `admin`, `time`, `reason`, `banend`) VALUES (?, ?, ?, ?, ?)";
293: $numrows = $this->db->exec($sql, $charId, $sender, time(), $reason, $ban_end);
294:
295: $this->uploadBanlist();
296:
297: return $numrows;
298: }
299:
300: public function remove($charId) {
301: $sql = "DELETE FROM banlist_<myname> WHERE charid = ?";
302: $numrows = $this->db->exec($sql, $charId);
303:
304: $this->uploadBanlist();
305:
306: return $numrows;
307: }
308:
309: public function uploadBanlist() {
310: $this->banlist = array();
311:
312: $sql = "
313: SELECT b.*, IFNULL(p.name, b.charid) AS name
314: FROM banlist_<myname> b LEFT JOIN players p ON b.charid = p.charid";
315: $data = $this->db->query($sql);
316: forEach ($data as $row) {
317: $this->banlist[$row->charid] = $row;
318: }
319: }
320:
321: public function isBanned($charId) {
322: return isset($this->banlist[$charId]);
323: }
324:
325: public function getBanlist() {
326: return $this->banlist;
327: }
328: }
329: