2013-11-09 17:24:03 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace ManiaControl;
|
2014-04-12 12:14:37 +02:00
|
|
|
|
2014-05-09 10:48:51 +02:00
|
|
|
use ManiaControl\Admin\AuthenticationManager;
|
2015-06-21 22:24:10 +02:00
|
|
|
use ManiaControl\Callbacks\CallbackListener;
|
|
|
|
use ManiaControl\Callbacks\CallbackManager;
|
2015-06-26 11:48:21 +02:00
|
|
|
use ManiaControl\Communication\CommunicationAnswer;
|
2015-06-22 20:22:17 +02:00
|
|
|
use ManiaControl\Communication\CommunicationListener;
|
|
|
|
use ManiaControl\Communication\CommunicationMethods;
|
2017-03-26 19:44:55 +02:00
|
|
|
use ManiaControl\General\UsageInformationAble;
|
|
|
|
use ManiaControl\General\UsageInformationTrait;
|
2014-05-09 10:48:51 +02:00
|
|
|
use ManiaControl\Players\Player;
|
2014-05-13 17:59:37 +02:00
|
|
|
use Maniaplanet\DedicatedServer\Xmlrpc\UnknownPlayerException;
|
2013-11-09 17:24:03 +01:00
|
|
|
|
|
|
|
/**
|
2014-04-12 12:14:37 +02:00
|
|
|
* Chat Utility Class
|
2013-11-09 17:24:03 +01:00
|
|
|
*
|
2014-05-02 17:40:47 +02:00
|
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
2020-01-22 10:39:35 +01:00
|
|
|
* @copyright 2014-2020 ManiaControl Team
|
2014-05-02 17:40:47 +02:00
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2017-03-26 19:44:55 +02:00
|
|
|
class Chat implements CallbackListener, CommunicationListener, UsageInformationAble {
|
|
|
|
use UsageInformationTrait;
|
2017-05-06 15:14:58 +02:00
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2013-12-03 18:03:16 +01:00
|
|
|
* Constants
|
|
|
|
*/
|
2020-02-28 17:38:05 +01:00
|
|
|
const SETTING_FORMAT_ERROR = 'Error Format';
|
2013-12-03 18:03:16 +01:00
|
|
|
const SETTING_FORMAT_INFORMATION = 'Information Format';
|
2014-01-16 18:08:32 +01:00
|
|
|
const SETTING_FORMAT_SUCCESS = 'Success Format';
|
|
|
|
const SETTING_FORMAT_USAGEINFO = 'UsageInfo Format';
|
2020-02-28 17:38:05 +01:00
|
|
|
const SETTING_PUBLIC_PREFIX = 'Public Messages Prefix';
|
|
|
|
const SETTING_PRIVATE_PREFIX = 'Privat Messages Prefix';
|
2015-06-21 22:24:10 +02:00
|
|
|
const CHAT_BUFFER_SIZE = 200;
|
2020-02-28 17:38:05 +01:00
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2014-08-02 22:31:46 +02:00
|
|
|
* Private properties
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2014-08-02 22:31:46 +02:00
|
|
|
/** @var ManiaControl $maniaControl */
|
2013-11-10 13:46:44 +01:00
|
|
|
private $maniaControl = null;
|
2015-06-21 22:24:10 +02:00
|
|
|
private $chatBuffer = array();
|
2013-11-09 17:24:03 +01:00
|
|
|
|
2013-11-10 13:46:44 +01:00
|
|
|
/**
|
|
|
|
* Construct chat utility
|
|
|
|
*
|
2014-01-16 18:08:32 +01:00
|
|
|
* @param ManiaControl $maniaControl
|
2013-11-10 13:46:44 +01:00
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
2014-01-16 18:08:32 +01:00
|
|
|
|
2014-08-03 01:34:18 +02:00
|
|
|
// Settings
|
2020-02-28 17:38:05 +01:00
|
|
|
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_FORMAT_ERROR, '$f30');
|
2014-08-11 19:24:21 +02:00
|
|
|
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_FORMAT_INFORMATION, '$fff');
|
|
|
|
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_FORMAT_SUCCESS, '$0f0');
|
|
|
|
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_FORMAT_USAGEINFO, '$f80');
|
2020-02-28 17:38:05 +01:00
|
|
|
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_PUBLIC_PREFIX, '» ');
|
|
|
|
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_PRIVATE_PREFIX, '»» ');
|
2015-06-21 22:24:10 +02:00
|
|
|
|
|
|
|
//Callbacks
|
|
|
|
$this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_PLAYERCHAT, $this, 'onPlayerChat');
|
|
|
|
|
|
|
|
//Socket Listenings
|
2015-06-23 16:57:11 +02:00
|
|
|
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::SEND_CHAT_MESSAGE, $this, "communcationSendChat");
|
|
|
|
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::GET_SERVER_CHAT, $this, function ($data) {
|
2015-06-26 11:48:21 +02:00
|
|
|
return new CommunicationAnswer($this->chatBuffer);
|
2015-06-21 22:24:10 +02:00
|
|
|
});
|
2013-11-10 13:46:44 +01:00
|
|
|
}
|
2013-11-09 17:24:03 +01:00
|
|
|
|
|
|
|
/**
|
2020-05-25 10:52:36 +02:00
|
|
|
* Build the chat message prefix
|
2013-11-10 13:46:44 +01:00
|
|
|
*
|
2020-05-25 10:52:36 +02:00
|
|
|
* @param string|bool $prefixParam
|
|
|
|
* @param string|array $login
|
|
|
|
* @return string
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2020-05-25 10:52:36 +02:00
|
|
|
private function buildPrefix($prefixParam, $login = null) {
|
|
|
|
if (is_string($prefixParam)) {
|
|
|
|
return $prefixParam;
|
|
|
|
}
|
|
|
|
if ($prefixParam === true) {
|
|
|
|
if ($login) {
|
|
|
|
$prefix = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_PRIVATE_PREFIX);
|
|
|
|
} else {
|
|
|
|
$prefix = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_PUBLIC_PREFIX);
|
|
|
|
}
|
|
|
|
return $prefix;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles SendChat Communication Request
|
|
|
|
*
|
|
|
|
* @param $data
|
|
|
|
* @return CommunicationAnswer
|
|
|
|
*/
|
|
|
|
public function communcationSendChat($data) {
|
|
|
|
if (!is_object($data) || !property_exists($data, "message")) {
|
|
|
|
return new CommunicationAnswer("You have to provide a valid message", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$prefix = true;
|
|
|
|
if (property_exists($data, "prefix")) {
|
|
|
|
$prefix = $data->prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
$login = null;
|
|
|
|
if (property_exists($data, "login")) {
|
|
|
|
$login = $data->login;
|
|
|
|
}
|
|
|
|
|
|
|
|
$adminLevel = 0;
|
|
|
|
if (property_exists($data, "adminLevel")) {
|
|
|
|
$adminLevel = $data->adminLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
$type = "default";
|
|
|
|
if (property_exists($data, "type")) {
|
|
|
|
$type = $data->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
case "information":
|
|
|
|
if ($adminLevel) {
|
|
|
|
$this->sendInformationToAdmins($data->message, $adminLevel, $prefix);
|
|
|
|
} else {
|
|
|
|
$this->sendInformation($data->message, $login, $prefix);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "success":
|
|
|
|
if ($adminLevel) {
|
|
|
|
$this->sendInformationToAdmins($data->message, $adminLevel, $prefix);
|
|
|
|
} else {
|
|
|
|
$this->sendSuccess($data->message, $login, $prefix);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "error":
|
|
|
|
if ($adminLevel) {
|
|
|
|
$this->sendErrorToAdmins($data->message, $adminLevel, $prefix);
|
|
|
|
} else {
|
|
|
|
$this->sendError($data->message, $login, $prefix);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "usage":
|
|
|
|
$this->sendUsageInfo($data->message, $login, $prefix);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if ($adminLevel) {
|
|
|
|
$this->sendMessageToAdmins($data->message, $adminLevel, $prefix);
|
|
|
|
} else {
|
|
|
|
$this->sendChat($data->message, $login, $prefix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new CommunicationAnswer();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores the ChatMessage in the Buffer
|
|
|
|
*
|
|
|
|
* @param $data
|
|
|
|
*/
|
|
|
|
public function onPlayerChat($data) {
|
|
|
|
$login = $data[1][1];
|
|
|
|
$player = $this->maniaControl->getPlayerManager()->getPlayer($login);
|
|
|
|
|
|
|
|
$nickname = "";
|
|
|
|
if ($player) {
|
|
|
|
$nickname = $player->nickname;
|
|
|
|
}
|
|
|
|
array_push($this->chatBuffer, array("user" => $login, "nickname" => $nickname, "message" => $data[1][2]));
|
|
|
|
if (count($this->chatBuffer) > self::CHAT_BUFFER_SIZE) {
|
|
|
|
array_shift($this->chatBuffer);
|
|
|
|
}
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
2014-05-02 17:40:47 +02:00
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
/**
|
|
|
|
* Send a chat message to the given login
|
|
|
|
*
|
2014-01-16 18:08:32 +01:00
|
|
|
* @param string $message
|
|
|
|
* @param string $login
|
|
|
|
* @param string|bool $prefix
|
2017-05-24 21:02:47 +02:00
|
|
|
* @param bool $multiCall
|
2013-11-10 13:46:44 +01:00
|
|
|
* @return bool
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2017-05-24 21:02:47 +02:00
|
|
|
public function sendChat($message, $login = null, $prefix = true, $multiCall = true) {
|
2014-08-03 01:34:18 +02:00
|
|
|
if (!$this->maniaControl->getClient()) {
|
2013-11-10 13:46:44 +01:00
|
|
|
return false;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
2014-04-28 12:31:33 +02:00
|
|
|
|
2014-08-11 19:51:46 +02:00
|
|
|
$prefix = $this->buildPrefix($prefix, $login);
|
|
|
|
$chatMessage = '$<$z$ff0' . $prefix . $message . '$>';
|
|
|
|
|
|
|
|
if ($login) {
|
2014-05-20 14:59:53 +02:00
|
|
|
if (!is_array($login)) {
|
|
|
|
$login = Player::parseLogin($login);
|
|
|
|
}
|
2014-05-02 17:40:47 +02:00
|
|
|
try {
|
2017-05-24 21:02:47 +02:00
|
|
|
return $this->maniaControl->getClient()->chatSendServerMessage($chatMessage, $login, $multiCall);
|
2014-05-13 17:59:37 +02:00
|
|
|
} catch (UnknownPlayerException $e) {
|
2014-08-11 19:51:46 +02:00
|
|
|
return false;
|
2014-03-02 11:25:47 +01:00
|
|
|
}
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
2014-08-11 19:51:46 +02:00
|
|
|
|
2017-05-24 21:02:47 +02:00
|
|
|
return $this->maniaControl->getClient()->chatSendServerMessage($chatMessage, null, $multiCall);
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
|
2014-05-13 17:59:37 +02:00
|
|
|
/**
|
2014-05-20 14:59:53 +02:00
|
|
|
* Send an Error Message to all Connected Admins
|
2014-05-13 17:59:37 +02:00
|
|
|
*
|
2014-05-20 14:59:53 +02:00
|
|
|
* @param string $message
|
|
|
|
* @param int $minLevel
|
|
|
|
* @param bool $prefix
|
2014-05-13 17:59:37 +02:00
|
|
|
*/
|
|
|
|
public function sendErrorToAdmins($message, $minLevel = AuthenticationManager::AUTH_LEVEL_MODERATOR, $prefix = true) {
|
2014-08-11 19:24:21 +02:00
|
|
|
$format = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_FORMAT_ERROR);
|
2014-05-20 14:59:53 +02:00
|
|
|
$this->sendMessageToAdmins($format . $message, $minLevel, $prefix);
|
2014-05-13 17:59:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-25 10:52:36 +02:00
|
|
|
* Send an Error Message to the Chat
|
2014-05-13 17:59:37 +02:00
|
|
|
*
|
2014-05-20 14:59:53 +02:00
|
|
|
* @param string $message
|
2020-05-25 10:52:36 +02:00
|
|
|
* @param string $login
|
|
|
|
* @param string|bool $prefix
|
2014-07-25 15:16:41 +02:00
|
|
|
* @return bool
|
2014-05-13 17:59:37 +02:00
|
|
|
*/
|
2020-05-25 10:52:36 +02:00
|
|
|
public function sendError($message, $login = null, $prefix = true) {
|
|
|
|
$format = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_FORMAT_ERROR);
|
|
|
|
return $this->sendChat($format . $message, $login, $prefix);
|
2014-05-13 17:59:37 +02:00
|
|
|
}
|
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
/**
|
2020-05-25 10:52:36 +02:00
|
|
|
* Send the Exception Information to the Chat
|
2013-11-09 17:24:03 +01:00
|
|
|
*
|
2020-05-25 10:52:36 +02:00
|
|
|
* @param \Exception $exception
|
|
|
|
* @param string $login
|
2013-11-10 13:46:44 +01:00
|
|
|
* @return bool
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2020-05-25 10:52:36 +02:00
|
|
|
public function sendException(\Exception $exception, $login = null) {
|
|
|
|
$message = "Exception occurred: '{$exception->getMessage()}' ({$exception->getCode()})";
|
|
|
|
return $this->sendError($message, $login);
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
|
2014-08-23 12:59:08 +02:00
|
|
|
/**
|
2020-05-25 10:52:36 +02:00
|
|
|
* Send a Exception Message to all Connected Admins
|
2014-08-23 12:59:08 +02:00
|
|
|
*
|
2020-05-25 10:52:36 +02:00
|
|
|
* @param \Exception $exception
|
2014-08-23 12:59:08 +02:00
|
|
|
* @param int $minLevel
|
|
|
|
* @param bool|string $prefix
|
2020-05-25 10:52:36 +02:00
|
|
|
*/
|
|
|
|
public function sendExceptionToAdmins(\Exception $exception, $minLevel = AuthenticationManager::AUTH_LEVEL_MODERATOR, $prefix = true) {
|
|
|
|
$format = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_FORMAT_ERROR);
|
|
|
|
$message = $format . "Exception: '{$exception->getMessage()}' ({$exception->getCode()})";
|
|
|
|
$this->sendMessageToAdmins($message, $minLevel, $prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send an information message to the given login
|
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
* @param string $login
|
|
|
|
* @param string|bool $prefix
|
|
|
|
* @param bool $multiCall
|
2014-08-23 12:59:08 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2020-05-25 10:52:36 +02:00
|
|
|
public function sendInformation($message, $login = null, $prefix = true, $multiCall = true) {
|
2014-08-23 12:59:08 +02:00
|
|
|
$format = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_FORMAT_INFORMATION);
|
2020-05-25 10:52:36 +02:00
|
|
|
return $this->sendChat($format . $message, $login, $prefix, $multiCall);
|
2014-08-23 12:59:08 +02:00
|
|
|
}
|
|
|
|
|
2014-07-25 15:16:41 +02:00
|
|
|
/**
|
2020-05-25 10:52:36 +02:00
|
|
|
* Sends a Information Message to all connected Admins
|
2014-07-25 15:16:41 +02:00
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
* @param int $minLevel
|
|
|
|
* @param bool|string $prefix
|
|
|
|
* @return bool
|
|
|
|
*/
|
2020-05-25 10:52:36 +02:00
|
|
|
public function sendInformationToAdmins($message, $minLevel = AuthenticationManager::AUTH_LEVEL_MODERATOR, $prefix = true) {
|
|
|
|
$format = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_FORMAT_INFORMATION);
|
2014-07-25 15:16:41 +02:00
|
|
|
return $this->sendMessageToAdmins($format . $message, $minLevel, $prefix);
|
|
|
|
}
|
|
|
|
|
2014-05-02 17:40:47 +02:00
|
|
|
/**
|
2020-05-25 10:52:36 +02:00
|
|
|
* Send a Message to all connected Admins
|
2014-05-02 17:40:47 +02:00
|
|
|
*
|
2020-05-25 10:52:36 +02:00
|
|
|
* @param string $message
|
|
|
|
* @param int $minLevel
|
|
|
|
* @param bool|string $prefix
|
2014-05-02 17:40:47 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2020-05-25 10:52:36 +02:00
|
|
|
public function sendMessageToAdmins($message, $minLevel = AuthenticationManager::AUTH_LEVEL_MODERATOR, $prefix = true) {
|
|
|
|
$admins = $this->maniaControl->getAuthenticationManager()->getConnectedAdmins($minLevel);
|
|
|
|
return $this->sendChat($message, $admins, $prefix);
|
2014-05-02 17:40:47 +02:00
|
|
|
}
|
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
/**
|
2020-05-25 10:52:36 +02:00
|
|
|
* Send a success message to the given login
|
2013-11-09 17:24:03 +01:00
|
|
|
*
|
2014-01-16 18:08:32 +01:00
|
|
|
* @param string $message
|
|
|
|
* @param string $login
|
2020-05-25 10:52:36 +02:00
|
|
|
* @param bool|string $prefix
|
2013-11-10 13:46:44 +01:00
|
|
|
* @return bool
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2020-05-25 10:52:36 +02:00
|
|
|
public function sendSuccess($message, $login = null, $prefix = true) {
|
|
|
|
$format = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_FORMAT_SUCCESS);
|
2014-05-02 18:21:38 +02:00
|
|
|
return $this->sendChat($format . $message, $login, $prefix);
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
2013-11-13 01:43:12 +01:00
|
|
|
|
2020-05-25 10:52:36 +02:00
|
|
|
|
2014-05-20 14:59:53 +02:00
|
|
|
/**
|
2020-05-25 10:52:36 +02:00
|
|
|
* Sends a Success Message to all connected Admins
|
2014-05-20 14:59:53 +02:00
|
|
|
*
|
2020-05-25 10:52:36 +02:00
|
|
|
* @param string $message
|
2014-05-20 14:59:53 +02:00
|
|
|
* @param int $minLevel
|
|
|
|
* @param bool|string $prefix
|
2020-05-25 10:52:36 +02:00
|
|
|
* @return bool
|
2014-05-20 14:59:53 +02:00
|
|
|
*/
|
2020-05-25 10:52:36 +02:00
|
|
|
public function sendSuccessToAdmins($message, $minLevel = AuthenticationManager::AUTH_LEVEL_MODERATOR, $prefix = true) {
|
|
|
|
$format = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_FORMAT_SUCCESS);
|
|
|
|
return $this->sendMessageToAdmins($format . $message, $minLevel, $prefix);
|
2014-05-20 14:59:53 +02:00
|
|
|
}
|
|
|
|
|
2013-11-13 01:43:12 +01:00
|
|
|
/**
|
|
|
|
* Send an usage info message to the given login
|
|
|
|
*
|
2014-01-16 18:08:32 +01:00
|
|
|
* @param string $message
|
|
|
|
* @param string $login
|
|
|
|
* @param string|bool $prefix
|
2013-11-13 01:43:12 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function sendUsageInfo($message, $login = null, $prefix = false) {
|
2014-08-11 19:24:21 +02:00
|
|
|
$format = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_FORMAT_USAGEINFO);
|
2014-05-02 18:21:38 +02:00
|
|
|
return $this->sendChat($format . $message, $login, $prefix);
|
2013-11-13 01:43:12 +01:00
|
|
|
}
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|