-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.cpp
323 lines (277 loc) · 13.2 KB
/
plugin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* TeamSpeak 3 demo plugin
*
* Copyright (c) 2008-2017 TeamSpeak Systems GmbH
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "teamspeak/public_errors.h"
#include "teamspeak/public_errors_rare.h"
#include "teamspeak/public_definitions.h"
#include "teamspeak/public_rare_definitions.h"
#include "teamspeak/clientlib_publicdefinitions.h"
#include "ts3_functions.h"
#include "API.h"
#ifdef _WIN32
#pragma warning (disable : 4100) /* Disable Unreferenced parameter warning */
#include <Windows.h>
#endif
#include "plugin.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 24
#define PATH_BUFSIZE 512
#define COMMAND_BUFSIZE 128
#define INFODATA_BUFSIZE 128
#define SERVERINFO_BUFSIZE 256
#define CHANNELINFO_BUFSIZE 512
#define RETURNCODE_BUFSIZE 128
#define API TS3_API
#define User API::TS3User
static char* pluginID = NULL;
static UINT WM_TEAMSPEAK3_JSON_API_STARTED = 0;
/*********************************** Required functions ************************************/
/*
* If any of these required functions is not implemented, TS3 will refuse to load the plugin
*/
const char* ts3plugin_name() {
return "Teamspeak 3 WinAPI";
}
const char* ts3plugin_version() {
return "1";
}
int ts3plugin_apiVersion() {
return PLUGIN_API_VERSION;
}
const char* ts3plugin_author() {
return "Jon @ https://github.com/3vcloud";
}
const char* ts3plugin_description() {
return "This plugin creates a locally hosted websocket for applications to access teamspeak info via JSON API";
}
int ts3plugin_requestAutoload() {
return 1;
}
/* Set TeamSpeak 3 callback functions */
void ts3plugin_setFunctionPointers(const struct TS3Functions funcs) {
ts3Functions = funcs;
}
// Returns server connection id on success, or 0 on fail
uint64 is_connected() {
ConnectStatus result = ConnectStatus::STATUS_DISCONNECTED;
int status = 0;
uint64 conn = ts3Functions.getCurrentServerConnectionHandlerID();
if (!conn)
return 0;
if (ts3Functions.getConnectionStatus(conn, &status) != ERROR_ok)
return 0;
if (static_cast<ConnectStatus>(status) != ConnectStatus::STATUS_CONNECTION_ESTABLISHED)
return 0;
return conn;
}
User* getUser(uint64 serverConnectionHandlerID, anyID clientID, bool fresh = false, bool* has_changed = nullptr) {
if (!is_connected())
return nullptr;
User* user = fresh ? nullptr : API::Instance().getUser(clientID);
if (!user) user = new User();
// NB: No error checking here, because not much we can do about it
int ret = ERROR_ok;
if((ret = ts3Functions.getClientDisplayName(serverConnectionHandlerID, clientID, user->name, sizeof(user->name))) != ERROR_ok)
API::onLogMessage("Failed to get getClientDisplayName, code 0x%04x", ret);
uint64 status = 0;
if((ret = ts3Functions.getClientVariableAsUInt64(serverConnectionHandlerID, clientID, CLIENT_FLAG_TALKING, &status)) == ERROR_ok)
user->is_talking = status == 1 ? 1 : 0;
else API::onLogMessage("Failed to get CLIENT_FLAG_TALKING, code 0x%04x", ret);
if ((ret = ts3Functions.getClientVariableAsUInt64(serverConnectionHandlerID, clientID, CLIENT_OUTPUT_MUTED, &status)) == ERROR_ok)
user->speakers_muted = status == 1 ? 1 : 0;
else API::onLogMessage("Failed to get CLIENT_OUTPUT_MUTED, code 0x%04x", ret);
if ((ret = ts3Functions.getClientVariableAsUInt64(serverConnectionHandlerID, clientID, CLIENT_INPUT_MUTED, &status)) == ERROR_ok)
user->mic_muted = status == 1 ? 1 : 0;
else API::onLogMessage("Failed to get CLIENT_INPUT_MUTED, code 0x%04x", ret);
if ((ret = ts3Functions.getChannelOfClient(serverConnectionHandlerID, clientID, &user->channel_id)) != ERROR_ok)
API::onLogMessage("Failed to get getChannelOfClient, code 0x%04x", ret);
return TS3_API::Instance().updateUser(clientID, user, has_changed);
}
int refreshAll() {
uint64 serverConnectionHandlerID = is_connected();
auto& instance = API::Instance();
auto& server = instance.server;
bool need_to_send = false;
int ret = ERROR_ok;
if (!is_connected()) {
// Not fully connected; make sure client and server vars are flushed
if (instance.server.host) {
instance.resetServerVariables();
instance.sendAllInfo();
}
instance.server.host[0] = 0;
return 0;
}
bool new_connection = !instance.server.host;
need_to_send |= new_connection;
char* s;
if ((ret = ts3Functions.getServerVariableAsString(serverConnectionHandlerID, VIRTUALSERVER_NAME, &s)) != ERROR_ok) {
API::onLogMessage("Failed to get server name, code 0x%04x", ret);
goto send_if_needed;
}
if (strcmp(s, server.name) != 0) {
need_to_send = true;
// Server has changed; update details
strncpy_s(server.name, s, sizeof(server.name) - 1);
ts3Functions.freeMemory(s);
// Get server info
if ((ret = ts3Functions.getServerConnectInfo(serverConnectionHandlerID, server.host, &server.port, server.password, sizeof(server.password))) != ERROR_ok) {
API::onLogMessage("Failed to get getServerConnectInfo, code 0x%04x", ret);
goto send_if_needed;
}
if ((ret = ts3Functions.getServerVariableAsString(serverConnectionHandlerID, VIRTUALSERVER_NAME, &s)) != ERROR_ok) {
API::onLogMessage("Failed to get server name, code 0x%04x", ret);
goto send_if_needed;
}
strncpy_s(server.name, s, sizeof(server.name) - 1);
ts3Functions.freeMemory(s);
}
else {
ts3Functions.freeMemory(s);
}
if ((ret = ts3Functions.getClientID(serverConnectionHandlerID, &server.my_client_id)) != ERROR_ok) {
API::onLogMessage("Failed to get my id, code 0x%04x", ret);
goto send_if_needed;
}
// Update all connected users
anyID* clientIDs;
if ((ret = ts3Functions.getClientList(serverConnectionHandlerID, &clientIDs)) != ERROR_ok) {
API::onLogMessage("Failed to get getClientList, code 0x%04x", ret);
goto send_if_needed;
}
bool has_changed = false;
for (size_t i = 0; clientIDs[i] != 0; i++) {
getUser(serverConnectionHandlerID, clientIDs[i], false, &has_changed);
need_to_send |= has_changed;
}
ts3Functions.freeMemory(clientIDs);
send_if_needed:
if (need_to_send) {
instance.sendAllInfo();
}
return ret;
}
int ts3plugin_updateAPIServer() {
return refreshAll();
}
/*
* Custom code called right after loading the plugin. Returns 0 on success, 1 on failure.
* If the function returns 1 on failure, the plugin will be unloaded again.
*/
int ts3plugin_init() {
TS3_API::Instance();
WM_TEAMSPEAK3_JSON_API_STARTED = RegisterWindowMessageW(L"WM_TEAMSPEAK3_JSON_API_STARTED");
SendNotifyMessageW(HWND_BROADCAST, WM_TEAMSPEAK3_JSON_API_STARTED, NULL, NULL);
return 0;
}
/* Custom code called right before the plugin is unloaded */
void ts3plugin_shutdown() { /* Your plugin cleanup code here */ }
/****************************** Optional functions ********************************/
/*
* Following functions are optional, if not needed you don't need to implement them.
*/
/* Client changed current server connection handler */
void ts3plugin_currentServerConnectionChanged(uint64 serverConnectionHandlerID) {
printf("PLUGIN: currentServerConnectionChanged %llu (%llu)\n", (long long unsigned int)serverConnectionHandlerID, (long long unsigned int)ts3Functions.getCurrentServerConnectionHandlerID());
ts3plugin_updateAPIServer();
}
/* Required to release the memory for parameter "data" allocated in ts3plugin_infoData and ts3plugin_initMenus */
void ts3plugin_freeMemory(void* data) {
free(data);
}
/************************** TeamSpeak callbacks ***************************/
/*
* Following functions are optional, feel free to remove unused callbacks.
* See the clientlib documentation for details on each function.
*/
/* Clientlib */
void ts3plugin_onConnectStatusChangeEvent(uint64 serverConnectionHandlerID, int newStatus, unsigned int errorNumber) {
/* Some example code following to show how to use the information query functions. */
ts3plugin_updateAPIServer();
}
void ts3plugin_onUpdateChannelEvent(uint64 serverConnectionHandlerID, uint64 channelID) {
}
void ts3plugin_onUpdateChannelEditedEvent(uint64 serverConnectionHandlerID, uint64 channelID, anyID invokerID, const char* invokerName, const char* invokerUniqueIdentifier) {
}
void ts3plugin_onUpdateClientEvent(uint64 serverConnectionHandlerID, anyID clientID, anyID invokerID, const char* invokerName, const char* invokerUniqueIdentifier) {
getUser(serverConnectionHandlerID, clientID, true);
}
void ts3plugin_channeUpdate(uint64 serverConnectionHandlerID, anyID clientID, uint64 channelID) {
User* user = getUser(serverConnectionHandlerID, clientID);
if (!user)
return;
user->channel_id = channelID;
TS3_API::Instance().sendClient(clientID);
}
void ts3plugin_onClientMoveEvent(uint64 serverConnectionHandlerID, anyID clientID, uint64 oldChannelID, uint64 newChannelID, int visibility, const char* moveMessage) {
ts3plugin_channeUpdate(serverConnectionHandlerID, clientID, newChannelID);
}
void ts3plugin_onClientKickFromChannelEvent(uint64 serverConnectionHandlerID, anyID clientID, uint64 oldChannelID, uint64 newChannelID, int visibility, anyID kickerID, const char* kickerName, const char* kickerUniqueIdentifier, const char* kickMessage) {
ts3plugin_channeUpdate(serverConnectionHandlerID, clientID, newChannelID);
}
void ts3plugin_onClientKickFromServerEvent(uint64 serverConnectionHandlerID, anyID clientID, uint64 oldChannelID, uint64 newChannelID, int visibility, anyID kickerID, const char* kickerName, const char* kickerUniqueIdentifier, const char* kickMessage) {
API::Instance().deleteUser(clientID);
}
void ts3plugin_onServerEditedEvent(uint64 serverConnectionHandlerID, anyID editerID, const char* editerName, const char* editerUniqueIdentifier) {
ts3plugin_updateAPIServer();
}
void ts3plugin_onServerStopEvent(uint64 serverConnectionHandlerID, const char* shutdownMessage) {
ts3plugin_updateAPIServer();
}
int ts3plugin_onTextMessageEvent(uint64 serverConnectionHandlerID, anyID targetMode, anyID toID, anyID fromID, const char* fromName, const char* fromUniqueIdentifier, const char* message, int ffIgnored) {
User* from = getUser(serverConnectionHandlerID, fromID, false);
if (!from)
return ffIgnored;
User* to = getUser(serverConnectionHandlerID, toID, false);
API::Instance().onTeamspeakMessage(from, to, message);
API::Instance().onLogMessage("PLUGIN: onTextMessageEvent %llu %d %d %s %s %d\n", (long long unsigned int)serverConnectionHandlerID, targetMode, fromID, fromName, message, ffIgnored);
return ffIgnored; /* 0 = handle normally, 1 = client will ignore the text message */
}
void ts3plugin_onTalkStatusChangeEvent(uint64 serverConnectionHandlerID, int status, int isReceivedWhisper, anyID clientID) {
/* Demonstrate usage of getClientDisplayName */
User* from = getUser(serverConnectionHandlerID, clientID, false);
if (!from) return;
from->is_talking = status == 1 ? 1 : 0;
API::Instance().sendClient(clientID);
}
/* Clientlib rare */
void ts3plugin_onClientChannelGroupChangedEvent(uint64 serverConnectionHandlerID, uint64 channelGroupID, uint64 channelID, anyID clientID, anyID invokerClientID, const char* invokerName, const char* invokerUniqueIdentity) {
getUser(serverConnectionHandlerID, clientID);
}
void ts3plugin_onServerGroupClientAddedEvent(uint64 serverConnectionHandlerID, anyID clientID, const char* clientName, const char* clientUniqueIdentity, uint64 serverGroupID, anyID invokerClientID, const char* invokerName, const char* invokerUniqueIdentity) {
getUser(serverConnectionHandlerID, clientID);
}
void ts3plugin_onServerGroupClientDeletedEvent(uint64 serverConnectionHandlerID, anyID clientID, const char* clientName, const char* clientUniqueIdentity, uint64 serverGroupID, anyID invokerClientID, const char* invokerName, const char* invokerUniqueIdentity) {
getUser(serverConnectionHandlerID, clientID);
}
void ts3plugin_onServerLogEvent(uint64 serverConnectionHandlerID, const char* logMsg) {
}
/* Client UI callbacks */
/*
* Called from client when an avatar image has been downloaded to or deleted from cache.
* This callback can be called spontaneously or in response to ts3Functions.getAvatar()
*/
void ts3plugin_onAvatarUpdated(uint64 serverConnectionHandlerID, anyID clientID, const char* avatarPath) {
getUser(serverConnectionHandlerID, clientID);
/* If avatarPath is NULL, the avatar got deleted */
/* If not NULL, avatarPath contains the path to the avatar file in the TS3Client cache */
}
/* Called when client custom nickname changed */
void ts3plugin_onClientDisplayNameChanged(uint64 serverConnectionHandlerID, anyID clientID, const char* displayName, const char* uniqueClientIdentifier) {
User* user = getUser(serverConnectionHandlerID, clientID, false);
if (!user) return;
strncpy_s(user->name, displayName, sizeof(user->name));
API::Instance().sendClient(clientID);
}