Skip to content

Commit d8f5ab9

Browse files
committed
v1.1 features
Issue #2 Issue #3
1 parent 5087311 commit d8f5ab9

File tree

5 files changed

+146
-42
lines changed

5 files changed

+146
-42
lines changed

CommandTracker-source/README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
#CommandTracker v1.0.1
1+
#CommandTracker v1.1
22

33
Plugin for PocketMine-MP that logs all player commands and censors commands for inappropriate language.
44

55
Copyright (C) 2015 Scott Handley
6+
https://github.com/mcprostar205/pocketmine-plugins/tree/master/CommandTracker-source
67

78
This program is free software: you can redistribute it and/or modify
89
it under the terms of the GNU General Public License as published by
@@ -56,8 +57,10 @@ has been run at least one time.
5657

5758
| Configuration | Type | Default | Description |
5859
| :---: | :---: | :---: | :--- |
60+
| log-tofile | boolean | false | Redirects tracking log from the console to a file in _CommandTracker/logs_.
5961
| show-passwords | boolean | false | Obfuscate passwords for SimpleAuth's "register" and "login" commands
6062
| commands-censored | string | say,tell | Comma separated list of commands that qualify for censoring.
63+
| commands-ignored | string | empty | Comma separate list of commands to suppress from tracking.
6164
| words-banned | string | empty | Comma separated list of words deemed inappropriate.
6265

6366
## Permissions
@@ -74,12 +77,27 @@ All methods are available through the main plugin object
7477

7578
* boolean showPasswords()
7679
* boolean isCommandCensored(String $command)
80+
* boolean isCommandTracked(String $command)
7781
* boolean hasBannedWord(String $message)
7882

7983
## Release Notes
8084

85+
### 1.1
86+
87+
* Added feature to redirect tracking activity to a log file.
88+
89+
A new _config.yml_ entry called `log-tofile` can be added with values `true` (file) or `false` (system console).
90+
91+
Note: Log files are generated in the _CommandTracker/logs_ directory.
92+
93+
* Added feature to ignor specific commands from tracking activity.
94+
95+
A new _config.yml_ entry called `commands-ignored` can be added with a comma separated list of commands to ignor/suppress.
96+
8197
### 1.0.1
98+
8299
* Fixed empty banned word condition throwing the following exception when Player chats:
83100

84101
`[Server thread/CRITICAL]: "Could not pass event 'pocketmine\event\player\PlayerCommandPreprocessEvent' to 'CommandTracker v1.0': Uninitialized string offset: 0 on CommandTracker\EventListener`
102+
85103
`[22:14:33] [Server thread/NOTICE]: StringOutOfBoundsException: "Uninitialized string offset: 0" (E_NOTICE) in "/CommandTracker_v1.0.phar/src/CommandTracker/CommandTracker" at line 152`

CommandTracker-source/plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: CommandTracker
22
main: CommandTracker\CommandTracker
3-
version: "1.0.1"
3+
version: "1.1"
44
api: [1.10.0]
55
load: STARTUP
66
author: Scott Handley
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
---
2+
log-tofile: false
23
show-passwords: false
34
commands-censored: say,tell
5+
commands-ignored:
46
words-banned:
57
...

CommandTracker-source/src/CommandTracker/CommandTracker.php

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/*
44
* Copyright (C) 2015 Scott Handley
5+
* https://github.com/mcprostar205/pocketmine-plugins/tree/master/CommandTracker-source
56
*
67
* This program is free software: you can redistribute it and/or modify
78
* it under the terms of the GNU General Public License as published by
@@ -19,6 +20,7 @@
1920

2021
namespace CommandTracker;
2122

23+
use LogLevel;
2224
use pocketmine\plugin\PluginBase;
2325
use pocketmine\command\Command;
2426
use pocketmine\command\CommandExecutor;
@@ -28,12 +30,17 @@
2830

2931
class CommandTracker extends PluginBase implements CommandExecutor
3032
{
33+
const PROP_LOG_TOFILE = "log-tofile";
3134
const PROP_SHOW_PASSWORDS = "show-passwords";
3235
const PROP_CENSOR_COMMANDS = "commands-censored";
36+
const PROP_IGNOR_COMMANDS = "commands-ignored";
3337
const PROP_BANNED_WORDS = "words-banned";
3438

39+
protected $logToFile = true;
40+
protected $logfile = null;
3541
protected $passwordsVisible = false;
3642
protected $commandsCensored = [];
43+
protected $commandsIgnored = [];
3744
protected $wordsBanned = [];
3845

3946
public function onEnable()
@@ -42,12 +49,33 @@ public function onEnable()
4249
$this->saveDefaultConfig();
4350
$this->getServer()->getPluginManager()->registerEvents(new EventListener($this), $this);
4451

52+
$this->logToFile = $this->getConfig()->get(CommandTracker::PROP_LOG_TOFILE);
4553
$this->passwordsVisible = $this->getConfig()->get(CommandTracker::PROP_SHOW_PASSWORDS);
4654
$this->commandsCensored = \explode(",", $this->getConfig()->get(CommandTracker::PROP_CENSOR_COMMANDS));
55+
$this->commandsIgnored = \explode(",", $this->getConfig()->get(CommandTracker::PROP_IGNOR_COMMANDS));
4756
$this->wordsBanned = \explode(",", $this->getConfig()->get(CommandTracker::PROP_BANNED_WORDS));
4857

58+
if( $this->logToFile === true )
59+
{
60+
if(!file_exists($this->getDataFolder() . "logs/"))
61+
{
62+
\mkdir($this->getDataFolder() . "logs/");
63+
}
64+
$logfilename = $this->getDataFolder() . "logs/CommandTracker_" . \date("Y-d-m_G-i-s") . ".log";
65+
$this->logfile = \fopen($logfilename, "at");
66+
}
67+
4968
} /* onEnable */
5069

