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