1: <?php
2:
3: namespace Budabot\Core;
4:
5: 6: 7: 8: 9: 10:
11: class LimitsController {
12: 13: 14: 15:
16: public $moduleName;
17:
18:
19: public $settingManager;
20:
21:
22: public $chatBot;
23:
24:
25: public $accessManager;
26:
27:
28: public $playerManager;
29:
30:
31: public $playerHistoryManager;
32:
33:
34: public $util;
35:
36:
37: public $whitelistController;
38:
39:
40: public $logger;
41:
42: 43: 44:
45: public function setup() {
46: $this->settingManager->add($this->moduleName, "tell_req_lvl", "Minimum level required to send tell to bot", "edit", "number", "0", "0;10;50;100;150;190;205;215");
47: $this->settingManager->add($this->moduleName, "tell_req_faction", "Faction required to send tell to bot", "edit", "options", "all", "all;Omni;Neutral;Clan;not Omni;not Neutral;not Clan");
48: $this->settingManager->add($this->moduleName, "tell_min_player_age", "Minimum age of player to send tell to bot", "edit", "time", "1s", "1s;7days;14days;1month;2months;6months;1year;2years", '', 'mod', 'limits.txt');
49: $this->settingManager->add($this->moduleName, "tell_error_msg_type", "How to show error messages when limit requirements are not met", "edit", "options", "2", "Specific;Generic;None", "2;1;0");
50: }
51:
52: public function check($sender, $message) {
53: if (preg_match("/^about$/i", $message)) {
54: $msg = true;
55: } else if ($this->whitelistController->check($sender) || $sender == ucfirst(strtolower($this->settingManager->get("relaybot")))) {
56: $msg = true;
57: } else if ($this->accessManager->checkAccess($sender, 'member')) {
58:
59: $msg = true;
60: } else {
61: $msg = $this->runChecks($sender);
62: }
63:
64: if ($msg === true) {
65: return true;
66: } else {
67: $this->logger->log('Info', "$sender denied access to bot due to: $msg");
68:
69: $this->handleLimitCheckFail($msg, $sender);
70:
71: list($cmd, $params) = explode(' ', $message, 2);
72: $cmd = strtolower($cmd);
73:
74: if ($this->settingManager->get('access_denied_notify_guild') == 1) {
75: $this->chatBot->sendGuild("Player <highlight>$sender<end> was denied access to command <highlight>$cmd<end> due to limit checks.", true);
76: }
77: if ($this->settingManager->get('access_denied_notify_priv') == 1) {
78: $this->chatBot->sendPrivate("Player <highlight>$sender<end> was denied access to command <highlight>$cmd<end> due to limit checks.", true);
79: }
80:
81: return false;
82: }
83: }
84:
85: public function handleLimitCheckFail($msg, $sender) {
86: if ($this->settingManager->get('tell_error_msg_type') == 2) {
87: $this->chatBot->sendTell($msg, $sender);
88: } else if ($this->settingManager->get('tell_error_msg_type') == 1) {
89: $msg = "Error! You do not have access to this bot.";
90: $this->chatBot->sendTell($msg, $sender);
91: } else {
92:
93: }
94: }
95:
96: public function runChecks($sender) {
97: if ($this->settingManager->get("tell_req_lvl") != 0 || $this->settingManager->get("tell_req_faction") != "all") {
98:
99: $whois = $this->playerManager->getByName($sender);
100: if ($whois === null) {
101: return "Error! Unable to get your character info for limit checks. Please try again later.";
102: }
103:
104:
105: if ($this->settingManager->get("tell_req_lvl") != 0 && $this->settingManager->get("tell_req_lvl") > $whois->level) {
106: return "Error! You must be at least level <highlight>" . $this->settingManager->get("tell_req_lvl") . "<end>.";
107: }
108:
109:
110: if (($this->settingManager->get("tell_req_faction") == "Omni" || $this->settingManager->get("tell_req_faction") == "Clan" || $this->settingManager->get("tell_req_faction") == "Neutral") && $this->settingManager->get("tell_req_faction") != $whois->faction) {
111: return "Error! You must be <highlight>" . $this->settingManager->get("tell_req_faction") . "<end>.";
112: } else if ($this->settingManager->get("tell_req_faction") == "not Omni" || $this->settingManager->get("tell_req_faction") == "not Clan" || $this->settingManager->get("tell_req_faction") == "not Neutral") {
113: $tmp = explode(" ", $this->settingManager->get("tell_req_faction"));
114: if ($tmp[1] == $whois->faction) {
115: return "Error! You must not be <highlight>{$tmp[1]}<end>.";
116: }
117: }
118: }
119:
120:
121: if ($this->settingManager->get("tell_min_player_age") > 1) {
122: $history = $this->playerHistoryManager->lookup($sender, $this->chatBot->vars['dimension']);
123: if ($history === null) {
124: return "Error! Unable to get your character history for limit checks. Please try again later.";
125: } else {
126: $minAge = time() - $this->settingManager->get("tell_min_player_age");
127: $entry = array_pop($history->data);
128:
129:
130: if ($entry->last_changed > $minAge) {
131: $timeString = $this->util->unixtimeToReadable($this->settingManager->get("tell_min_player_age"));
132: return "Error! You must be at least <highlight>$timeString<end> old.";
133: }
134: }
135: }
136:
137: return true;
138: }
139: }
140: