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