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