]> jfr.im git - irc/gameservirc.git/blob - gameserv/config.cpp
added new config file directive scriptdir and changed all calls of strcpy to strncpy...
[irc/gameservirc.git] / gameserv / config.cpp
1 #include <fstream>
2 #include <iostream>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include "extern.h"
7 #include "flags.h"
8
9 using namespace std;
10
11 int load_config_file(char *config);
12 void unload_config_file();
13
14 /* Random Configuration Stuff Goes Here until I code it to load from a .conf file :)*/
15
16 char *s_GameServ; // GameServ's nickname
17 char *gshost; // GameServ's Hostname
18 char *gsident; // GameServ's ident/username
19 char *servername; // GameServ's Server
20 char *c_Forest; // Forest channel
21 char *c_ForestTopic; // Forest Channel Topic
22 char *adminpass; // Administrator password
23 char *welcomemsg; // Welcome Message
24 char *ignoreserverslist; // Servernames to ignore
25 char *nsname; // NickServ's name
26 char *nspass; // GameServ's NickServ Password
27
28 int welcomedelay; // Welcome Message Delay
29 int updateperiod; // Seconds until another player database update
30 int forestfights; // Forest fights per day
31 int maxafightdistance; // Max levels above a player they can fight player->player
32 int maxbfightdistance; // Max levels below a player they can fight player->player
33 int maxidletime; // Max time (in seconds) a player can be idle for
34 int idlecheckperiod; // Period for checking every player's idle time
35 int level1expire; // Days for level 1 players to expire
36 int defaultexpire; // Days for other levels to expire
37 int maxitems; // Maximum amount of items a player can carry
38 unsigned int maxnicklen; // Maximum length a nickname can be
39 long refreshperiod; // Period for refreshing players
40 long rolloverperiod; // Period for rolling over forest fights
41 long configflags; // Holds the binary representation of some boolean values
42 long maxforestfights; // Maximum amount of forest fights to roll over
43 long numrolloverfights; // Number of forest fights to roll over
44
45
46 // Remote server stuff. This is used for the outgoing connection gameserv needs to make
47 // to a real ircd.
48 char *remoteserver; // Server to connect to
49 char *localhost; // Hostname of the local address to bind to
50 int remoteport; // Port to connect to on remoteserver
51 char *remotepass; // Password for the server link
52
53 char *storeitemdata; // File to store the items available in the store
54 char *tavernitemdata; // File to store the items available in the tavern
55 char *itemdata; // File to store the items in
56 char *playerdata; // File to store player data in
57 char *dragondata; // File to store current dragon data in
58 char *masterdata; // File to store the master data in
59 char *newsdata; // File to store news data in
60 char *pidfile; // Process ID file
61 char *scriptdir; // Directory to store scripts in
62
63 #if defined(P10)
64 char *gsnum = "[]AAA"; // GameServ Numeric
65 #endif
66
67 void unload_config_file()
68 {
69 if (s_GameServ)
70 delete [] s_GameServ;
71 if (gshost)
72 delete [] gshost;
73 if (gsident)
74 delete [] gsident;
75 if (servername)
76 delete [] servername;
77 if (c_Forest)
78 delete [] c_Forest;
79 if (c_ForestTopic)
80 delete [] c_ForestTopic;
81 if (remoteserver)
82 delete [] remoteserver;
83 if (localhost)
84 delete [] localhost;
85 if (remotepass)
86 delete [] remotepass;
87 if (playerdata)
88 delete [] playerdata;
89 if (storeitemdata)
90 delete [] storeitemdata;
91 if (tavernitemdata)
92 delete [] tavernitemdata;
93 if (itemdata)
94 delete [] itemdata;
95 if (dragondata)
96 delete [] dragondata;
97 if (masterdata)
98 delete [] masterdata;
99 if (adminpass)
100 delete [] adminpass;
101 if (welcomemsg)
102 delete [] welcomemsg;
103 if (pidfile)
104 delete [] pidfile;
105 if (ignoreserverslist)
106 delete [] ignoreserverslist;
107 if (newsdata)
108 delete [] newsdata;
109 if (nsname)
110 delete [] nsname;
111 if (nspass)
112 delete [] nspass;
113 if (scriptdir)
114 delete [] scriptdir;
115
116 configflags = 0;
117 }
118
119 int load_config_file(char *config)
120 {
121 char *buf, *directive, *value;
122
123 #define numdirectives 42
124
125 unload_config_file();
126
127 struct DIRECTIVE {
128 bool done;
129 char *desc;
130 };
131
132 DIRECTIVE directives[numdirectives];
133
134 directives[0].desc = "s_GameServ - GameServ Nickname";
135 directives[1].desc = "GSHOST - GameServ Hostname";
136 directives[2].desc = "GSIDENT - GameServ Ident";
137 directives[3].desc = "SERVERNAME - Pseudo Server's Name";
138 directives[4].desc = "C_FOREST - Forest Channel";
139 directives[5].desc = "C_FORESTTOPIC - Topic for the Forest Channel";
140 directives[6].desc = "REMOTESERVER - Server for gameserv to connect to (ip or hostname)";
141 directives[7].desc = "REMOTEPORT - Port on the remote server to connect to";
142 directives[8].desc = "REMOTEPASS - Password on the remote server";
143 directives[9].desc = "PLAYERDATA - File to store the player saves in";
144 directives[10].desc = "SAVEDNOTICE - True/False as to wether or not to tell the forest "\
145 "channel that the player database has been saved";
146 directives[11].desc = "ADMINPASS - Password to identify as an admin with";
147 directives[12].desc = "WELCOMEDELAY - Delay (in seconds) to wait before welcoming new users to the network";
148 directives[13].desc = "FORESTFIGHTS - Number of forest fights players get every day";
149 directives[14].desc = "UPDATEPERIOD - Number of seconds between every player data save";
150 directives[15].desc = "PIDFILE - Filename to store the gameserv process ID in";
151 directives[16].desc = "MAXAFIGHTDISTANCE - The maximum number of levels above you "\
152 "that you can fight player->player";
153 directives[17].desc = "MAXBFIGHTDISTANCE - The maximum number of levels below you "\
154 "that you can fight player->player";
155 directives[18].desc = "MAXIDLETIME - The maximum amount of time (in seconds) "\
156 "that a player can be idle before something happens";
157 directives[19].desc = "IDLECHECKPERIOD - The period (in seconds) in which the entire "\
158 "players list will be checked for idlers. See also: "\
159 "MAXIDLETIME";
160 directives[20].desc = "LISTENONC_FOREST - True/False as to "\
161 "whether or not to listen for forest "\
162 "commands on the forest channel";
163 directives[21].desc = "NEWSDATA - File to store daily news in";
164 directives[22].desc = "REFRESHPERIOD - Period (in seconds) to "\
165 "refresh players";
166 directives[23].desc = "LEVEL1EXPIRE - Amount of days until a "\
167 "level 1 player's account is deleted due to "\
168 "inactivity.";
169 directives[24].desc = "DEFAULTEXPIRE - Amount of days until a "\
170 "level 2 and above player's account is "\
171 "deleted due to inactivity";
172 directives[25].desc = "USENICKSERV - True/False as to wether or not "\
173 "GameServ should identify with NickServ";
174 directives[26].desc = "NSNAME - Your network's NickServ nickname";
175 directives[27].desc = "NSPASS - GameServ's NickServ Password";
176 directives[28].desc = "DRAGONDATA - File to store the current "\
177 "dragon's stats in";
178 directives[29].desc = "MASTERDATA - File to store the level master stats in";
179 directives[30].desc = "ITEMDATA - File to store the items in";
180 directives[31].desc = "TAVERNITEMDATA - File in which to store the items that are available in the tavern";
181 directives[32].desc = "MAXITEMS - Maximum number of items a player can carry";
182 directives[33].desc = "STOREITEMDATA - File in which to store the items that are available in the store";
183 directives[34].desc = "LOCALHOST - Local hostname or IP to bind to when connecting to the remote server";
184 directives[35].desc = "ROLLOVERFORESTFIGHTS - True/False as to wether or not to roll over forest fights "\
185 "into the next day";
186 directives[36].desc = "MAXFORESTFIGHTS - Maximum amount of forest fights to roll over";
187 directives[37].desc = "ROLLOVERPERIOD - Period (in seconds) to rollover forest fights";
188 directives[38].desc = "NUMROLLOVERFIGHTS - Number of forest fights to roll over";
189 directives[39].desc = "FAIRFIGHTS - True/False as to whether or not to check for fair fights";
190 directives[40].desc = "MAXNICKLEN - Maximum length a nickname can be";
191 directives[41].desc = "SCRIPTDIR - Directory to store scripts in";
192
193 configflags = 0;
194
195 for (int count = 0; count < numdirectives; count++)
196 {
197 directives[count].done = false;
198 }
199
200 ifstream infile;
201 infile.open(config);
202 if (infile.fail())
203 {
204 log("Error opening %s", config);
205 cerr << "Error opening " << config << endl;
206 return 0;
207 }
208
209 buf = new char[1024];
210
211 while (infile.getline(buf, 1024, '\n'))
212 {
213 #ifdef DEBUGMODE
214 log("Config file entry buf: %s", buf);
215 #endif
216
217 if (buf[0] == '#' || buf[0] == ' ' || buf[0] == '\0' || buf[0] == '\n' || buf[0] == '\r')
218 continue;
219
220 directive = strtok(buf, " ");
221
222 if (stricmp(directive, "DIE") == 0)
223 {
224 value = strtok(NULL, "");
225 log("You should read the entire %s file!", config);
226 cerr << "You should read the entire " << config << " file!"
227 << endl;
228 delete []buf;
229 exit(0);
230 }
231 if (stricmp(directive, "S_GAMESERV") == 0)
232 {
233 value = strtok(NULL, " ");
234 s_GameServ = new char[strlen(value) + 1];
235 strcpy(s_GameServ, value);
236 directives[0].done = true;
237 }
238 else if (stricmp(directive, "GSHOST") == 0)
239 {
240 value = strtok(NULL, " ");
241 gshost = new char[strlen(value) + 1];
242 strcpy(gshost, value);
243 directives[1].done = true;
244 }
245 else if (stricmp(directive, "GSIDENT") == 0)
246 {
247 value = strtok(NULL, " ");
248 gsident = new char[strlen(value) + 1];
249 strcpy(gsident, value);
250 directives[2].done = true;
251 }
252 else if (stricmp(directive, "SERVERNAME") == 0)
253 {
254 value = strtok(NULL, " ");
255 servername = new char[strlen(value) + 1];
256 strcpy(servername, value);
257 directives[3].done = true;
258 }
259 else if (stricmp(directive, "C_FOREST") == 0)
260 {
261 value = strtok(NULL, " ");
262 c_Forest = new char[strlen(value) + 1];
263 strcpy(c_Forest, value);
264 directives[4].done = true;
265 }
266 else if (stricmp(directive, "C_FORESTTOPIC") == 0)
267 {
268 value = strtok(NULL, "");
269 c_ForestTopic = new char[strlen(value) + 1];
270 strcpy(c_ForestTopic, value);
271 directives[5].done = true;
272 }
273 else if (stricmp(directive, "REMOTESERVER") == 0)
274 {
275 value = strtok(NULL, " ");
276 remoteserver = new char[strlen(value) + 1];
277 strcpy(remoteserver, value);
278 directives[6].done = true;
279 }
280 else if (stricmp(directive, "REMOTEPORT") == 0)
281 {
282 value = strtok(NULL, " ");
283 remoteport = stringtoint(value);
284 directives[7].done = true;
285 }
286 else if (stricmp(directive, "REMOTEPASS") == 0)
287 {
288 value = strtok(NULL, " ");
289 remotepass = new char[strlen(value) + 1];
290 strcpy(remotepass, value);
291 directives[8].done = true;
292 }
293 else if (stricmp(directive, "PLAYERDATA") == 0)
294 {
295 value = strtok(NULL, "");
296 playerdata = new char[strlen(value) + 1];
297 strcpy(playerdata, value);
298 directives[9].done = true;
299 }
300 else if (stricmp(directive, "SAVEDNOTICE") == 0)
301 {
302 value = strtok(NULL, " ");
303 if (stricmp(value, "TRUE") == 0)
304 setSavedNotice();
305 directives[10].done = true;
306 }
307 else if (stricmp(directive, "ADMINPASS") == 0)
308 {
309 value = strtok(NULL, "");
310 adminpass = new char[strlen(value) + 1];
311 strcpy(adminpass, value);
312 directives[11].done = true;
313 }
314 else if (stricmp(directive, "WELCOMEDELAY") == 0)
315 {
316 value = strtok(NULL, " ");
317 welcomedelay = stringtoint(value);
318 directives[12].done = true;
319 }
320 else if (stricmp(directive, "FORESTFIGHTS") == 0)
321 {
322 value = strtok(NULL, " ");
323 forestfights = stringtoint(value);
324 directives[13].done = true;
325 }
326 else if (stricmp(directive, "UPDATEPERIOD") == 0)
327 {
328 value = strtok(NULL, " ");
329 updateperiod = stringtoint(value);
330 directives[14].done = true;
331 }
332 else if (stricmp(directive, "PIDFILE") == 0)
333 {
334 value = strtok(NULL, " ");
335 pidfile = new char[strlen(value) + 1];
336 strcpy(pidfile, value);
337 directives[15].done = true;
338 }
339 else if (stricmp(directive, "MAXAFIGHTDISTANCE") == 0)
340 {
341 value = strtok(NULL, " ");
342 maxafightdistance = stringtoint(value);
343 directives[16].done = true;;
344 }
345 else if (stricmp(directive, "MAXBFIGHTDISTANCE") == 0)
346 {
347 value = strtok(NULL, " ");
348 maxbfightdistance = stringtoint(value);
349 directives[17].done = true;
350 }
351 else if (stricmp(directive, "MAXIDLETIME") == 0)
352 {
353 value = strtok(NULL, " ");
354 maxidletime = stringtoint(value);
355 directives[18].done = true;
356 }
357 else if (stricmp(directive, "IDLECHECKPERIOD") == 0)
358 {
359 value = strtok(NULL, " ");
360 idlecheckperiod = stringtoint(value);
361 directives[19].done = true;
362 }
363 else if (stricmp(directive, "LISTENONC_FOREST") == 0)
364 {
365 value = strtok(NULL, " ");
366 if(stricmp(value, "TRUE") == 0)
367 {
368 setListenOnCF();
369 }
370
371 directives[20].done = true;
372 }
373 else if (stricmp(directive, "NEWSDATA") == 0)
374 {
375 value = strtok(NULL, " ");
376 newsdata = new char [strlen(value) + 1];
377 strncpy(newsdata, value, sizeof(newsdata));
378 directives[21].done = true;
379 }
380 else if (stricmp(directive, "REFRESHPERIOD") == 0)
381 {
382 value = strtok(NULL, " ");
383 refreshperiod = stringtoint(value);
384 directives[22].done = true;
385 }
386 else if (stricmp(directive, "LEVEL1EXPIRE") == 0)
387 {
388 value = strtok(NULL, " ");
389 level1expire = stringtoint(value);
390 directives[23].done = true;
391 }
392 else if (stricmp(directive, "DEFAULTEXPIRE") == 0)
393 {
394 value = strtok(NULL, " ");
395 defaultexpire = stringtoint(value);
396 directives[24].done = true;
397 }
398 else if (stricmp(directive, "USENICKSERV") == 0)
399 {
400 value = strtok(NULL, " ");
401 if (stricmp(value, "TRUE") == 0)
402 {
403 setUseNickServ();
404 }
405
406 directives[25].done = true;
407 }
408 else if (stricmp(directive, "NSNAME") == 0)
409 {
410 value = strtok(NULL, " ");
411 nsname = new char[strlen(value) + 1];
412 strncpy(nsname, value, sizeof(nsname));
413 directives[26].done = true;
414 }
415 else if (stricmp(directive, "NSPASS") == 0)
416 {
417 value = strtok(NULL, " ");
418 nspass = new char[strlen(value) + 1];
419 strncpy(nspass, value, sizeof(nspass));
420 directives[27].done = true;
421 }
422 else if (stricmp(directive, "DRAGONDATA") == 0)
423 {
424 value = strtok(NULL, "");
425 dragondata = new char[strlen(value) + 1];
426 strncpy(dragondata, value, sizeof(dragondata));
427 directives[28].done = true;
428 }
429 else if (stricmp(directive, "MASTERDATA") == 0)
430 {
431 value = strtok(NULL, "");
432 masterdata = new char[strlen(value) + 1];
433 strncpy(masterdata, value, sizeof(masterdata));
434 directives[29].done = true;
435 }
436 else if (stricmp(directive, "ITEMDATA") == 0)
437 {
438 value = strtok(NULL, "");
439 itemdata = new char[strlen(value) + 1];
440 strncpy(itemdata, value, sizeof(itemdata));
441 directives[30].done = true;
442 }
443 else if (stricmp(directive, "TAVERNITEMDATA") == 0)
444 {
445 value = strtok(NULL, "");
446 tavernitemdata = new char[strlen(value) + 1];
447 strncpy(tavernitemdata, value, sizeof(tavernitemdata));
448 directives[31].done = true;
449 }
450 else if (stricmp(directive, "MAXITEMS") == 0)
451 {
452 value = strtok(NULL, " ");
453 maxitems = stringtoint(value);
454 directives[32].done = true;
455 }
456 else if (stricmp(directive, "STOREITEMDATA") == 0)
457 {
458 value = strtok(NULL, "");
459 storeitemdata = new char[strlen(value) + 1];
460 strncpy(storeitemdata, value, sizeof(storeitemdata));
461 directives[33].done = true;
462 }
463 else if (stricmp(directive, "LOCALHOST") == 0)
464 {
465 value = strtok(NULL, " ");
466 localhost = new char[strlen(value) + 1];
467 strncpy(localhost, value, sizeof(localhost));
468 directives[34].done = true;
469 }
470 else if (stricmp(directive, "ROLLOVERFORESTFIGHTS") == 0)
471 {
472 value = strtok(NULL, " ");
473 if (stricmp(value, "TRUE") == 0)
474 {
475 setRolloverForestFights();
476 }
477 else
478 {
479 directives[36].done = true;
480 directives[37].done = true;
481 directives[38].done = true;
482 }
483 directives[35].done = true;
484 }
485 else if (stricmp(directive, "MAXFORESTFIGHTS") == 0)
486 {
487 value = strtok(NULL, " ");
488 maxforestfights = stringtoint(value);
489 directives[36].done = true;
490 }
491 else if (stricmp(directive, "ROLLOVERPERIOD") == 0)
492 {
493 value = strtok(NULL, " ");
494 rolloverperiod = stringtoint(value);
495 directives[37].done = true;
496 }
497 else if (stricmp(directive, "NUMROLLOVERFIGHTS") == 0)
498 {
499 value = strtok(NULL, "");
500 numrolloverfights = stringtoint(value);
501 directives[38].done = true;
502 }
503 else if (stricmp(directive, "FAIRFIGHTS") == 0)
504 {
505 value = strtok(NULL, " ");
506 if (stricmp(value, "TRUE") == 0)
507 setFairFights();
508 directives[39].done = true;
509 }
510 else if (stricmp(directive, "MAXNICKLEN") == 0)
511 {
512 value = strtok(NULL, " ");
513 maxnicklen = stringtoint(value);
514 directives[40].done = true;
515 }
516 else if (stricmp(directive, "SCRIPTDIR") == 0)
517 {
518 value = strtok(NULL, "");
519 scriptdir = new char [strlen(value)];
520 strncpy(scriptdir, value, sizeof(scriptdir));
521 directives[41].done = true;
522 }
523 else if (stricmp(directive, "WELCOMEMSG") == 0)
524 {
525 // This directive is optional
526 value = strtok(NULL, "");
527 welcomemsg = new char[strlen(value) + 1];
528 strncpy(welcomemsg, value, sizeof(welcomemsg));
529 setWelcome();
530 }
531 else if (stricmp(directive, "USEPRIVMSG") == 0)
532 {
533 // This directive is optional
534 setUsePrivmsg();
535 }
536 else if (stricmp(directive, "BOPER") == 0)
537 {
538 // This directive is optional
539 setBOper();
540 }
541 else if (stricmp(directive, "IGNORESERVERS") == 0)
542 {
543 // This directive is optional
544 value = strtok(NULL, "");
545 ignoreserverslist = new char[strlen(value) + 1];
546 strncpy(ignoreserverslist, value, sizeof(ignoreserverslist));
547 }
548 else
549 {
550 #ifdef DEBUGMODE
551 log("Unknown Directive. Buffer: %s", buf);
552 cerr << "Unknown " << config << " directive. Buffer: "
553 << buf << endl;
554 #endif
555 continue;
556 }
557 }
558 delete [] buf;
559 infile.close();
560
561 int nonemissing = 1;
562 for (int count2 = 0; count2 < numdirectives; count2++)
563 {
564 if (!directives[count2].done)
565 {
566 cerr << "Missing config directive: " << directives[count2].desc << endl;
567 nonemissing = 0;
568 }
569 }
570
571 return nonemissing;
572 }