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