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