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