70+
public function onDisable()
71+
{
72+
if( ($this->logToFile === true) && ($this->logfile !== null) )
73+
{
74+
\fclose($this->logfile);
75+
}
76+
77+
} /* onDisable */
78+
5179
public function onCommand(CommandSender $sender, Command $command, $label, array $args)
5280
{
5381
$message = null;
@@ -119,15 +147,32 @@ public function showPasswords()
119147

120148
} /* showPasswords */
121149

150+
public function isCommandTracked($command)
151+
{
152+
$cmdfound = false;
153+
if( isset($this->commandsIgnored) )
154+
{
155+
foreach( $this->commandsIgnored as $ignoredCommand )
156+
{
157+
if( isset($ignoredCommand[0]) && \strcasecmp($command,$ignoredCommand) === 0 )
158+
{
159+
$cmdfound = true;
160+
break;
161+
}
162+
} /* foreach */
163+
}
164+
return !$cmdfound;
165+
166+
} /* isCommandTracked */
167+
122168
public function isCommandCensored($command)
123169
{
124170
$commandfound = false;
125-
$command = \strtolower($command);
126171
if( isset($this->commandsCensored) )
127172
{
128173
foreach( $this->commandsCensored as $censoredCommand )
129174
{
130-
if( \strcmp($command,$censoredCommand) === 0 )
175+
if( isset($censoredCommand[0]) && \strcasecmp($command,$censoredCommand) === 0 )
131176
{
132177
$commandfound = true;
133178
break;
@@ -183,7 +228,38 @@ public function hasBannedWord($message)
183228
return $wordfound;
184229

185230
} /* hasBannedWord */
186-
231+
232+
public function logMessage($level, $message)
233+
{
234+
if( $this->logToFile !== true )
235+
{
236+
$this->getLogger()->log($level, $message);
237+
}
238+
else
239+
{
240+
$entry = \date("Y-d-m [G:i:s] ");
241+
switch( $level )
242+
{
243+
case LogLevel::ERROR:
244+
$entry .= "[ERROR]: ";
245+
break;
246+
247+
case LogLevel::WARNING:
248+
$entry .= "[WARNING]: ";
249+
break;
250+
251+
case LogLevel::INFO:
252+
default:
253+
$entry .= "[INFO]: ";
254+
break;
255+
}
256+
257+
$entry .= $message . "\n";
258+
\fwrite($this->logfile, $entry);
259+
}
260+
261+
} /* logMessage */
262+
187263
protected function banWord($word, $add = true)
188264
{
189265
$message = null;

CommandTracker-source/src/CommandTracker/EventListener.php

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/*
44
* Copyright (C) 2015 Scott Handley
5+
* https://github.com/mcprostar205/pocketmine-plugins/tree/master/CommandTracker-source
56
*
67
* This program is free software: you can redistribute it and/or modify
78
* it under the terms of the GNU General Public License as published by
@@ -19,13 +20,14 @@
1920

2021
namespace CommandTracker;
2122

23+
use LogLevel;
2224
use pocketmine\plugin\PluginBase;
2325
use pocketmine\event\Listener;
2426
use pocketmine\event\player\PlayerCommandPreprocessEvent;
2527
use pocketmine\event\server\ServerCommandEvent;
2628
use pocketmine\event\server\RemoteServerCommandEvent;
2729
use pocketmine\utils\TextFormat;
28-
30+
2931
class EventListener extends PluginBase implements Listener
3032
{
3133

@@ -36,66 +38,72 @@ public function __construct(CommandTracker $plugin)
3638

3739
public function onPlayerCommand(PlayerCommandPreprocessEvent $event)
3840
{
39-
$bannedword = false;
40-
$message = $event->getMessage();
4141
$playername = $event->getPlayer()->getDisplayName();
42-
43-
// censor global chat if "say" command is also censored
44-
if( $message[0] != '/' && $this->plugin->isCommandCensored("say") )
42+
$message = $event->getMessage();
43+
$words = \explode(" ", $message);
44+
45+
// global chat is the "say" command
46+
if( $message[0] !== '/' )
4547
{
46-
$this->plugin->getLogger()->info("<$playername> /say $message");
47-
$bannedword = $this->plugin->hasBannedWord($message);
48+
$cmd = "say";
4849
}
49-
// obfuscate password from tracking during registration
50-
elseif( (\substr_compare($message,"/register",0,9) === 0) &&
51-
($this->plugin->showPasswords() === false) )
50+
else
5251
{
53-
$this->plugin->getLogger()->info("<$playername> /register ****");
52+
$cmd = \strtolower(\substr(\array_shift($words),1));
5453
}
55-
// obfuscate password from tracking during login
56-
elseif( (\substr_compare($message,"/login",0,6) === 0) &&
54+
55+
// obfuscate password from tracking on SimpleAuth register or login commands
56+
if( ((\strcmp($cmd,"register") === 0) || (\strcmp($cmd,"login") === 0)) &&
5757
($this->plugin->showPasswords() === false) )
5858
{
59-
$this->plugin->getLogger()->info("<$playername> /login ****");
59+
$this->plugin->logMessage(LogLevel::INFO, "<$playername> /$cmd ****");
6060
}
61-
// log the command
6261
else
6362
{
64-
$this->plugin->getLogger()->info("<$playername> $message");
65-
66-
// verify command qualifies for censorship
67-
$words = \explode(" ", $message);
68-
$cmd = \array_shift($words);
69-
if( $this->plugin->isCommandCensored(\substr($cmd,1)) && isset($words) )
63+
if( $this->plugin->isCommandTracked($cmd) )
7064
{
71-
$bannedword = $this->plugin->hasBannedWord($message);
65+
$this->plugin->logMessage(LogLevel::INFO, "<$playername> /$cmd " . \implode(" ", $words));
7266
}
73-
}
74-
75-
// if banned word found, cancel command and inform player about inappropriate word usage
76-
if( $bannedword === true )
77-
{
78-
$this->plugin->getLogger()->warning("<$playername> used an inappropriate word. The command has been censored.");
79-
$event->getPlayer()->sendMessage(TextFormat::RED . "Command cancelled due to inappropriate language. Administrator has been notified.");
80-
$event->setCancelled(true);
81-
}
67+
68+
// verify command qualifies for censorship
69+
if( $this->plugin->isCommandCensored($cmd) && isset($words) )
70+
{
71+
// if banned word found, cancel command and inform player about inappropriate word usage
72+
if( $this->plugin->hasBannedWord($message) === true )
73+
{
74+
$this->plugin->logMessage(LogLevel::WARNING, "<$playername> used an inappropriate word. The command ($cmd) has been censored.");
75+
$event->getPlayer()->sendMessage(TextFormat::RED . "Command cancelled due to inappropriate language. Administrator has been notified.");
76+
$event->setCancelled(true);
77+
78+
} /* if( hasBannedWord ) */
79+
80+
} /* if( isCommandCensored ) */
81+
82+
} /* else $cmd (!register || !login) && showPasswords */
8283

8384
} /* onPlayerCommand */
8485

8586
public function onServerCommand(ServerCommandEvent $event)
8687
{
87-
$command = $event->getCommand();
88-
$this->plugin->getLogger()->info("<CONSOLE> /$command");
88+
$this->logConsoleCommand("CONSOLE", $event->getCommand());
8989

9090
} /* onServerCommand */
9191

9292
public function onRemoteCommand(RemoteServerCommandEvent $event)
93-
{
94-
$command = $event->getCommand();
95-
$this->plugin->getLogger()->info("<REMOTE> /$command");
93+
{
94+
$this->logConsoleCommand("REMOTE", $event->getCommand());
9695

9796
} /* onRemoteCommand */
9897

98+
protected function logConsoleCommand($context, $message)
99+
{
100+
$words = \explode(" ", $message);
101+
$cmd = \strtolower(\array_shift($words));
102+
if( $this->plugin->isCommandTracked($cmd) )
103+
{
104+
$this->plugin->logMessage(LogLevel::INFO, "<$context> /$message");
105+
}
106+
}
99107
}
100108

101109
?>

0 commit comments

Comments
 (0)