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