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