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