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