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