Advertisement
X39

Teamspeakplugin

X39
Feb 15th, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.84 KB | None | 0 0
  1. /*
  2.  * TeamSpeak 3 demo plugin
  3.  *
  4.  * Copyright (c) 2008-2012 TeamSpeak Systems GmbH
  5.  */
  6. #pragma comment(lib, "ws2_32.lib")
  7.  
  8. #ifdef _WIN32
  9. #pragma warning (disable : 4100)  /* Disable Unreferenced parameter warning */
  10. #include <Windows.h>
  11. #endif
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <assert.h>
  17. #include <process.h>
  18. #include "public_errors.h"
  19. #include "public_errors_rare.h"
  20. #include "public_definitions.h"
  21. #include "public_rare_definitions.h"
  22. #include "ts3_functions.h"
  23. #include "plugin.h"
  24.  
  25. #include <WinSock.h>
  26. #include <errno.h>
  27. #include <string.h>
  28. #include <sys/types.h>
  29. #include <time.h>
  30.  
  31.  
  32. static struct TS3Functions ts3Functions;
  33.  
  34. #ifdef _WIN32
  35. #define _strcpy(dest, destSize, src) strcpy_s(dest, destSize, src)
  36. #define snprintf sprintf_s
  37. #else
  38. #define _strcpy(dest, destSize, src) { strncpy(dest, src, destSize-1); (dest)[destSize-1] = '\0'; }
  39. #endif
  40.  
  41. #define PLUGIN_API_VERSION 19
  42.  
  43. #define PATH_BUFSIZE 512
  44. #define COMMAND_BUFSIZE 128
  45. #define INFODATA_BUFSIZE 128
  46. #define SERVERINFO_BUFSIZE 256
  47. #define CHANNELINFO_BUFSIZE 512
  48. #define RETURNCODE_BUFSIZE 128
  49. static char* pluginID = NULL;
  50.  
  51. #define MY_MESSAGE_NOTIFICATION      1048 //Custom notification message
  52.  
  53. #ifdef _WIN32
  54. /* Helper function to convert wchar_T to Utf-8 encoded strings on Windows */
  55. static int wcharToUtf8(const wchar_t* str, char** result) {
  56.     int outlen = WideCharToMultiByte(CP_UTF8, 0, str, -1, 0, 0, 0, 0);
  57.     *result = (char*)malloc(outlen);
  58.     if(WideCharToMultiByte(CP_UTF8, 0, str, -1, *result, outlen, 0, 0) == 0) {
  59.         *result = NULL;
  60.         return -1;
  61.     }
  62.     return 0;
  63. }
  64. #endif
  65.  
  66. /*********************************** Required functions ************************************/
  67. /*
  68.  * If any of these required functions is not implemented, TS3 will refuse to load the plugin
  69.  */
  70.  
  71. /* Unique name identifying this plugin */
  72. const char* ts3plugin_name() {
  73.     return "MAVS";
  74. }
  75.  
  76. /* Plugin version */
  77. const char* ts3plugin_version() {
  78.     return "0.1";
  79. }
  80.  
  81. /* Plugin API version. Must be the same as the clients API major version, else the plugin fails to load. */
  82. int ts3plugin_apiVersion() {
  83.     return PLUGIN_API_VERSION;
  84. }
  85.  
  86. /* Plugin author */
  87. const char* ts3plugin_author() {
  88.     /* If you want to use wchar_t, see ts3plugin_name() on how to use */
  89.     return "X39";
  90. }
  91.  
  92. /* Plugin description */
  93. const char* ts3plugin_description() {
  94.     /* If you want to use wchar_t, see ts3plugin_name() on how to use */
  95.     return "The Minecraft-Advanced-Voicechat-System [MAVS] is used to create a nearly realistic sound feeling for the players who use it";
  96. }
  97.  
  98. /* Set TeamSpeak 3 callback functions */
  99. void ts3plugin_setFunctionPointers(const struct TS3Functions funcs) {
  100.     ts3Functions = funcs;
  101. }
  102.  
  103.  
  104.  
  105. void THREAD_startServerThread(void* params);
  106. void THREAD_server(void* params);
  107. void ts3plugin_shutdown();
  108. bool executeMessage(char buffer[]);
  109.  
  110. static SOCKET serverSocket;
  111. static SOCKET connectionSocket;
  112. static WSADATA wsaData;
  113. static int killthread;
  114.  
  115. int ts3plugin_init() {
  116.     char appPath[PATH_BUFSIZE];
  117.     char resourcesPath[PATH_BUFSIZE];
  118.     char configPath[PATH_BUFSIZE];
  119.     char pluginPath[PATH_BUFSIZE];
  120.  
  121.     /* Your plugin init code here */
  122.     printf("PLUGIN: init\n");
  123.  
  124.     /* Example on how to query application, resources and configuration paths from client */
  125.     /* Note: Console client returns empty string for app and resources path */
  126.     ts3Functions.getAppPath(appPath, PATH_BUFSIZE);
  127.     ts3Functions.getResourcesPath(resourcesPath, PATH_BUFSIZE);
  128.     ts3Functions.getConfigPath(configPath, PATH_BUFSIZE);
  129.     ts3Functions.getPluginPath(pluginPath, PATH_BUFSIZE);
  130.  
  131.     killthread = 0;
  132.     printf("PLUGIN: Starting plugin network thread");
  133.  
  134.     _beginthread(THREAD_startServerThread, 0, NULL);
  135.  
  136.     printf("PLUGIN: App path: %s\nResources path: %s\nConfig path: %s\nPlugin path: %s\n", appPath, resourcesPath, configPath, pluginPath);
  137.  
  138.     return 0;  /* 0 = success, 1 = failure, -2 = failure but client will not show a "failed to load" warning */
  139.     /* -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
  140.      * the plugin again, avoiding the show another dialog by the client telling the user the plugin failed to load.
  141.      * For normal case, if a plugin really failed to load because of an error, the correct return value is 1. */
  142. }
  143. void shutdownPlugin(char* s)
  144. {
  145.     printf(s);
  146.     ts3plugin_shutdown();
  147. }
  148. void THREAD_startServerThread(void* params){
  149.     _beginthread(THREAD_server, 0, NULL);
  150.  
  151.     //killthread watchloop
  152.     while(1)
  153.     {
  154.         if(killthread >= 1)
  155.         {
  156.             //cleanup connection
  157.             WSACancelBlockingCall();
  158.             if (closesocket(serverSocket) < 0)
  159.                 printf("Error closing serverSocket socket in parent.");
  160.             if (closesocket(connectionSocket) < 0)
  161.                 printf("Error closing connectionSocket socket in parent.");
  162.             if (WSACleanup() < 0)
  163.                 printf("Error cleaning up WSA.");
  164.             killthread--;
  165.             break;
  166.         }
  167.     }
  168.  
  169. }
  170. void THREAD_server(void* params){
  171.     SOCKADDR_IN serv_addr;
  172.     char buffer[255];
  173.  
  174.  
  175.     WSAStartup(MAKEWORD(2, 0), &wsaData);
  176.  
  177.     if ((serverSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
  178.         shutdownPlugin("Couldn't create listening socket.");
  179.  
  180.     memset(&serv_addr, 0, sizeof(serv_addr));
  181.     serv_addr.sin_family = AF_INET;
  182.     serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  183.     serv_addr.sin_port = htons(30153);
  184.  
  185.     if (bind(serverSocket, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
  186.         shutdownPlugin("Couldn't bind listening socket.");
  187.  
  188.     if (listen(serverSocket, 1) < 0 )
  189.         shutdownPlugin("Call to listen failed.");
  190.  
  191.     if ((connectionSocket = accept(serverSocket, NULL, NULL)) < 0)
  192.         shutdownPlugin("Error calling accept()");
  193.  
  194.     while (1) {
  195.         if(killthread >= 1)
  196.             break;
  197.  
  198.  
  199.     }
  200.     killthread--;
  201.  
  202. }
  203.  
  204. /* Custom code called right before the plugin is unloaded */
  205. void ts3plugin_shutdown() {
  206.     /* Your plugin cleanup code here */
  207.     printf("PLUGIN: shutdown\n");
  208.  
  209.     /*
  210.      * Note:
  211.      * If your plugin implements a settings dialog, it must be closed and deleted here, else the
  212.      * TeamSpeak client will most likely crash (DLL removed but dialog from DLL code still open).
  213.      */
  214.     killthread = 2;
  215.     while(killthread != 0);
  216.  
  217.     /* Free pluginID if we registered it */
  218.     if(pluginID) {
  219.         free(pluginID);
  220.         pluginID = NULL;
  221.     }
  222. }
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242. /****************************** Optional functions ********************************/
  243. /*
  244.  * Following functions are optional, if not needed you don't need to implement them.
  245.  */
  246.  
  247. /* 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). */
  248. int ts3plugin_offersConfigure() {
  249.     printf("PLUGIN: offersConfigure\n");
  250.     /*
  251.      * Return values:
  252.      * PLUGIN_OFFERS_NO_CONFIGURE         - Plugin does not implement ts3plugin_configure
  253.      * PLUGIN_OFFERS_CONFIGURE_NEW_THREAD - Plugin does implement ts3plugin_configure and requests to run this function in an own thread
  254.      * PLUGIN_OFFERS_CONFIGURE_QT_THREAD  - Plugin does implement ts3plugin_configure and requests to run this function in the Qt GUI thread
  255.      */
  256.     return PLUGIN_OFFERS_NO_CONFIGURE;  /* In this case ts3plugin_configure does not need to be implemented */
  257. }
  258.  
  259. /* Plugin might offer a configuration window. If ts3plugin_offersConfigure returns 0, this function does not need to be implemented. */
  260. void ts3plugin_configure(void* handle, void* qParentWidget) {
  261.     printf("PLUGIN: configure\n");
  262. }
  263.  
  264. /*
  265.  * 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
  266.  * automatically called after the plugin was initialized. This function is optional. If you don't use these features, this function can be omitted.
  267.  * Note the passed pluginID parameter is no longer valid after calling this function, so you must copy it and store it in the plugin.
  268.  */
  269. void ts3plugin_registerPluginID(const char* id) {
  270.     //const size_t sz = strlen(id) + 1;
  271.     //pluginID = (char*)malloc(sz * sizeof(char));
  272.     //_strcpy(pluginID, sz, id);  /* The id buffer will invalidate after exiting this function */
  273.     //printf("PLUGIN: registerPluginID: %s\n", pluginID);
  274. }
  275.  
  276. /* Plugin command keyword. Return NULL or "" if not used. */
  277. const char* ts3plugin_commandKeyword() {
  278.     return NULL;
  279. }
  280.  
  281. /* Plugin processes console command. Return 0 if plugin handled the command, 1 if not handled. */
  282. int ts3plugin_processCommand(uint64 serverConnectionHandlerID, const char* command) {
  283.     return 1;
  284. }
  285.  
  286. void ts3plugin_currentServerConnectionChanged(uint64 serverConnectionHandlerID) {
  287. }
  288. const char* ts3plugin_infoTitle() {
  289.     return "USING MAVS"; //TODO
  290. }
  291.  
  292. /*
  293.  * Dynamic content shown in the right column in the info frame. Memory for the data string needs to be allocated in this
  294.  * function. The client will call ts3plugin_freeMemory once done with the string to release the allocated memory again.
  295.  * Check the parameter "type" if you want to implement this feature only for specific item types. Set the parameter
  296.  * "data" to NULL to have the client ignore the info data.
  297.  */
  298. void ts3plugin_infoData(uint64 serverConnectionHandlerID, uint64 id, enum PluginItemType type, char** data) {
  299.     data = NULL;
  300. //  char* name;
  301. //
  302. //  /* For demonstration purpose, display the name of the currently selected server, channel or client. */
  303. //  switch(type) {
  304. //      case PLUGIN_SERVER:
  305. //          if(ts3Functions.getServerVariableAsString(serverConnectionHandlerID, VIRTUALSERVER_NAME, &name) != ERROR_ok) {
  306. //              printf("Error getting virtual server name\n");
  307. //              return;
  308. //          }
  309. //          break;
  310. //      case PLUGIN_CHANNEL:
  311. //          if(ts3Functions.getChannelVariableAsString(serverConnectionHandlerID, id, CHANNEL_NAME, &name) != ERROR_ok) {
  312. //              printf("Error getting channel name\n");
  313. //              return;
  314. //          }
  315. //          break;
  316. //      case PLUGIN_CLIENT:
  317. //          if(ts3Functions.getClientVariableAsString(serverConnectionHandlerID, (anyID)id, CLIENT_NICKNAME, &name) != ERROR_ok) {
  318. //              printf("Error getting client nickname\n");
  319. //              return;
  320. //          }
  321. //          break;
  322. //      default:
  323. //          printf("Invalid item type: %d\n", type);
  324. //          data = NULL;  /* Ignore */
  325. //          return;
  326. //  }
  327. //
  328. //  *data = (char*)malloc(INFODATA_BUFSIZE * sizeof(char));  /* Must be allocated in the plugin! */
  329. //  snprintf(*data, INFODATA_BUFSIZE, "The nickname is [I]\"%s\"[/I]", name);  /* bbCode is supported. HTML is not supported */
  330. //  ts3Functions.freeMemory(name);
  331. }
  332.  
  333. /* Required to release the memory for parameter "data" allocated in ts3plugin_infoData and ts3plugin_initMenus */
  334. void ts3plugin_freeMemory(void* data) {
  335.     free(data);
  336. }
  337.  
  338. /*
  339.  * Plugin requests to be always automatically loaded by the TeamSpeak 3 client unless
  340.  * the user manually disabled it in the plugin dialog.
  341.  * This function is optional. If missing, no autoload is assumed.
  342.  */
  343. int ts3plugin_requestAutoload() {
  344.     return 0;  /* 1 = request autoloaded, 0 = do not request autoload */
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement