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