TrackManiaControl/application/plugins/Chatlog.php

156 lines
4.2 KiB
PHP
Raw Normal View History

2013-11-28 01:26:13 +01:00
<?php
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
*/
class ChatlogPlugin implements CallbackListener, Plugin {
2013-11-28 01:26:13 +01:00
/**
* Constants
*/
2014-01-27 20:39:10 +01:00
const ID = 1;
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
*
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
$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
$folderName = $this->maniaControl->settingManager->getSetting($this, self::SETTING_FOLDERNAME);
$folderName = FileUtil::getClearedFileName($folderName);
2014-01-27 20:39:10 +01:00
$folderDir = ManiaControlDir . '/' . $folderName;
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);
if ($usePId) {
$dotIndex = strripos($fileName, '.');
2014-01-27 20:39:10 +01:00
$pIdPart = '_' . getmypid();
if ($dotIndex !== false && $dotIndex >= 0) {
$fileName = substr($fileName, 0, $dotIndex) . $pIdPart . substr($fileName, $dotIndex);
2014-01-27 20:39:10 +01:00
} else {
$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;
}
/**
* @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() {
return 'Plugin logging the chat messages of the server.';
}
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 = '';
}
$message = date(self::DATE) . " >> {$login}: {$text}" . PHP_EOL;
2013-11-28 01:26:13 +01:00
file_put_contents($this->fileName, $message, FILE_APPEND);
}
}