Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * TeamSpeak 3 demo plugin
- *
- * Copyright (c) 2008-2012 TeamSpeak Systems GmbH
- */
- #pragma comment(lib, "ws2_32.lib")
- #ifdef _WIN32
- #pragma warning (disable : 4100) /* Disable Unreferenced parameter warning */
- #include <Windows.h>
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <assert.h>
- #include <process.h>
- #include "public_errors.h"
- #include "public_errors_rare.h"
- #include "public_definitions.h"
- #include "public_rare_definitions.h"
- #include "ts3_functions.h"
- #include "plugin.h"
- #include <WinSock.h>
- #include <errno.h>
- #include <string.h>
- #include <sys/types.h>
- #include <time.h>
- static struct TS3Functions ts3Functions;
- #ifdef _WIN32
- #define _strcpy(dest, destSize, src) strcpy_s(dest, destSize, src)
- #define snprintf sprintf_s
- #else
- #define _strcpy(dest, destSize, src) { strncpy(dest, src, destSize-1); (dest)[destSize-1] = '\0'; }
- #endif
- #define PLUGIN_API_VERSION 19
- #define PATH_BUFSIZE 512
- #define COMMAND_BUFSIZE 128
- #define INFODATA_BUFSIZE 128
- #define SERVERINFO_BUFSIZE 256
- #define CHANNELINFO_BUFSIZE 512
- #define RETURNCODE_BUFSIZE 128
- static char* pluginID = NULL;
- #define MY_MESSAGE_NOTIFICATION 1048 //Custom notification message
- #ifdef _WIN32
- /* Helper function to convert wchar_T to Utf-8 encoded strings on Windows */
- static int wcharToUtf8(const wchar_t* str, char** result) {
- int outlen = WideCharToMultiByte(CP_UTF8, 0, str, -1, 0, 0, 0, 0);
- *result = (char*)malloc(outlen);
- if(WideCharToMultiByte(CP_UTF8, 0, str, -1, *result, outlen, 0, 0) == 0) {
- *result = NULL;
- return -1;
- }
- return 0;
- }
- #endif
- /*********************************** Required functions ************************************/
- /*
- * If any of these required functions is not implemented, TS3 will refuse to load the plugin
- */
- /* Unique name identifying this plugin */
- const char* ts3plugin_name() {
- return "MAVS";
- }
- /* Plugin version */
- const char* ts3plugin_version() {
- return "0.1";
- }
- /* Plugin API version. Must be the same as the clients API major version, else the plugin fails to load. */
- int ts3plugin_apiVersion() {
- return PLUGIN_API_VERSION;
- }
- /* Plugin author */
- const char* ts3plugin_author() {
- /* If you want to use wchar_t, see ts3plugin_name() on how to use */
- return "X39";
- }
- /* Plugin description */
- const char* ts3plugin_description() {
- /* If you want to use wchar_t, see ts3plugin_name() on how to use */
- return "The Minecraft-Advanced-Voicechat-System [MAVS] is used to create a nearly realistic sound feeling for the players who use it";
- }
- /* Set TeamSpeak 3 callback functions */
- void ts3plugin_setFunctionPointers(const struct TS3Functions funcs) {
- ts3Functions = funcs;
- }
- void THREAD_startServerThread(void* params);
- void THREAD_server(void* params);
- void ts3plugin_shutdown();
- bool executeMessage(char buffer[]);
- static SOCKET serverSocket;
- static SOCKET connectionSocket;
- static WSADATA wsaData;
- static int killthread;
- int ts3plugin_init() {
- char appPath[PATH_BUFSIZE];
- char resourcesPath[PATH_BUFSIZE];
- char configPath[PATH_BUFSIZE];
- char pluginPath[PATH_BUFSIZE];
- /* Your plugin init code here */
- printf("PLUGIN: init\n");
- /* Example on how to query application, resources and configuration paths from client */
- /* Note: Console client returns empty string for app and resources path */
- ts3Functions.getAppPath(appPath, PATH_BUFSIZE);
- ts3Functions.getResourcesPath(resourcesPath, PATH_BUFSIZE);
- ts3Functions.getConfigPath(configPath, PATH_BUFSIZE);
- ts3Functions.getPluginPath(pluginPath, PATH_BUFSIZE);
- killthread = 0;
- printf("PLUGIN: Starting plugin network thread");
- _beginthread(THREAD_startServerThread, 0, NULL);
- printf("PLUGIN: App path: %s\nResources path: %s\nConfig path: %s\nPlugin path: %s\n", appPath, resourcesPath, configPath, pluginPath);
- return 0; /* 0 = success, 1 = failure, -2 = failure but client will not show a "failed to load" warning */
- /* -2 is a very special case and should only be used if a plugin displays a dialog (e.g. overlay) asking the user to disable
- * the plugin again, avoiding the show another dialog by the client telling the user the plugin failed to load.
- * For normal case, if a plugin really failed to load because of an error, the correct return value is 1. */
- }
- void shutdownPlugin(char* s)
- {
- printf(s);
- ts3plugin_shutdown();
- }
- void THREAD_startServerThread(void* params){
- _beginthread(THREAD_server, 0, NULL);
- //killthread watchloop
- while(1)
- {
- if(killthread >= 1)
- {
- //cleanup connection
- WSACancelBlockingCall();
- if (closesocket(serverSocket) < 0)
- printf("Error closing serverSocket socket in parent.");
- if (closesocket(connectionSocket) < 0)
- printf("Error closing connectionSocket socket in parent.");
- if (WSACleanup() < 0)
- printf("Error cleaning up WSA.");
- killthread--;
- break;
- }
- }
- }
- void THREAD_server(void* params){
- SOCKADDR_IN serv_addr;
- char buffer[255];
- WSAStartup(MAKEWORD(2, 0), &wsaData);
- if ((serverSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
- shutdownPlugin("Couldn't create listening socket.");
- memset(&serv_addr, 0, sizeof(serv_addr));
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
- serv_addr.sin_port = htons(30153);
- if (bind(serverSocket, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
- shutdownPlugin("Couldn't bind listening socket.");
- if (listen(serverSocket, 1) < 0 )
- shutdownPlugin("Call to listen failed.");
- if ((connectionSocket = accept(serverSocket, NULL, NULL)) < 0)
- shutdownPlugin("Error calling accept()");
- while (1) {
- if(killthread >= 1)
- break;
- }
- killthread--;
- }
- /* Custom code called right before the plugin is unloaded */
- void ts3plugin_shutdown() {
- /* Your plugin cleanup code here */
- printf("PLUGIN: shutdown\n");
- /*
- * Note:
- * If your plugin implements a settings dialog, it must be closed and deleted here, else the
- * TeamSpeak client will most likely crash (DLL removed but dialog from DLL code still open).
- */
- killthread = 2;
- while(killthread != 0);
- /* Free pluginID if we registered it */
- if(pluginID) {
- free(pluginID);
- pluginID = NULL;
- }
- }
- /****************************** Optional functions ********************************/
- /*
- * Following functions are optional, if not needed you don't need to implement them.
- */
- /* Tell client if plugin offers a configuration window. If this function is not implemented, it's an assumed "does not offer" (PLUGIN_OFFERS_NO_CONFIGURE). */
- int ts3plugin_offersConfigure() {
- printf("PLUGIN: offersConfigure\n");
- /*
- * Return values:
- * PLUGIN_OFFERS_NO_CONFIGURE - Plugin does not implement ts3plugin_configure
- * PLUGIN_OFFERS_CONFIGURE_NEW_THREAD - Plugin does implement ts3plugin_configure and requests to run this function in an own thread
- * PLUGIN_OFFERS_CONFIGURE_QT_THREAD - Plugin does implement ts3plugin_configure and requests to run this function in the Qt GUI thread
- */
- return PLUGIN_OFFERS_NO_CONFIGURE; /* In this case ts3plugin_configure does not need to be implemented */
- }
- /* Plugin might offer a configuration window. If ts3plugin_offersConfigure returns 0, this function does not need to be implemented. */
- void ts3plugin_configure(void* handle, void* qParentWidget) {
- printf("PLUGIN: configure\n");
- }
- /*
- * If the plugin wants to use error return codes, plugin commands, hotkeys or menu items, it needs to register a command ID. This function will be
- * automatically called after the plugin was initialized. This function is optional. If you don't use these features, this function can be omitted.
- * Note the passed pluginID parameter is no longer valid after calling this function, so you must copy it and store it in the plugin.
- */
- void ts3plugin_registerPluginID(const char* id) {
- //const size_t sz = strlen(id) + 1;
- //pluginID = (char*)malloc(sz * sizeof(char));
- //_strcpy(pluginID, sz, id); /* The id buffer will invalidate after exiting this function */
- //printf("PLUGIN: registerPluginID: %s\n", pluginID);
- }
- /* Plugin command keyword. Return NULL or "" if not used. */
- const char* ts3plugin_commandKeyword() {
- return NULL;
- }
- /* Plugin processes console command. Return 0 if plugin handled the command, 1 if not handled. */
- int ts3plugin_processCommand(uint64 serverConnectionHandlerID, const char* command) {
- return 1;
- }
- void ts3plugin_currentServerConnectionChanged(uint64 serverConnectionHandlerID) {
- }
- const char* ts3plugin_infoTitle() {
- return "USING MAVS"; //TODO
- }
- /*
- * Dynamic content shown in the right column in the info frame. Memory for the data string needs to be allocated in this
- * function. The client will call ts3plugin_freeMemory once done with the string to release the allocated memory again.
- * Check the parameter "type" if you want to implement this feature only for specific item types. Set the parameter
- * "data" to NULL to have the client ignore the info data.
- */
- void ts3plugin_infoData(uint64 serverConnectionHandlerID, uint64 id, enum PluginItemType type, char** data) {
- data = NULL;
- // char* name;
- //
- // /* For demonstration purpose, display the name of the currently selected server, channel or client. */
- // switch(type) {
- // case PLUGIN_SERVER:
- // if(ts3Functions.getServerVariableAsString(serverConnectionHandlerID, VIRTUALSERVER_NAME, &name) != ERROR_ok) {
- // printf("Error getting virtual server name\n");
- // return;
- // }
- // break;
- // case PLUGIN_CHANNEL:
- // if(ts3Functions.getChannelVariableAsString(serverConnectionHandlerID, id, CHANNEL_NAME, &name) != ERROR_ok) {
- // printf("Error getting channel name\n");
- // return;
- // }
- // break;
- // case PLUGIN_CLIENT:
- // if(ts3Functions.getClientVariableAsString(serverConnectionHandlerID, (anyID)id, CLIENT_NICKNAME, &name) != ERROR_ok) {
- // printf("Error getting client nickname\n");
- // return;
- // }
- // break;
- // default:
- // printf("Invalid item type: %d\n", type);
- // data = NULL; /* Ignore */
- // return;
- // }
- //
- // *data = (char*)malloc(INFODATA_BUFSIZE * sizeof(char)); /* Must be allocated in the plugin! */
- // snprintf(*data, INFODATA_BUFSIZE, "The nickname is [I]\"%s\"[/I]", name); /* bbCode is supported. HTML is not supported */
- // ts3Functions.freeMemory(name);
- }
- /* Required to release the memory for parameter "data" allocated in ts3plugin_infoData and ts3plugin_initMenus */
- void ts3plugin_freeMemory(void* data) {
- free(data);
- }
- /*
- * Plugin requests to be always automatically loaded by the TeamSpeak 3 client unless
- * the user manually disabled it in the plugin dialog.
- * This function is optional. If missing, no autoload is assumed.
- */
- int ts3plugin_requestAutoload() {
- return 0; /* 1 = request autoloaded, 0 = do not request autoload */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement