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:
32: class AdminController {
33:
34: 35: 36: 37:
38: public $moduleName;
39:
40:
41: public $adminManager;
42:
43:
44: public $chatBot;
45:
46:
47: public $buddylistManager;
48:
49:
50: public $accessManager;
51:
52:
53: public $commandAlias;
54:
55:
56: public $text;
57:
58:
59: public $db;
60:
61:
62: public $settingManager;
63:
64:
65: public $altsController;
66:
67: 68: 69: 70:
71: public function setup() {
72: $this->adminManager->uploadAdmins();
73:
74: $this->commandAlias->register($this->moduleName, "admin add", "addadmin");
75: $this->commandAlias->register($this->moduleName, "admin rem", "remadmin");
76: $this->commandAlias->register($this->moduleName, "mod add", "addmod");
77: $this->commandAlias->register($this->moduleName, "mod rem", "remmod");
78: }
79:
80: 81: 82: 83:
84: public function adminAddCommand($message, $channel, $sender, $sendto, $args) {
85: $who = ucfirst(strtolower($args[1]));
86: $intlevel = 4;
87: $rank = 'an administrator';
88:
89: $this->add($who, $sender, $sendto, $intlevel, $rank);
90: }
91:
92: 93: 94: 95:
96: public function modAddCommand($message, $channel, $sender, $sendto, $args) {
97: $who = ucfirst(strtolower($args[1]));
98: $intlevel = 3;
99: $rank = 'a moderator';
100:
101: $this->add($who, $sender, $sendto, $intlevel, $rank);
102: }
103:
104: 105: 106: 107:
108: public function adminRemoveCommand($message, $channel, $sender, $sendto, $args) {
109: $who = ucfirst(strtolower($args[1]));
110: $intlevel = 4;
111: $rank = 'an administrator';
112:
113: $this->remove($who, $sender, $sendto, $intlevel, $rank);
114: }
115:
116: 117: 118: 119:
120: public function modRemoveCommand($message, $channel, $sender, $sendto, $args) {
121: $who = ucfirst(strtolower($args[1]));
122: $intlevel = 3;
123: $rank = 'a moderator';
124:
125: $this->remove($who, $sender, $sendto, $intlevel, $rank);
126: }
127:
128: 129: 130: 131: 132:
133: public function adminlistCommand($message, $channel, $sender, $sendto) {
134: if (strtolower($message) == "adminlist all") {
135: $showOfflineAlts = true;
136: } else {
137: $showOfflineAlts = false;
138: }
139:
140: $blob .= "<header2>Administrators<end>\n";
141: forEach ($this->adminManager->admins as $who => $data) {
142: if ($this->adminManager->admins[$who]["level"] == 4) {
143: if ($who != "") {
144: $blob .= "<tab>$who";
145: if ($this->accessManager->checkAccess($who, 'superadmin')) {
146: $blob .= " (<highlight>Super-administrator<end>) ";
147: }
148: $blob .= $this->getOnlineStatus($who) . "\n" . $this->getAltAdminInfo($who, $showOfflineAlts);
149: }
150: }
151: }
152:
153: $blob .= "<header2>Moderators<end>\n";
154: forEach ($this->adminManager->admins as $who => $data){
155: if ($this->adminManager->admins[$who]["level"] == 3){
156: if ($who != "") {
157: $blob .= "<tab>$who" . $this->getOnlineStatus($who) . "\n" . $this->getAltAdminInfo($who, $showOfflineAlts);
158: }
159: }
160: }
161:
162: $link = $this->text->makeBlob('Bot administrators', $blob);
163: $sendto->reply($link);
164: }
165:
166: 167: 168: 169: 170:
171: public function checkAdminsEvent($eventObj) {
172: $data = $this->db->query("SELECT * FROM admin_<myname>");
173: forEach ($data as $row) {
174: $this->buddylistManager->add($row->name, 'admin');
175: }
176: }
177:
178: private function getOnlineStatus($who) {
179: if ($this->buddylistManager->isOnline($who) == 1 && isset($this->chatBot->chatlist[$who])) {
180: return " (<green>Online and in chat<end>)";
181: } else if ($this->buddylistManager->isOnline($who) == 1) {
182: return " (<green>Online<end>)";
183: } else {
184: return " (<red>Offline<end>)";
185: }
186: }
187:
188: private function getAltAdminInfo($who, $showOfflineAlts) {
189: $blob = '';
190: $altInfo = $this->altsController->getAltInfo($who);
191: if ($altInfo->main == $who) {
192: forEach ($altInfo->alts as $alt => $validated) {
193: if ($validated == 1 && ($showOfflineAlts || $this->buddylistManager->isOnline($alt))) {
194: $blob .= "<tab><tab>$alt" . $this->getOnlineStatus($alt) . "\n";
195: }
196: }
197: }
198: return $blob;
199: }
200:
201: public function add($who, $sender, $sendto, $intlevel, $rank) {
202: if ($this->chatBot->get_uid($who) == null){
203: $sendto->reply("Character <highlight>$who<end> does not exist.");
204: return;
205: }
206:
207: if ($this->adminManager->checkExisting($who, $intlevel)) {
208: $sendto->reply("<highlight>$who<end> is already $rank.");
209: return;
210: }
211:
212: if (!$this->checkAccessLevel($sender, $who, $sendto)) {
213: $sendto->reply("You must have a higher access level than <highlight>$who<end> in order to change his access level.");
214: return;
215: }
216:
217: if (!$this->checkAltsInheritAdmin($who)) {
218: $msg = "<red>WARNING<end>: $who is not a main. This command did NOT affect $who's access level and no action was performed.";
219: $sendto->reply($msg);
220: return;
221: }
222:
223: $action = $this->adminManager->addToLists($who, $intlevel);
224:
225: $sendto->reply("<highlight>$who<end> has been $action to $rank.");
226: $this->chatBot->sendTell("You have been $action to $rank by <highlight>$sender<end>.", $who);
227: }
228:
229: public function remove($who, $sender, $sendto, $intlevel, $rank) {
230: if (!$this->adminManager->checkExisting($who, $intlevel)) {
231: $sendto->reply("<highlight>$who<end> is not $rank.");
232: return;
233: }
234:
235: if (!$this->checkAccessLevel($sender, $who, $sendto)) {
236: $sendto->reply("You must have a higher access level than <highlight>$who<end> in order to change his access level.");
237: return;
238: }
239:
240: $this->adminManager->removeFromLists($who);
241:
242: if (!$this->checkAltsInheritAdmin($who)) {
243: $msg = "<red>WARNING<end>: $who is not a main. This command did NOT affect $who's access level.";
244: $sendto->reply($msg);
245: }
246:
247: $sendto->reply("<highlight>$who<end> has been removed as $rank.");
248: $this->chatBot->sendTell("You have been removed as $rank by <highlight>$sender<end>.", $who);
249: }
250:
251: public function checkAltsInheritAdmin($who) {
252: $ai = $this->altsController->getAltInfo($who);
253: if ($ai->main != $who) {
254: return false;
255: } else {
256: return true;
257: }
258: }
259:
260: public function checkAccessLevel($actor, $actee) {
261: $senderAccessLevel = $this->accessManager->getAccessLevelForCharacter($actor);
262: $whoAccessLevel = $this->accessManager->getSingleAccessLevel($actee);
263: if ($this->accessManager->compareAccessLevels($whoAccessLevel, $senderAccessLevel) >= 0) {
264: return false;
265: } else {
266: return true;
267: }
268: }
269: }
270: