1: <?php
2:
3: namespace aliuly\common;
4: use aliuly\common\Session;
5: use pocketmine\plugin\PluginBase;
6: use pocketmine\event\player\PlayerCommandPreprocessEvent;
7: use pocketmine\event\server\RemoteServerCommandEvent;
8: use pocketmine\event\server\ServerCommandEvent;
9: use pocketmine\event\player\PlayerJoinEvent;
10: use pocketmine\Player;
11: use aliuly\common\MPMU;
12:
13:
14:
15: 16: 17:
18: class SpySession extends Session {
19: const RCON = "*RCON*";
20: const CONSOLE = "*CONSOLE*";
21: protected $conlog;
22: protected $privacy;
23: protected $exPerms;
24: protected $notice;
25:
26: 27: 28:
29: public function __construct(PluginBase $owner, $privacy= null, $exPerms = null, $notice = null) {
30: parent::__construct($owner);
31: $this->conlog = null;
32: $this->privacy = $privacy;
33: $this->exPerms = $exPerms;
34: $this->notice = is_null($notice) || is_array($notice) ? $notice : [ $notice ];
35: }
36: 37: 38: 39:
40: public function setLogging($cb) {
41: $this->conlog = is_callable($cb) ? $cb : null;
42: }
43: 44: 45: 46:
47: public function isLogging() {
48: return is_callable($this->conlog);
49: }
50: 51: 52: 53: 54:
55: public function configTap(Player $pl, $cb) {
56: $n = MPMU::iName($pl);
57: if (is_callable($cb)) {
58: if (!isset($this->state[$n])) {
59: $this->state[$n] = [];
60: }
61: $this->state[$n]["callback"] = $cb;
62: } else {
63: if (isset($this->state[$n])) unset($this->state[$n]);
64: }
65: }
66: 67: 68: 69: 70:
71: public function isTapping(Player $pl) {
72: $n = MPMU::iName($pl);
73: return isset($this->state[$n]);
74: }
75: 76: 77: 78: 79:
80: public function getTaps(Player $pl) {
81: $n = MPMU::iName($pl);
82: if (!isset($this->state[$n])) return null;
83: if (!isset($this->state[$n]["taps"])) return null;
84: return array_keys($this->state[$n]["taps"]);
85: }
86:
87: 88: 89: 90: 91:
92: public function setTap(Player $pl,$v,$ena = true) {
93: $v = MPMU::iName($v);
94: $n = MPMU::iName($pl);
95: if ($v === null) {
96:
97: if ($ena) {
98: if (isset($this->state[$n]["taps"])) unset($this->state[$n]["taps"]);
99: } else {
100: $this->state[$n]["taps"] = [];
101: }
102: return;
103: }
104: if (!isset($this->state[$n]["taps"])) $this->state[$n]["taps"] = [];
105: if ($ena) {
106: $this->state[$n]["taps"][$v] = $v;
107: } else {
108: if (isset($this->state[$n]["taps"][$v])) unset($this->state[$n]["taps"][$v]);
109: }
110: }
111:
112:
113: protected function logEvent($n,$msg) {
114:
115:
116:
117: foreach ([
118:
119: '/\/login\s*.*/' => '/login **CENSORED**',
120: '/\/register\s*.*/' => '/register **CENSORED**',
121: '/\/unregister\s*.*/' => '/register **CENSORED**',
122:
123: '/\/chpwd\s*.*/' => '/chpwd **CENSORED**',
124: ] as $re => $txt) {
125: $msg = preg_replace($re,$txt,$msg);
126: }
127: if (is_array($this->privacy)) {
128: foreach ($this->privacy as $re => $txt) {
129: $msg = preg_replace($re,$txt,$msg);
130: }
131: }
132:
133: if ($this->conlog !== null) {
134: $log = $this->conlog;
135: $log($n,$msg);
136: }
137: foreach ($this->state as $ls=>&$tt) {
138: if (isset($tt["taps"]) && !isset($tt["taps"][$n])) continue;
139: $log = $tt["callback"];
140: $log($ls,$n,$msg);
141: }
142: }
143: 144: 145:
146: public function onPlayerCmd(PlayerCommandPreprocessEvent $ev) {
147: if ($ev->isCancelled()) return;
148: if ($this->exPerms !== null && $ev->getPlayer()->hasPermission($this->exPerms)) return;
149: $this->logEvent(MPMU::iName($ev->getPlayer()),$ev->getMessage());
150: }
151: 152: 153:
154: public function onRconCmd(RemoteServerCommandEvent $ev) {
155: $this->logEvent(self::RCON,$ev->getMessage());
156: }
157: 158: 159:
160: public function onConsoleCmd(ServerCommandEvent $ev) {
161: $this->logEvent(self::CONSOLE,$ev->getCommand());
162: }
163: public function onJoin(PlayerJoinEvent $ev) {
164: if ($this->notice === null) return;
165: foreach ($this->notice as $ln) {
166: $ev->getPlayer()->sendMessage($ln);
167: }
168: }
169: }
170: