-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
88 lines (67 loc) · 2.51 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include "config.h"
#include "collections/collections.h"
#include "utilities.h"
#include "bot/compare.h"
#include "communication/communication.h"
#include "highLevelFunctions/highLevelFunctions.h"
#include "database/database.h"
char *hostnameToIP(char *hostname){
struct hostent *he;
if((he = gethostbyname(hostname)) == NULL){
fprintf(stderr, "Nie mozna odczytac IP\n%s\n", strerror(errno));
exit(errno);
}
return inet_ntoa(*((struct in_addr *)(he->h_addr_list[0])));
}
void initDatabase(){
struct list *stats = execQuery("SELECT `TS_clid` FROM `connections` WHERE `connection_stop` IS NULL;");
for (struct list *it = stats; it != NULL; it = it->next) {
char query[1024];
sprintf(query, "CALL `client_leave`('%s', '0');", treeGetValue(it->tree, "TS_clid"));
listFree(execQuery(query));
}
}
int main() {
char *address = hostnameToIP(SERVER_ADDRESS);
int connection = connectTeamSpeak(address, SERVER_PORT);
char buffer[256];
struct result res;
sprintf(buffer, "login %s %s\n", TS3_SERVER_ADMIN_LOGIN, TS3_SERVER_ADMIN_PASSWORD);
res = executeCommandWithBooleanResponse(connection, buffer);
if (res.error != NULL) {
fprintf(stderr, "%s\n%s\n", buffer, res.error);
free(res.error);
exit(1);
}
sprintf(buffer, "use %d client_nickname=%s\n", TS3_SERVER_ID, TS3_BOT_NAME);
res = executeCommandWithBooleanResponse(connection, buffer);
if (res.error != NULL) {
fprintf(stderr, "%s\n%s\n", buffer, res.error);
free(res.error);
exit(1);
}
initDatabase();
struct list *clientListOld = clientList(connection, "-uid -voice -times -groups -info -country -ip");
struct list *clientListNew = NULL;
struct list *channelListOld = channelList(connection, NULL);
struct list *channelListNew = NULL;
sleep(TS3_BOT_REFRESH_TIME);
while (1) {
clientListNew = clientList(connection, "-uid -voice -times -groups -info -country -ip");
channelListNew = channelList(connection, NULL);
compareClientList(connection, clientListOld, clientListNew);
compareChannelList(connection, channelListOld, channelListNew);
listFree(channelListOld);
listFree(clientListOld);
channelListOld = channelListNew;
clientListOld = clientListNew;
sleep(TS3_BOT_REFRESH_TIME);
}
listFree(clientListOld);
listFree(channelListOld);
return 0;
}