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