2013-11-28 01:26:13 +01:00
|
|
|
<?php
|
2014-04-27 16:22:12 +02:00
|
|
|
|
|
|
|
namespace MCTeam;
|
|
|
|
|
2013-11-28 01:26:13 +01:00
|
|
|
use ManiaControl\Callbacks\CallbackListener;
|
|
|
|
use ManiaControl\Callbacks\CallbackManager;
|
2014-02-08 15:00:21 +01:00
|
|
|
use ManiaControl\Files\FileUtil;
|
2014-01-27 20:39:10 +01:00
|
|
|
use ManiaControl\ManiaControl;
|
2013-11-28 01:26:13 +01:00
|
|
|
use ManiaControl\Plugins\Plugin;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ManiaControl Chatlog Plugin
|
|
|
|
*
|
|
|
|
* @author steeffeen
|
2014-04-13 12:00:08 +02:00
|
|
|
* @copyright ManiaControl Copyright © 2014 ManiaControl Team
|
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
2013-11-28 01:26:13 +01:00
|
|
|
*/
|
2013-12-03 22:21:17 +01:00
|
|
|
class ChatlogPlugin implements CallbackListener, Plugin {
|
2013-11-28 01:26:13 +01:00
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
2014-02-26 10:36:22 +01:00
|
|
|
const ID = 26;
|
2014-01-27 20:39:10 +01:00
|
|
|
const VERSION = 0.1;
|
|
|
|
const DATE = 'd-m-y h:i:sa T';
|
|
|
|
const SETTING_FOLDERNAME = 'Log-Folder Name';
|
|
|
|
const SETTING_FILENAME = 'Log-File Name';
|
|
|
|
const SETTING_USEPID = 'Use Process-Id for File Name';
|
2013-11-28 01:26:13 +01:00
|
|
|
const SETTING_LOGSERVERMESSAGES = 'Log Server Messages';
|
2014-01-27 20:39:10 +01:00
|
|
|
|
2013-11-28 01:26:13 +01:00
|
|
|
/**
|
|
|
|
* Private properties
|
|
|
|
*/
|
2014-01-27 20:39:10 +01:00
|
|
|
/** @var maniaControl $maniaControl */
|
2013-12-14 23:29:17 +01:00
|
|
|
private $maniaControl = null;
|
2013-11-28 01:26:13 +01:00
|
|
|
private $fileName = null;
|
|
|
|
private $logServerMessages = true;
|
|
|
|
|
|
|
|
/**
|
2014-01-27 20:39:10 +01:00
|
|
|
* Prepares the Plugin
|
2013-12-03 22:21:17 +01:00
|
|
|
*
|
2014-01-27 20:39:10 +01:00
|
|
|
* @param ManiaControl $maniaControl
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public static function prepare(ManiaControl $maniaControl) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-12-14 23:29:17 +01:00
|
|
|
* @see \ManiaControl\Plugins\Plugin::load()
|
2013-11-28 01:26:13 +01:00
|
|
|
*/
|
2013-12-14 23:29:17 +01:00
|
|
|
public function load(ManiaControl $maniaControl) {
|
2013-11-28 01:26:13 +01:00
|
|
|
$this->maniaControl = $maniaControl;
|
2014-01-27 20:39:10 +01:00
|
|
|
|
2013-11-28 01:26:13 +01:00
|
|
|
// Init settings
|
2013-11-28 02:04:06 +01:00
|
|
|
$this->maniaControl->settingManager->initSetting($this, self::SETTING_FOLDERNAME, 'logs');
|
|
|
|
$this->maniaControl->settingManager->initSetting($this, self::SETTING_FILENAME, 'ChatLog.log');
|
|
|
|
$this->maniaControl->settingManager->initSetting($this, self::SETTING_USEPID, false);
|
2013-11-28 01:26:13 +01:00
|
|
|
$this->maniaControl->settingManager->initSetting($this, self::SETTING_LOGSERVERMESSAGES, true);
|
2014-01-27 20:39:10 +01:00
|
|
|
|
2013-11-28 01:26:13 +01:00
|
|
|
// Get settings
|
2013-11-28 02:04:06 +01:00
|
|
|
$folderName = $this->maniaControl->settingManager->getSetting($this, self::SETTING_FOLDERNAME);
|
|
|
|
$folderName = FileUtil::getClearedFileName($folderName);
|
2014-01-27 20:39:10 +01:00
|
|
|
$folderDir = ManiaControlDir . '/' . $folderName;
|
2013-11-28 02:04:06 +01:00
|
|
|
if (!is_dir($folderDir)) {
|
|
|
|
$success = mkdir($folderDir);
|
|
|
|
if (!$success) {
|
|
|
|
trigger_error("Couldn't create chat log folder '{$folderName}'.");
|
|
|
|
}
|
|
|
|
}
|
2013-11-28 01:26:13 +01:00
|
|
|
$fileName = $this->maniaControl->settingManager->getSetting($this, self::SETTING_FILENAME);
|
|
|
|
$fileName = FileUtil::getClearedFileName($fileName);
|
2014-01-27 20:39:10 +01:00
|
|
|
$usePId = $this->maniaControl->settingManager->getSetting($this, self::SETTING_USEPID);
|
2013-11-28 02:04:06 +01:00
|
|
|
if ($usePId) {
|
|
|
|
$dotIndex = strripos($fileName, '.');
|
2014-01-27 20:39:10 +01:00
|
|
|
$pIdPart = '_' . getmypid();
|
2013-11-28 02:04:06 +01:00
|
|
|
if ($dotIndex !== false && $dotIndex >= 0) {
|
|
|
|
$fileName = substr($fileName, 0, $dotIndex) . $pIdPart . substr($fileName, $dotIndex);
|
2014-01-27 20:39:10 +01:00
|
|
|
} else {
|
2013-11-28 02:04:06 +01:00
|
|
|
$fileName .= $pIdPart;
|
|
|
|
}
|
|
|
|
}
|
2014-01-27 20:39:10 +01:00
|
|
|
$this->fileName = $folderDir . '/' . $fileName;
|
2013-11-28 01:26:13 +01:00
|
|
|
$this->logServerMessages = $this->maniaControl->settingManager->getSetting($this, self::SETTING_LOGSERVERMESSAGES);
|
2014-01-27 20:39:10 +01:00
|
|
|
|
2013-11-28 01:26:13 +01:00
|
|
|
// Register for callbacks
|
2014-01-27 20:39:10 +01:00
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCHAT, $this, 'handlePlayerChatCallback');
|
|
|
|
|
2013-12-14 23:29:17 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see \ManiaControl\Plugins\Plugin::unload()
|
|
|
|
*/
|
|
|
|
public function unload() {
|
|
|
|
$this->maniaControl->callbackManager->unregisterCallbackListener($this);
|
|
|
|
unset($this->maniaControl);
|
2013-11-28 01:26:13 +01:00
|
|
|
}
|
|
|
|
|
2013-12-09 10:04:22 +01:00
|
|
|
/**
|
|
|
|
* @see \ManiaControl\Plugins\Plugin::getId()
|
|
|
|
*/
|
|
|
|
public static function getId() {
|
|
|
|
return self::ID;
|
|
|
|
}
|
|
|
|
|
2013-12-03 22:21:17 +01:00
|
|
|
/**
|
|
|
|
* @see \ManiaControl\Plugins\Plugin::getName()
|
|
|
|
*/
|
|
|
|
public static function getName() {
|
|
|
|
return 'Chatlog Plugin';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see \ManiaControl\Plugins\Plugin::getVersion()
|
|
|
|
*/
|
|
|
|
public static function getVersion() {
|
|
|
|
return self::VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see \ManiaControl\Plugins\Plugin::getAuthor()
|
|
|
|
*/
|
|
|
|
public static function getAuthor() {
|
|
|
|
return 'steeffeen';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see \ManiaControl\Plugins\Plugin::getDescription()
|
|
|
|
*/
|
|
|
|
public static function getDescription() {
|
2014-02-26 10:36:22 +01:00
|
|
|
return 'Plugin logging the Chat Messages of the Server for later Checks and Controlling.';
|
2013-12-03 22:21:17 +01:00
|
|
|
}
|
|
|
|
|
2013-11-28 01:26:13 +01:00
|
|
|
/**
|
|
|
|
* Handle PlayerChat callback
|
|
|
|
*
|
2014-01-27 20:39:10 +01:00
|
|
|
* @param array $chatCallback
|
2013-11-28 01:26:13 +01:00
|
|
|
*/
|
|
|
|
public function handlePlayerChatCallback(array $chatCallback) {
|
|
|
|
$data = $chatCallback[1];
|
|
|
|
if ($data[0] <= 0 && !$this->logServerMessages) {
|
|
|
|
// Skip server message
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->logText($data[2], $data[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log the given message
|
|
|
|
*
|
2014-01-27 20:39:10 +01:00
|
|
|
* @param string $text
|
|
|
|
* @param string $login
|
2013-11-28 01:26:13 +01:00
|
|
|
*/
|
|
|
|
private function logText($text, $login = null) {
|
|
|
|
if (!$login) {
|
|
|
|
$login = '';
|
|
|
|
}
|
2013-12-31 18:24:40 +01:00
|
|
|
$message = date(self::DATE) . " >> {$login}: {$text}" . PHP_EOL;
|
2013-11-28 01:26:13 +01:00
|
|
|
file_put_contents($this->fileName, $message, FILE_APPEND);
|
|
|
|
}
|
|
|
|
}
|