From 3984dd3d9b4dc45005913b7b0914b26667c93830 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Schro=CC=88der?= Date: Thu, 28 Nov 2013 01:26:13 +0100 Subject: [PATCH] - Chatlog Plugin --- application/plugins/Chatlog.php | 82 +++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 application/plugins/Chatlog.php diff --git a/application/plugins/Chatlog.php b/application/plugins/Chatlog.php new file mode 100644 index 00000000..b699da39 --- /dev/null +++ b/application/plugins/Chatlog.php @@ -0,0 +1,82 @@ +maniaControl = $maniaControl; + + $this->name = 'Chatlog Plugin'; + $this->version = self::VERSION; + $this->author = 'steeffeen'; + $this->description = 'Plugin logging the chat messages of the server.'; + + // Init settings + $this->maniaControl->settingManager->initSetting($this, self::SETTING_FILENAME, 'chat.log'); + $this->maniaControl->settingManager->initSetting($this, self::SETTING_LOGSERVERMESSAGES, true); + + // Get settings + $fileName = $this->maniaControl->settingManager->getSetting($this, self::SETTING_FILENAME); + $fileName = FileUtil::getClearedFileName($fileName); + $this->fileName = ManiaControlDir . '/' . $fileName; + $this->logServerMessages = $this->maniaControl->settingManager->getSetting($this, self::SETTING_LOGSERVERMESSAGES); + + // Register for callbacks + $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCHAT, $this, + 'handlePlayerChatCallback'); + } + + /** + * Handle PlayerChat callback + * + * @param array $chatCallback + */ + 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 + * + * @param string $message + * @param string $login + */ + private function logText($text, $login = null) { + if (!$login) { + $login = ''; + } + $message = date(ManiaControl::DATE) . " >> {$login}: {$text}" . PHP_EOL; + file_put_contents($this->fileName, $message, FILE_APPEND); + } +} + +?>