-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSGSXLConfig.cpp
391 lines (350 loc) · 11.2 KB
/
SGSXLConfig.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
* Copyright (C) 2010,2011,2012 by Jonathan Naylor G4KLX
* Copyright (c) 2017 by Thomas A. Early N7TAE
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <string>
#include <sstream>
#include <iostream>
#include <filesystem>
#include "Utils.h"
#include "SGSXLConfig.h"
CSGSXLConfig::CSGSXLConfig(const std::string &pathname)
{
m_hasErrors = false;
if (pathname.size() < 1) {
printf("Configuration filename too short!\n");
m_hasErrors = true;
return;
}
Config cfg;
try {
cfg.readFile(pathname.c_str());
}
catch(const FileIOException &fioex) {
printf("Can't read %s\n", pathname.c_str());
m_hasErrors = true;
return;
}
catch(const ParseException &pex) {
printf("Parse error at %s:%d - %s\n", pex.getFile(), pex.getLine(), pex.getError());
m_hasErrors = true;
return;
}
if (! get_value(cfg, "gateway.callsign", m_callsign, 3, 8, "")
|| 0 == m_callsign.size()) {
m_hasErrors = true;
printf("No Gateway callsign specified");
return;
}
CUtils::ToUpper(m_callsign);
get_value(cfg, "gateway.address", m_address, 0, 20, "");
printf("GATEWAY: callsign='%s' address='%s'\n", m_callsign.c_str(), m_address.c_str());
//ircDDB Networks
for(int i = 0; i < cfg.lookup("ircddb").getLength(); i++) {
SircDDB * ircddb = new SircDDB();
std::stringstream key;
key << "ircddb.[" << i << "].hostname";
if(! get_value(cfg, key.str(), ircddb->hostname, 5, 30, "") || ircddb->hostname == "") {//do not allow hostname to be empty
delete ircddb;
continue;
}
key.str("");key.clear();
key << "ircddb.[" << i << "].username";
if (! get_value(cfg, key.str(), ircddb->username, 3, 8, m_callsign)) {//default user name to callsign
delete ircddb;
continue;
}
CUtils::ToUpper(ircddb->username);
key.str("");key.clear();
key << "ircddb.[" << i << "].password";
if(!get_value(cfg, key.str(), ircddb->password, 0, 30, "")) {
delete ircddb;
continue;
}
ircddb->isQuadNet = ircddb->hostname.find("openquad.net") != std::string::npos;
this->m_ircDDB.push_back(ircddb);
std::cout << "IRCDDB: host=" << ircddb->hostname << " user=" << ircddb->username << " password=" << ircddb->password << "\n";
}
if(this->m_ircDDB.size() == 0) {//no ircddb network specified? Default to openquad!
SircDDB * ircddb = new SircDDB();
ircddb->hostname = "rr.openquad.net";
ircddb->password = "";
ircddb->username = m_callsign;
ircddb->isQuadNet = true;
this->m_ircDDB.push_back(ircddb);
std::cout << "No ircDDB networks configure'd, defaulting to IRCDDB: host=" << ircddb->hostname << " user=" << ircddb->username << " password=" << ircddb->password << "\n";
}
// module parameters
for (int i=0; i<cfg.lookup("module").getLength(); i++) {
std::stringstream key;
std::string basename, subscribe, unsubscribe, band;
key << "module.[" << i << "].basename";
if (get_value(cfg, key.str(), basename, 1, 7, "")) {
bool isokay = true;
for (std::string::iterator it=basename.begin(); it!=basename.end(); it++) {
if (! isalnum(*it)) {
isokay = false;
break;
}
}
if (isokay)
CUtils::ToUpper(basename);
else {
printf("Malformed basename for module %d: '%s'\n", i, basename.c_str());
basename.clear();
}
}
key.str("");key.clear();
key << "module.[" << i << "].band";
get_value(cfg, key.str(), band, 1, 1, "A");
CUtils::ToUpper(band);
if (! isalpha(band[0])) {
printf("Module %d band is not a letter\n", i);
basename.clear();
}
key.str("");key.clear();
key << "module.[" << i << "].subscribe";
get_value(cfg, key.str(), subscribe, 1, 1, "A");
CUtils::ToUpper(subscribe);
if (subscribe[0] != ' ' && ('A' > subscribe[0] || subscribe[0] > 'Z')) {
printf("subscribe suffix not space or letter\n");
basename.clear();
}
key.str("");key.clear();
key << "module.[" << i << "].unsubscribe";
get_value(cfg, key.str(), unsubscribe, 1, 1, "T");
CUtils::ToUpper(unsubscribe);
if ('A' > unsubscribe[0] || unsubscribe[0] > 'Z') {
printf("unsubscribe suffix not a letter\n");
basename.clear();
}
if (! subscribe.compare(unsubscribe)) {
// subscribe and unsubscribe suffix needs to be different
printf("subscribe and unsubscribe for %s are identical\n", basename.c_str());
basename.clear();
}
// skip to the next module definition
if (0 == basename.size())
continue;
struct Smodule *pmod = new struct Smodule;
// pad basename with spaces
basename.resize(7, ' ');
pmod->callsign = basename + subscribe;
pmod->logoff = basename + unsubscribe;
pmod->band = band;
key.str("");key.clear();
key << "module.[" << i << "].info";
get_value(cfg, key.str(), pmod->info, 0, 20, "Smart Group Server XL");
if (pmod->info.size())
pmod->info.resize(20, ' ');
key.str("");key.clear();
key << "module.[" << i << "].permanent";
get_value(cfg, key.str(), pmod->permanent, 0, 120, "");
CUtils::ToUpper(pmod->permanent);
int ivalue;
key.str("");key.clear();
key << "module.[" << i << "].usertimeout";
get_value(cfg, key.str(), ivalue, 0, 300, 300);
pmod->usertimeout = (unsigned int)ivalue;
bool bvalue;
key.str("");key.clear();
key << "module.[" << i << "].callsignswitch";
get_value(cfg, key.str(), bvalue, false);
pmod->callsignswitch = bvalue ? SCS_GROUP_CALLSIGN : SCS_USER_CALLSIGN;
key.str("");key.clear();
key << "module.[" << i << "].txmsgswitch";
get_value(cfg, key.str(), pmod->txmsgswitch, true);
key.str("");key.clear();
key << "module.[" << i << "].reflector";
if (! get_value(cfg, key.str(), basename, 8, 8, "")) {
printf("reflector %d must be undefined or exactly 8 chars!\n", i);
basename.clear();
}
pmod->reflector.clear();
if (basename.size()) {
CUtils::ToUpper(basename);
if ( (0==basename.compare(0,3,"XRF") || 0==basename.compare(0,3,"DCS")) && isdigit(basename[3]) && isdigit(basename[4]) && isdigit(basename[5]) && ' '==basename[6] && isalpha(basename[7]) )
pmod->reflector = basename;
}
printf("Module %d: callsign='%s' unsubscribe='%s' info='%s' permanent='%s' usertimeout=%d callsignswitch=%s, txmsgswitch=%s reflector='%s'\n",
i, pmod->callsign.c_str(), pmod->logoff.c_str(), pmod->info.c_str(), pmod->permanent.c_str(), pmod->usertimeout,
SCS_GROUP_CALLSIGN==pmod->callsignswitch ? "Group" : "User", pmod->txmsgswitch ? "true" : "false", pmod->reflector.c_str());
m_module.push_back(pmod);
}
// remote control
get_value(cfg, "remote.enabled", m_remoteEnabled, false);
if (m_remoteEnabled) {
get_value(cfg, "remote.password", m_remotePassword, 6, 30, "");
int ivalue;
get_value(cfg, "remote.port", ivalue, 1000, 65000, 39999);
m_remotePort = (unsigned int)ivalue;
printf("Remote enabled: password='%s', port=%d\n", m_remotePassword.c_str(), m_remotePort);
} else {
m_remotePort = 0U;
m_remotePassword.clear();
printf("Remote disabled\n");
}
//audio
get_value(cfg, "audio.enabled", m_audioEnabled, true);
get_value(cfg, "audio.language", m_audioLanguage, TL_ENGLISH_UK);
if(m_audioEnabled) {
printf("Audio enabled\n");
} else {
printf("Audio disabled\n");
}
}
CSGSXLConfig::~CSGSXLConfig()
{
while (m_module.size()) {
delete m_module.back();
m_module.pop_back();
}
while(m_ircDDB.size()) {
delete m_ircDDB.back();
m_ircDDB.pop_back();
}
}
unsigned int CSGSXLConfig::getModCount()
{
return m_module.size();
}
unsigned int CSGSXLConfig::getIrcDDBCount()
{
return m_ircDDB.size();
}
unsigned int CSGSXLConfig::getLinkCount(const char *type)
{
unsigned int count = 0;
for (unsigned int i=0; i<getModCount(); i++)
if (0 == m_module[i]->reflector.compare(0, 3, type))
count++;
return count;
}
void CSGSXLConfig::getAudio(bool & enabled, TEXT_LANG & lang) const
{
enabled = m_audioEnabled;
lang = m_audioLanguage;
}
bool CSGSXLConfig::hasErrors()
{
return m_hasErrors;
}
bool CSGSXLConfig::get_value(const Config &cfg, const std::string &path, int &value, int min, int max, int default_value)
{
if (cfg.lookupValue(path, value)) {
if (value < min || value > max)
value = default_value;
} else
value = default_value;
return true;
}
bool CSGSXLConfig::get_value(const Config &cfg, const std::string &path, TEXT_LANG& lang, TEXT_LANG defaultValue)
{
lang = defaultValue;
std::string value;
if(cfg.lookupValue(path, value)) {
if(value == "de_DE")
{
lang = TL_DEUTSCH;
}
else if(value == "dk_DK")
{
lang = TL_DANSK;
}
else if(value == "it_IT")
{
lang = TL_ITALIANO;
}
else if(value == "fr_FR")
{
lang = TL_FRANCAIS;
}
else if(value == "es_ES")
{
lang = TL_ESPANOL;
}
else if(value == "se_SE")
{
lang = TL_SVENSKA;
}
else if(value == "pl_PL")
{
lang = TL_POLSKI;
}
else if(value == "en_US")
{
lang = TL_ENGLISH_US;
}
else if(value == "en_GB")
{
lang = TL_ENGLISH_UK;
}
else if(value == "no_NO")
{
lang = TL_NORSK;
}
}
return true;
}
bool CSGSXLConfig::get_value(const Config &cfg, const std::string &path, bool &value, bool default_value)
{
if (! cfg.lookupValue(path, value))
value = default_value;
return true;
}
bool CSGSXLConfig::get_value(const Config &cfg, const std::string &path, std::string &value, int min, int max, const std::string &default_value)
{
if (cfg.lookupValue(path, value)) {
int l = value.length();
if (l<min || l>max) {
std::cout << path << "=" << value << " has an inalid length, must be between " << min << " and " << max << " actual " << l << "\n";
return false;
}
} else
value = default_value;
return true;
}
void CSGSXLConfig::getGateway(std::string& callsign, std::string& address) const
{
callsign = m_callsign;
address = m_address;
}
void CSGSXLConfig::getIrcDDB(unsigned int ircddb, std::string& hostname, std::string& username, std::string& password, bool &isQuadNet) const
{
hostname = m_ircDDB[ircddb]->hostname;
username = m_ircDDB[ircddb]->username;
password = m_ircDDB[ircddb]->password;
isQuadNet = m_ircDDB[ircddb]->isQuadNet;
}
void CSGSXLConfig::getGroup(unsigned int mod, std::string& band, std::string& callsign, std::string& logoff, std::string& info, std::string& permanent, unsigned int& userTimeout, CALLSIGN_SWITCH& callsignSwitch, bool& txMsgSwitch, std::string& reflector) const
{
band = m_module[mod]->band;
callsign = m_module[mod]->callsign;
logoff = m_module[mod]->logoff;
info = m_module[mod]->info;
permanent = m_module[mod]->permanent;
userTimeout = m_module[mod]->usertimeout;
callsignSwitch = m_module[mod]->callsignswitch;
txMsgSwitch = m_module[mod]->txmsgswitch;
reflector = m_module[mod]->reflector;
}
void CSGSXLConfig::getRemote(bool& enabled, std::string& password, unsigned int& port) const
{
enabled = m_remoteEnabled;
password = m_remotePassword;
port = m_remotePort;
}