-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
339 lines (262 loc) · 9.21 KB
/
main.cc
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//***************************************************************************
// File main.cc
// Date 23.07.17 - #1
// Copyright (c) 2017-2017 s710 (s710 (at) posteo (dot) de). All rights reserved.
// --------------------------------------------------------------------------
// Ark ClusterChat
//***************************************************************************
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "def.h"
#include "clusterchat.hpp"
#include "thread.hpp"
#include "ini.h"
//***************************************************************************
// globals
//***************************************************************************
#define VERSION "1.1"
CondVar mainCond;
Mutex mainMutex;
int shouldExit = no;
char* lastSection= 0;
ServerConfig* lastConfig= 0;
int doHelp();
void doCopyright();
int doCommandline(int argc, char* argv[], std::list<ServerConfig*>* configs);
int parseServer(char* string, ServerConfig* cfg);
int iniHandler(void* user, const char* section, const char* name, const char* value);
int Globals::cfgVerbose= 0;
int Globals::cfgDebug= 0;
int Globals::cfgShowAdmin= 0;
//***************************************************************************
// signal processing
//***************************************************************************
static void* sig_thread(void *arg)
{
int sig;
sigset_t *set = (sigset_t*)arg;
for (;;)
{
if (!sigwait(set, &sig))
{
printf("[SIG Handler] Got signal: %s\n", strsignal(sig));
if (sig == SIGINT || sig == SIGTERM || sig == SIGQUIT)
{
printf("[SIG Handler] Exiting ...\n");
shouldExit= yes;
mainMutex.lock();
mainCond.broadcast();
mainMutex.unlock();
break;
}
}
}
return 0;
}
//***************************************************************************
// main
//***************************************************************************
int main(int argc, char* argv[])
{
int res= success;
std::list<ServerConfig*> configs;
if (argc == 1 || (argv[1] && (!strcmp(argv[1], "-v") || !strcmp(argv[1], "-?") || !strcmp(argv[1], "--help"))))
return doHelp();
doCopyright();
if (doCommandline(argc, argv, &configs) != success)
return fail;
printf("Main: Using servers:\n");
for (std::list<ServerConfig*>::iterator it= configs.begin(); it != configs.end(); ++it)
printf("Main: - %s@%s:%d\n", (*it)->title.c_str(), (*it)->host.c_str(), (*it)->port);
printf("Main: Starting (show admin cmd: %d, debug: %d, verbose: %d).\n", Globals::cfgShowAdmin, Globals::cfgDebug, Globals::cfgVerbose);
mainMutex.lock();
sigset_t set;
pthread_t handleThread;
ClusterChat clusterChat;
// Block SIGQUIT and SIGUSR1; other threads created by main()
// will inherit a copy of the signal mask.
sigemptyset(&set);
sigaddset(&set, SIGQUIT);
sigaddset(&set, SIGTERM);
sigaddset(&set, SIGINT);
pthread_sigmask(SIG_BLOCK, &set, NULL);
pthread_create(&handleThread, NULL, &sig_thread, (void *) &set);
res= clusterChat.init(&configs);
if (res)
{
fprintf(stderr, "ClusterChat: Error: Failed to start worker threads (%d), exiting ...\n", res);
clusterChat.shutdown();
return -1;
}
while (!shouldExit)
mainCond.timedWait(mainMutex, 3);
printf("Main: Exiting.\n");
pthread_join(handleThread, 0);
clusterChat.shutdown();
return 0;
}
//***************************************************************************
// main
//***************************************************************************
int doHelp()
{
doCopyright();
printf("\nUsage:\n");
printf(" $ arkclusterchat [OPTIONS]\n");
printf("Options:\n");
printf(" -s [SERVER] Add servers in the format: [TITLE]:[RCONPASSWORD]@[HOST]:[RCONPORT].\n");
printf(" For example '-s TheIsland:greatPassw0rd@myhost:32330'.\n");
printf(" Option can be repeated to set multiple servers. Title will be printed in\n");
printf(" game chat as well as application log. Make sure to use your server RCON port.'\n\n");
printf(" -c [FILE] Path to ini configuration file with server descriptions (as alternative to -s option).\n\n");
printf(" --verbose Print all messages sent/received.\n");
printf(" --debug Send chat messages ONLY to the server they have been received on.\n\n");
printf("The configuration file should have the following contents PER SERVER:\n\n");
printf(" [(TITLE)]\n");
printf(" host = (HOSTNAME)\n");
printf(" port = (RCONPORT)\n");
printf(" password = (RCONPASSWORD)\n\n");
printf(" Example:\n\n");
printf(" [TheIsland]\n");
printf(" host = 123.123.123.123\n");
printf(" port = 32330\n");
printf(" password = greatPassw0rd\n\n");
printf(" [ScorchedEarth]\n");
printf(" host = some.greathost.com\n");
printf(" port = 32331\n");
printf(" password = \"greatPassw0rdT00\"\n\n");
printf("Notes:\n");
printf(" - Server titles must not contain spaces or special characters.\n");
printf(" - Server titles in the configuration file must be UNIQUE.\n");
return 0;
}
//***************************************************************************
// main
//***************************************************************************
void doCopyright()
{
printf("ClusterChat - Ark cross server chat application version %s\n", VERSION);
printf("Copyright (c) 2017 by s710\n");
printf("GitHub: https://github.com/patrickjane/arkclusterchat\n");
}
//***************************************************************************
// doCommandline
//***************************************************************************
int doCommandline(int argc, char* argv[], std::list<ServerConfig*>* configs)
{
int i= 0;
const char* configFile= 0;
while (++i < argc && argv[i])
{
if (!strcmp(argv[i], "-c") && argv[i+1])
{
configFile= argv[i+1];
i++;
}
if (!strcmp(argv[i], "-s") && argv[i+1])
{
ServerConfig* cfg= new ServerConfig();
if (parseServer(argv[i+1], cfg) != success)
{
fprintf(stderr, "Error: Failed to parse malformed server '%s'\n", argv[i+1]);
delete cfg;
return fail;
}
configs->push_back(cfg);
i++;
}
if (!strcmp(argv[i], "--verbose"))
{
Globals::cfgVerbose= 1;
continue;
}
if (!strcmp(argv[i], "--debug"))
{
Globals::cfgDebug= 1;
continue;
}
if (!strcmp(argv[i], "--show-admin-cmd"))
{
Globals::cfgShowAdmin= 1;
continue;
}
}
if (configFile)
{
for (std::list<ServerConfig*>::iterator it= configs->begin(); it != configs->end(); ++it)
delete *it;
configs->clear();
printf("Main: Using configuration file '%s'\n", configFile);
if (ini_parse(configFile, iniHandler, configs) < 0)
{
fprintf(stderr, "Error: Failed to parse file '%s'\n", configFile);
return fail;
}
}
return done;
}
//***************************************************************************
// parseServer
//***************************************************************************
int parseServer(char* string, ServerConfig* cfg)
{
// expect: title:password@host:port
// parse from both sides, to allow passwords with special characters.
// title is expected to have NO spaces and NO special characters.
if (!string || !strlen(string) || !cfg)
return fail;
char* p= 0;
char* passwordStart= 0;
char* end= 0;
p= passwordStart= strchr(string, ':');
passwordStart++;
if (!p)
return fail;
cfg->title.assign(string, p-string);
p= string+strlen(string)-1;
while (*p && *p != ':' && p > string)
p--;
if (*p != ':')
return fail;
cfg->port= atoi(p+1);
end= p-1;
while (*p && *p != '@' && p > string)
p--;
if (*p != '@')
return fail;
cfg->host.assign(p+1, end-p);
cfg->password.assign(passwordStart, p-passwordStart);
return success;
}
//***************************************************************************
// iniHandler
//***************************************************************************
int iniHandler(void* user, const char* section, const char* name, const char* value)
{
std::list<ServerConfig*>* configs= (std::list<ServerConfig*>*)user;
if (!lastSection)
{
// first section
lastSection= strdup(section);
lastConfig= new ServerConfig();
lastConfig->title.assign(section);
configs->push_back(lastConfig);
}
else if (strcmp(lastSection, section))
{
// next section, append last section/server
::free((void*)lastSection);
lastSection= strdup(section);
lastConfig= new ServerConfig();
lastConfig->title.assign(section);
configs->push_back(lastConfig);
}
if (!strcmp(name, "host")) lastConfig->host.assign(value);
else if (!strcmp(name, "password")) lastConfig->password.assign(value);
else if (!strcmp(name, "port")) lastConfig->port= atoi(value);
return 1;
}