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