]>
Commit | Line | Data |
---|---|---|
85ce9d3e | 1 | /* |
2 | * This file is provided for use with the unix-socket-faq. It is public | |
3 | * domain, and may be copied freely. There is no copyright on it. The | |
4 | * original work was by Vic Metcalfe (vic@brutus.tlug.org), and any | |
5 | * modifications made to that work were made with the understanding that | |
6 | * the finished work would be in the public domain. | |
7 | * | |
8 | * If you have found a bug, please pass it on to me at the above address | |
9 | * acknowledging that there will be no copyright on your work. | |
10 | * | |
11 | * The most recent version of this file, and the unix-socket-faq can be | |
12 | * found at http://www.interlog.com/~vic/sock-faq/. | |
13 | */ | |
14 | ||
15 | #include "sockhelp.h" | |
c7340cbd | 16 | #include "options.h" |
85ce9d3e | 17 | #include "list.h" |
18 | #include "aClient.h" | |
19 | #include "extern.h" | |
448a1531 | 20 | #include "flags.h" |
85ce9d3e | 21 | #include <stdio.h> |
22 | #include <unistd.h> | |
23 | #include <string.h> | |
fb37ecc7 | 24 | #include <fstream> |
85ce9d3e | 25 | #include <stdlib.h> |
ce61cdfa | 26 | #include <fcntl.h> |
27 | #include <signal.h> | |
28 | //#include <sys/types.h> | |
29 | //#include <sys/wait.h> | |
30 | //#include <errno.h> | |
31 | ||
fb37ecc7 | 32 | using std::ofstream; |
33 | using std::ifstream; | |
ce61cdfa | 34 | using std::cerr; |
35 | using std::endl; | |
fb37ecc7 | 36 | |
91c0b563 | 37 | char *PACKAGE = "GameServ"; |
4f7dd884 | 38 | char *VERSION = "1.2.3 +devel"; |
173302fe | 39 | |
85ce9d3e | 40 | int sock; |
abbfcdb9 | 41 | int day; |
44ea29f7 | 42 | |
7996e5fd | 43 | List<aClient> clients[U_TABLE_SIZE]; |
85ce9d3e | 44 | |
abbfcdb9 | 45 | void save_day(); |
46 | void load_day(); | |
15838737 | 47 | void prettyIntro(); |
40251952 | 48 | void check_idles(); |
44ea29f7 | 49 | |
ce61cdfa | 50 | // Make this a daemon |
51 | int daemon(int nochdir, int noclose); | |
52 | ||
53 | // Close all file descriptors from >= fd | |
54 | void closeall(int fd); | |
55 | ||
624c0352 | 56 | int main(int argc, char *argv[]) |
85ce9d3e | 57 | { |
e3ede4a7 | 58 | char buffer[1024], buf[1024]; |
59 | memset(buffer, 0, 1024); | |
60 | memset(buf, 0, 1024); | |
61 | int connected; | |
62 | long lastidlecheck; | |
63 | char *cmd, *source = NULL, *conf; | |
64 | srand(time(NULL)); | |
65 | conf = new char[16]; | |
66 | strcpy(conf, "gameserv.conf"); | |
624c0352 | 67 | |
1579dfa2 | 68 | /* |
69 | * This needs to be fixed to work for any number of arguments in any | |
70 | * order | |
71 | * | |
72 | */ | |
624c0352 | 73 | if (argc > 1) |
74 | { | |
75 | if ( argc > 2 || stricmp(argv[1], "--help") == 0) | |
76 | { | |
77 | cout << "Usage: gameserv [options] [configfile]" << endl; | |
78 | cout << "Options:" << endl; | |
79 | cout << "--help Displays this help dialogue" << endl; | |
80 | return 1; | |
81 | } | |
e3ede4a7 | 82 | delete []conf; |
83 | conf = argv[1]; | |
624c0352 | 84 | } |
85 | ||
15838737 | 86 | prettyIntro(); |
624c0352 | 87 | |
88 | if (load_config_file(conf)) | |
89 | { | |
90 | cout << "Config file loaded ok...\n" | |
91 | << "Turning into a daemon" << endl; | |
92 | } | |
93 | else | |
94 | exit(2); | |
324ab87f | 95 | |
e3ede4a7 | 96 | if (argc <= 1) |
97 | delete []conf; | |
98 | ||
ce61cdfa | 99 | // Turn into a daemon |
5aa1d28d | 100 | /* |
ce61cdfa | 101 | if (daemon(1,0) < 0) |
102 | { | |
103 | perror("Could not turn into a daemon"); | |
448a1531 | 104 | exit(3); |
ce61cdfa | 105 | } |
5aa1d28d | 106 | */ |
05c527e6 | 107 | init_masters(); |
108 | load_gs_dbase(); | |
5aa1d28d | 109 | cout << "Loading news" << endl; |
110 | loadNews(newsdata, todaysnews); | |
05c527e6 | 111 | |
112 | if (load_monsters() == false) | |
113 | goto end; | |
114 | ||
448a1531 | 115 | shuttingdown = false; |
116 | ||
117 | char ignoreservers[32][256]; | |
118 | char *currentserver; | |
119 | currentserver = strtok(ignoreserverslist, " "); | |
120 | for (int server = 0; server < 32 && currentserver != NULL; server++) | |
121 | { | |
122 | strncpy(ignoreservers[server], currentserver, 255); | |
123 | log("Placing %s on the server ignore list", currentserver); | |
124 | currentserver = strtok(NULL, " "); | |
125 | } | |
05c527e6 | 126 | |
127 | strcpy(boss.name, "Red Dragon"); | |
128 | strcpy(boss.weapon, "Breath of Unholy Fire"); | |
68379f96 | 129 | boss.strength = 2500; |
05c527e6 | 130 | boss.gold = 2000000000; |
131 | boss.exp = 2000000000; | |
6d053d90 | 132 | boss.maxhp = 6667; |
133 | boss.hp = 6667; | |
05c527e6 | 134 | strcpy(boss.death, "You finally snuff out the deadly murderous "\ |
135 | "dragon's dark flames. You have freed the land of its terror "\ | |
136 | "filled reign from above!"); | |
137 | ||
448a1531 | 138 | |
05c527e6 | 139 | // This loop will retry the connection 3 times |
140 | for (int retry = 0; retry < 3 && !shuttingdown; retry++) | |
141 | { | |
0b6098d5 | 142 | connected = 1; |
143 | load_day(); | |
05c527e6 | 144 | |
448a1531 | 145 | |
0b6098d5 | 146 | long int loadtime = time(NULL); |
147 | long int currentTime; | |
148 | long int oldTime = loadtime; | |
40251952 | 149 | |
0b6098d5 | 150 | lastidlecheck = loadtime; |
05c527e6 | 151 | |
0b6098d5 | 152 | #ifdef DEBUGMODE |
153 | log("Setting primary Idle Check timestamp: %ld", lastidlecheck); | |
154 | #endif | |
155 | bool loaded = false; | |
156 | ||
157 | ignore_pipe(); | |
158 | sock = make_connection(remoteport, SOCK_STREAM, remoteserver); | |
159 | if (sock == -1) { | |
160 | fprintf(stderr,"make_connection failed.\n"); | |
85ce9d3e | 161 | unload_config_file(); |
162 | return -1; | |
163 | } | |
448a1531 | 164 | log("%S socket connected."); |
85ce9d3e | 165 | |
c7340cbd | 166 | #ifdef UNREAL |
85ce9d3e | 167 | raw("PROTOCTL NICKv2 VHP"); |
168 | raw("PASS :%s", remotepass); | |
c7340cbd | 169 | raw("SERVER %s 1 :%s", servername, servername); |
6f727d4c | 170 | raw("NICK %S 1 %d %S %s %s %d +w%s %s :%s v%s", time(NULL), gshost, |
171 | servername, time(NULL), (isBOper() ? "o" : ""), gshost, PACKAGE, VERSION); | |
85ce9d3e | 172 | raw(":%S JOIN %s", c_Forest); |
03a4bdbb | 173 | raw(":%S MODE %s +tn", c_Forest); |
c7340cbd | 174 | #elif defined(BAHAMUT) |
175 | raw("PASS %s :TS", remotepass); | |
176 | raw("SERVER %s 1 :%s", servername, servername); | |
6f727d4c | 177 | raw("NICK %S 1 %d +w%s %s %s %s 0 :GameServ", time(NULL), (isBOper() ? "o" : ""), |
178 | gsident, gshost, servername); | |
03a4bdbb | 179 | raw(":%s SJOIN %d %d %s +nt :@%S", servername, time(NULL), time(NULL), c_Forest); |
581ec09e | 180 | #elif defined(HYBRID) |
181 | raw("PASS %s :TS", remotepass); | |
182 | raw("SERVER %s 1 :%s", servername, servername); | |
6f727d4c | 183 | raw("NICK %S 1 %d +w%s %s %s %s :GameServ", time(NULL), (isBOper() ? "o" : ""), |
184 | gsident, gshost, servername); | |
448a1531 | 185 | raw(":%s SJOIN %ld %s +nt :@%S", servername, time(NULL), c_Forest); |
e1c41a84 | 186 | #elif defined(P10) |
187 | // Server numeric is: [] <-- must be unique | |
188 | raw("PASS :%s", remotepass); | |
189 | raw("SERVER %s 1 %d %d P10 []AAF :%s", servername, time(NULL), time(NULL), servername); | |
6f727d4c | 190 | raw("[] N %S 1 %d %s %s %s DAqAoB %s :%S", time(NULL), gsident, gshost, |
c8117c0f | 191 | (isBOper() ? "+o" : ""), gsnum); |
03a4bdbb | 192 | raw("[] B %s %d +tn %s:o", c_Forest, time(NULL) - 864000, gsnum); |
c7340cbd | 193 | #endif |
e1c41a84 | 194 | |
195 | #if defined(P10) | |
196 | raw("%s T %s :%s", gsnum, c_Forest, c_ForestTopic); | |
fc9d2643 | 197 | raw("[] EB"); // End burst |
581ec09e | 198 | #else |
199 | #ifndef HYBRID | |
85ce9d3e | 200 | raw(":%S MODE %s +o %S", c_Forest); |
581ec09e | 201 | #endif |
c7340cbd | 202 | raw(":%S TOPIC %s :%s", c_Forest, c_ForestTopic); |
e1c41a84 | 203 | #endif |
85ce9d3e | 204 | |
205 | sock_gets(sock,buffer,sizeof(buffer)-1); /* -1 added thanks to | |
206 | David Duchene <dave@ltd.com> for pointing out the possible | |
207 | buffer overflow resulting from the linefeed added below. */ | |
208 | ||
209 | ||
9f8c2acc | 210 | #ifdef DEBUGMODE |
211 | log("Server: %s",buffer); | |
212 | #endif | |
213 | ||
85ce9d3e | 214 | while (connected) { |
215 | if (sock_gets(sock,buffer,sizeof(buffer)) == -1) { | |
216 | connected = 0; | |
217 | } | |
218 | strcpy(buf, buffer); | |
219 | ||
e1c41a84 | 220 | #if !defined(P10) |
85ce9d3e | 221 | if (buffer[0] == ':') |
222 | { | |
223 | source = strtok(buf, " "); | |
224 | cmd = strtok(NULL, " "); | |
225 | } | |
226 | else | |
227 | cmd = strtok(buf, " "); | |
e1c41a84 | 228 | #else |
229 | source = strtok(buf, " "); | |
230 | cmd = strtok(NULL, " "); | |
231 | #endif | |
85ce9d3e | 232 | |
9f8c2acc | 233 | #ifdef DEBUGMODE |
234 | log("Server: %s", buffer); | |
235 | #endif | |
5963944b | 236 | |
bf2cabcd | 237 | // Wait N seconds then we're loaded. |
5963944b | 238 | if (!loaded) |
239 | { | |
922daad7 | 240 | if (time(NULL) >= welcomedelay + loadtime) |
05c527e6 | 241 | { |
5963944b | 242 | loaded = true; |
05c527e6 | 243 | retry = 0; // Start the reconnection cycle over |
244 | } | |
5963944b | 245 | } |
40251952 | 246 | else |
247 | { | |
248 | long TIME = time(NULL); | |
249 | if (TIME - lastidlecheck >= idlecheckperiod) | |
250 | { | |
251 | check_idles(); | |
252 | lastidlecheck = TIME; | |
253 | } | |
254 | } | |
5963944b | 255 | |
922daad7 | 256 | // Save the player data every updateperiod seconds |
257 | currentTime = time(NULL); | |
258 | if (currentTime - oldTime >= updateperiod) | |
259 | { | |
260 | oldTime = currentTime; | |
0b6098d5 | 261 | log("Saving to %s", playerdata); |
922daad7 | 262 | save_gs_dbase(); |
5aa1d28d | 263 | saveNews(newsdata, todaysnews); |
922daad7 | 264 | } |
265 | ||
e1c41a84 | 266 | |
267 | #if !defined(P10) | |
85ce9d3e | 268 | if (stricmp(cmd, "PING") == 0) { |
0a1518fa | 269 | char *timestamp; |
270 | timestamp = strtok(NULL, ""); | |
271 | raw("PONG %s", timestamp); | |
e1c41a84 | 272 | #else |
273 | if (stricmp(cmd, "G") == 0) { | |
274 | char *timestamp; | |
275 | timestamp = strtok(NULL, " "); | |
276 | raw("[] Z [] %s 0 %s", timestamp + 1, timestamp); | |
277 | #endif | |
fc9d2643 | 278 | #ifdef P10 |
279 | } else if (stricmp(cmd, "EB") == 0) { | |
280 | raw("[] EA"); | |
281 | #endif | |
0501fe18 | 282 | } else if (stricmp(cmd, "VERSION") == 0) { |
283 | char *server; | |
284 | server = strtok(NULL, " "); | |
285 | server++; | |
2e9db9c4 | 286 | raw(":%s 351 %s %s_%s. %s", servername, source+1, PACKAGE, VERSION, servername); |
ba2a880f | 287 | #if !defined(P10) |
85ce9d3e | 288 | } else if (strncmp(cmd, "NICK", 4) == 0) { |
289 | if (buffer[0] == ':') | |
290 | { | |
291 | aClient *tempPtr; | |
28f552b8 | 292 | if ((tempPtr = find((source + 1)))) |
85ce9d3e | 293 | { |
294 | char *nick; | |
59dc3990 | 295 | unsigned long oldhv, newhv; |
85ce9d3e | 296 | nick = strtok(NULL, " "); |
448a1531 | 297 | oldhv = iHASH((unsigned char *) tempPtr->getNick()); |
298 | newhv = iHASH((unsigned char *) nick); | |
85ce9d3e | 299 | tempPtr->setNick(nick); |
59dc3990 | 300 | clients[oldhv].remove(tempPtr); |
301 | clients[newhv].insertAtBack(tempPtr); | |
85ce9d3e | 302 | } |
303 | } | |
304 | else | |
305 | { | |
306 | char *nick; | |
ba2a880f | 307 | #else |
308 | } else if (stricmp(cmd, "N") == 0 && strlen(source) == 2) { | |
309 | { | |
ce61cdfa | 310 | char *nick, *realnick; |
311 | realnick = strtok(NULL, " "); | |
ba2a880f | 312 | |
ce61cdfa | 313 | for (int x = 0; x < 5; x++) |
ba2a880f | 314 | nick = strtok(NULL, " "); |
fc9d2643 | 315 | |
ba2a880f | 316 | if (nick[0] == '+') |
fc9d2643 | 317 | { |
318 | #ifdef DEBUGMODE | |
319 | log ("aClient has modes"); | |
320 | #endif | |
321 | ||
322 | // Searching for the +r mode (extra parameter) | |
323 | for (unsigned int count = 1; count < strlen(nick); count++) | |
324 | { | |
325 | if (nick[count] == 'r') | |
326 | { | |
327 | nick = strtok(NULL, " "); | |
328 | break; | |
329 | } | |
330 | } | |
331 | nick = strtok(NULL, " "); | |
332 | } | |
ba2a880f | 333 | #endif |
448a1531 | 334 | aClient *newuser, *temp; |
ba2a880f | 335 | |
85ce9d3e | 336 | nick = strtok(NULL, " "); |
ba2a880f | 337 | |
ce61cdfa | 338 | #ifdef P10 |
339 | newuser = new aClient(nick, realnick); | |
340 | #else | |
341 | newuser = new aClient(nick); | |
342 | #endif | |
343 | ||
344 | ||
5963944b | 345 | if (loaded) |
18b84d11 | 346 | |
347 | if (isWelcome()) | |
348 | { | |
349 | #ifdef P10 | |
350 | notice(s_GameServ, nick, welcomemsg, realnick); | |
351 | #else | |
352 | notice(s_GameServ, nick, welcomemsg, nick); | |
353 | #endif | |
354 | } | |
448a1531 | 355 | #ifdef P10 |
c8117c0f | 356 | unsigned long hv = sHASH((unsigned char *) nick); |
448a1531 | 357 | #else |
358 | unsigned long hv = iHASH((unsigned char *) nick); | |
359 | #endif | |
360 | ||
361 | temp = clients[hv].insertAtBack(newuser); | |
362 | ||
69fb5a93 | 363 | #if defined(HYBRID) || defined(BAHAMUT) |
448a1531 | 364 | char *nickserver; |
365 | strtok(NULL, " "); | |
366 | strtok(NULL, " "); | |
367 | nickserver = strtok(NULL, " "); | |
368 | if (nickserver[0] == '+') | |
369 | strtok(NULL, " "); | |
370 | strtok(NULL, " "); | |
371 | ||
903cd861 | 372 | nickserver = strtok(NULL, " "); |
373 | for (int x = 0; x < 32; x++) | |
374 | { | |
375 | if (stricmp(ignoreservers[x], nickserver) == 0) | |
376 | { | |
377 | setIgnore(temp); | |
378 | break; | |
379 | } | |
380 | } | |
381 | #elif defined(UNREAL) | |
382 | char *nickserver; | |
383 | strtok(NULL, " "); | |
384 | strtok(NULL, " "); | |
385 | strtok(NULL, " "); | |
386 | strtok(NULL, " "); | |
448a1531 | 387 | nickserver = strtok(NULL, " "); |
388 | for (int x = 0; x < 32; x++) | |
389 | { | |
390 | if (stricmp(ignoreservers[x], nickserver) == 0) | |
391 | { | |
392 | setIgnore(temp); | |
393 | break; | |
394 | } | |
395 | } | |
396 | #endif | |
85ce9d3e | 397 | delete newuser; |
398 | } | |
ba2a880f | 399 | #if defined(P10) |
400 | } else if (stricmp(cmd, "Q") == 0) { | |
0b259dff | 401 | // unsigned long hv = sHASH((unsigned char *) source); |
ba2a880f | 402 | #else |
85ce9d3e | 403 | } else if (stricmp(cmd, "QUIT") == 0) { |
0b259dff | 404 | // unsigned long hv = iHASH((unsigned char *) source); |
ba2a880f | 405 | #endif |
85ce9d3e | 406 | aClient *quitter; |
fc9d2643 | 407 | char z = source[0]; |
408 | ||
409 | if (z == ':') | |
410 | source++; | |
411 | ||
3f243b0b | 412 | if (!(quitter = find(source))) |
413 | { | |
414 | log("Fatal Error: could not find %s in the "\ | |
415 | "clients list", source); | |
416 | goto end; | |
417 | } | |
418 | ||
419 | logout(quitter); | |
420 | ||
421 | if (z == ':') | |
422 | source--; | |
423 | ||
424 | /* Attempting to use the logout() function | |
fc9d2643 | 425 | if ((quitter = find(source))) |
7996e5fd | 426 | clients[hv].remove(quitter); |
fc9d2643 | 427 | if ((quitter = findIRCplayer(source))) |
ee38284f | 428 | { |
7f17db99 | 429 | if (player_fight(quitter)) |
430 | { | |
431 | // Stop the fight on the other client | |
432 | aClient *otherplayer = quitter->stats->battle; | |
433 | otherplayer->stats->battle = NULL; | |
434 | notice(s_GameServ, otherplayer->getNick(), "%s "\ | |
435 | "has quit IRC. The fight stops here.", | |
436 | quitter->stats->name); | |
437 | } | |
438 | quitter->stats->battle = NULL; | |
439 | quitter->stats->fight = NULL; | |
440 | quitter->stats->master = NULL; | |
441 | ||
85bcf836 | 442 | quitter->setNick("Not Playing"); |
a5316c52 | 443 | #ifdef P10 |
85bcf836 | 444 | quitter->setRealNick("Not Playing"); |
a5316c52 | 445 | #endif |
85bcf836 | 446 | quitter->stats->client = NULL; // Unidentify them |
ee38284f | 447 | } |
3f243b0b | 448 | */ |
fc9d2643 | 449 | |
ba2a880f | 450 | #if defined(P10) |
451 | } else if (stricmp(cmd, "P") == 0) { | |
452 | char *rest, *dest; | |
453 | char *longname; | |
454 | longname = new char[strlen(s_GameServ) + strlen(servername) + 2]; | |
455 | ||
456 | sprintf(longname, "%S@%s", servername); | |
85ce9d3e | 457 | |
ba2a880f | 458 | dest = strtok(NULL, " "); |
459 | rest = strtok(NULL, ""); | |
460 | if (stricmp(dest, gsnum) == 0 || stricmp(dest, longname) == 0) | |
461 | { | |
462 | delete [] longname; | |
463 | gameserv(source, rest); | |
464 | } | |
6f727d4c | 465 | else if (stricmp(dest, c_Forest) == 0 && isListenOnCF()) |
ba2a880f | 466 | { |
467 | delete [] longname; | |
468 | forest(source, rest); | |
469 | } | |
470 | #else | |
85ce9d3e | 471 | } else if (stricmp(cmd, "PRIVMSG") == 0) { |
472 | char *rest, *dest; | |
473 | dest = strtok(NULL, " "); | |
474 | rest = strtok(NULL, ""); | |
ad7dfaa0 | 475 | if (strnicmp(dest, s_GameServ, strlen(s_GameServ)) == 0) |
85ce9d3e | 476 | gameserv(source, rest); |
1546c3da | 477 | else if (stricmp(dest, c_Forest) == 0 && isListenOnCF()) |
85ce9d3e | 478 | forest(source, rest); |
ba2a880f | 479 | #endif |
f2072f1a | 480 | #if defined(P10) |
481 | } else if (stricmp(cmd, "J") == 0) { | |
482 | #else | |
85ce9d3e | 483 | } else if (stricmp(cmd, "JOIN") == 0) { |
f2072f1a | 484 | #endif |
85ce9d3e | 485 | char *channel; |
f2072f1a | 486 | aClient *joiner; |
85ce9d3e | 487 | channel = strtok(NULL, " "); |
f2072f1a | 488 | |
489 | char z = source[0]; | |
490 | ||
491 | if (z == ':') | |
492 | source++; | |
493 | ||
494 | joiner = find(source); | |
495 | ||
496 | if (stricmp(channel, c_Forest) == 0 && is_playing(joiner)) | |
497 | { | |
498 | #ifdef DEBUGMODE | |
499 | log("Player %s (IRC: %s) joined %s", | |
500 | joiner->stats->name, | |
501 | #ifdef P10 | |
502 | joiner->getRealNick(), | |
503 | #else | |
504 | joiner->getNick(), | |
505 | #endif | |
506 | c_Forest); | |
507 | #endif | |
508 | raw(":%S MODE %s +v %s", c_Forest, (source)); | |
509 | } | |
510 | ||
511 | if (z == ':') | |
512 | source--; | |
c7340cbd | 513 | |
514 | #if defined(BAHAMUT) | |
515 | } else if (stricmp(cmd, "SJOIN") == 0) { | |
581ec09e | 516 | char *channel, *nick, *tmp, *rest; |
c7340cbd | 517 | strtok(NULL, " "); // Ignore the TS |
581ec09e | 518 | #ifndef HYBRID |
c7340cbd | 519 | strtok(NULL, " "); // Ignore the TS |
581ec09e | 520 | #endif |
c7340cbd | 521 | channel = strtok(NULL, " "); |
581ec09e | 522 | rest = strtok(NULL, ""); |
523 | tmp = strchr(rest, ':'); | |
524 | tmp++; | |
525 | nick = strtok(tmp, " "); | |
526 | while (nick != NULL) | |
527 | { | |
528 | if (*nick == '@') | |
529 | nick++; | |
530 | if (*nick == '+') | |
531 | nick++; // Assume for users set op and voice, they | |
532 | // are never passed as +@nick | |
533 | if (stricmp(channel, c_Forest) == 0 && is_playing(nick)) | |
534 | raw(":%S MODE %s +v %s", channel, nick); | |
535 | ||
536 | nick = strtok(NULL, " "); | |
537 | } | |
538 | #endif | |
85ce9d3e | 539 | } else { |
9f8c2acc | 540 | #ifdef DEBUGMODE |
541 | log("Unrecognized Message: cmd = %s source = %s", cmd, source); | |
542 | #endif | |
85ce9d3e | 543 | } |
544 | } | |
4dde2ed9 | 545 | |
05c527e6 | 546 | } // for loop for connection retry |
547 | ||
4dde2ed9 | 548 | end: |
549 | ||
c8ada07e | 550 | save_gs_dbase(); |
5aa1d28d | 551 | saveNews(newsdata, todaysnews); |
abbfcdb9 | 552 | save_day(); |
4dde2ed9 | 553 | |
c8ada07e | 554 | delete_monsters(); |
555 | delete_masters(); | |
556 | ||
9f8c2acc | 557 | #ifdef DEBUGMODE |
558 | log("<CLOSED>"); | |
559 | #endif | |
560 | ||
85ce9d3e | 561 | close(sock); |
562 | unload_config_file(); | |
563 | return 0; | |
564 | } | |
565 | ||
566 | aClient *find(char *nick) | |
567 | { | |
568 | return findbynick(nick); | |
569 | } | |
570 | ||
571 | aClient *find(const char *nick) | |
572 | { | |
573 | return findbynick(nick); | |
574 | } | |
575 | ||
ce61cdfa | 576 | #ifdef P10 |
577 | ||
578 | aClient *findbyrealnick(char *realnick) | |
579 | { | |
580 | ListNode <aClient> *newPtr; | |
448a1531 | 581 | unsigned long hv = sHASH((unsigned char *) realnick); |
7996e5fd | 582 | newPtr = clients[hv].First(); |
ce61cdfa | 583 | |
584 | aClient *client = NULL; | |
585 | ||
586 | while (newPtr) | |
587 | { | |
588 | client = newPtr->getData(); | |
589 | if (stricmp(client->getRealNick(), realnick) == 0) | |
590 | return client; | |
591 | client = NULL; | |
592 | newPtr = newPtr->Next(); | |
593 | } | |
594 | return client; | |
595 | } | |
4e5760fd | 596 | |
597 | #else | |
598 | ||
599 | aClient *findbyrealnick(char *realnick) | |
600 | { | |
601 | return findbynick(realnick); | |
602 | } | |
603 | ||
ce61cdfa | 604 | #endif |
85ce9d3e | 605 | |
606 | aClient *findbynick(char *nick) | |
607 | { | |
608 | ListNode <aClient> *newPtr; | |
448a1531 | 609 | #ifdef P10 |
610 | unsigned long hv = sHASH((unsigned char *) nick); | |
611 | #else | |
612 | unsigned long hv = iHASH((unsigned char *) nick); | |
613 | #endif | |
614 | ||
7996e5fd | 615 | newPtr = clients[hv].First(); |
85ce9d3e | 616 | |
617 | aClient *client = NULL; | |
618 | ||
619 | while (newPtr) | |
620 | { | |
621 | client = newPtr->getData(); | |
23ec3ff4 | 622 | #ifdef P10 |
23ec3ff4 | 623 | if (strcmp(client->getNick(), nick) == 0) |
624 | #else | |
625 | if (stricmp(client->getNick(), nick) == 0) | |
626 | #endif | |
85ce9d3e | 627 | return client; |
628 | client = NULL; | |
629 | newPtr = newPtr->Next(); | |
630 | } | |
631 | return client; | |
632 | } | |
633 | ||
ee38284f | 634 | aClient *findIRCplayer(const char *nick) |
635 | { | |
636 | ListNode <aClient> *newPtr; | |
637 | aClient *p = NULL; | |
638 | ||
448a1531 | 639 | p = find(nick); |
640 | ||
641 | if (!is_playing(p)) | |
642 | return NULL; | |
643 | ||
644 | unsigned long hv = iHASH((unsigned char *) p->stats->name); | |
645 | ||
7996e5fd | 646 | for (newPtr = players[hv].First(); newPtr; newPtr = newPtr->Next()) |
ee38284f | 647 | { |
648 | p = newPtr->getData(); | |
23ec3ff4 | 649 | #ifdef P10 |
650 | if (strcmp(p->getNick(), nick) == 0) | |
651 | #else | |
652 | if (stricmp(p->getNick(), nick) == 0) | |
653 | #endif | |
ee38284f | 654 | return p; |
655 | p = NULL; | |
656 | } | |
657 | return NULL; | |
658 | } | |
40251952 | 659 | |
0a1518fa | 660 | aClient *findplayer(const char *name) |
661 | { | |
662 | ListNode <aClient> *newPtr; | |
663 | Player *p = NULL; | |
448a1531 | 664 | unsigned long hv = iHASH((unsigned char *) name); |
7996e5fd | 665 | for (newPtr = players[hv].First(); newPtr; newPtr = newPtr->Next()) |
0a1518fa | 666 | { |
667 | p = newPtr->getData()->stats; | |
668 | if (stricmp(p->name, name) == 0) | |
669 | return newPtr->getData(); | |
670 | p = NULL; | |
671 | } | |
672 | return NULL; | |
673 | } | |
674 | ||
40251952 | 675 | void check_idles() |
676 | { | |
677 | ListNode <aClient> *newPtr; | |
678 | Player *p = NULL; | |
679 | ||
680 | for (int x = 0; x < U_TABLE_SIZE; x++) | |
681 | { | |
682 | for (newPtr = players[x].First(); newPtr; newPtr = newPtr->Next()) | |
683 | { | |
684 | p = newPtr->getData()->stats; | |
685 | if (timedOut(p)) | |
686 | { | |
687 | timeOutEvent(p); | |
688 | } | |
689 | } | |
690 | } | |
691 | } | |
692 | ||
85ce9d3e | 693 | aClient *findbynick(const char *nick) |
694 | { | |
695 | ListNode <aClient> *newPtr; | |
448a1531 | 696 | #ifdef P10 |
697 | unsigned long hv = sHASH((unsigned char *) nick); | |
698 | #else | |
699 | unsigned long hv = iHASH((unsigned char *) nick); | |
700 | #endif | |
701 | ||
7996e5fd | 702 | newPtr = clients[hv].First(); |
85ce9d3e | 703 | |
704 | aClient *client = NULL; | |
705 | ||
706 | while (newPtr) | |
707 | { | |
708 | client = newPtr->getData(); | |
23ec3ff4 | 709 | #ifdef P10 |
710 | if (strcmp(client->getNick(), nick) == 0) | |
711 | #else | |
712 | if (stricmp(client->getNick(), nick) == 0) | |
713 | #endif | |
85ce9d3e | 714 | return client; |
715 | client = NULL; | |
716 | newPtr = newPtr->Next(); | |
717 | } | |
718 | return client; | |
719 | } | |
720 | ||
abbfcdb9 | 721 | void load_day() |
44ea29f7 | 722 | { |
723 | ifstream infile; | |
724 | ||
abbfcdb9 | 725 | infile.open(".gsday"); |
44ea29f7 | 726 | |
727 | if (infile.fail()) | |
728 | { | |
9f8c2acc | 729 | #ifdef DEBUGMODE |
abbfcdb9 | 730 | log("Error opening .gsday"); |
9f8c2acc | 731 | #endif |
732 | ||
44ea29f7 | 733 | generate: |
9f8c2acc | 734 | #ifdef DEBUGMODE |
abbfcdb9 | 735 | log("Generating new day"); |
9f8c2acc | 736 | #endif |
abbfcdb9 | 737 | struct tm *tm; |
738 | time_t ti; | |
739 | time(&ti); | |
740 | tm = localtime(&ti); | |
741 | ||
742 | day = tm->tm_mday; | |
743 | ||
744 | save_day(); | |
44ea29f7 | 745 | return; |
746 | } | |
747 | ||
abbfcdb9 | 748 | infile >> day; |
44ea29f7 | 749 | infile.close(); |
abbfcdb9 | 750 | if (day < 1 || day > 31) |
44ea29f7 | 751 | goto generate; |
752 | } | |
753 | ||
abbfcdb9 | 754 | void save_day() |
44ea29f7 | 755 | { |
756 | ofstream outfile; | |
757 | ||
abbfcdb9 | 758 | outfile.open(".gsday"); |
44ea29f7 | 759 | |
760 | if (outfile.fail()) | |
761 | { | |
abbfcdb9 | 762 | log("Error creating new file .gsday"); |
44ea29f7 | 763 | return; |
764 | } | |
765 | ||
abbfcdb9 | 766 | outfile << day << endl; |
44ea29f7 | 767 | |
768 | outfile.close(); | |
769 | } | |
770 | ||
ce61cdfa | 771 | /* daemon() - detach process from user and disappear into the background |
772 | * returns -1 on failure, but you can't do much except exit in that case | |
773 | * since we may already have forked. This is based on the BSD version, | |
774 | * so the caller is responsible for things like the umask, etc. | |
775 | */ | |
776 | ||
777 | /* believed to work on all Posix systems */ | |
778 | ||
779 | int daemon(int nochdir, int noclose) | |
780 | { | |
781 | pid_t pid; | |
782 | switch (pid = fork()) | |
783 | { | |
784 | case 0: break; | |
785 | case -1: return -1; | |
786 | default: _exit(0); /* exit the original process */ | |
787 | } | |
788 | ||
789 | if (setsid() < 0) /* shoudn't fail */ | |
790 | return -1; | |
791 | ||
792 | /* dyke out this switch if you want to acquire a control tty in */ | |
793 | /* the future -- not normally advisable for daemons */ | |
794 | ||
795 | switch (pid = fork()) | |
796 | { | |
797 | case 0: break; | |
798 | case -1: return -1; | |
799 | default: | |
800 | ofstream outfile; | |
69ae096c | 801 | outfile.open(pidfile); |
ce61cdfa | 802 | if (outfile.fail()) |
69ae096c | 803 | cerr << "Unable to open " << pidfile << endl; |
ce61cdfa | 804 | outfile << pid << endl; |
805 | outfile.close(); | |
806 | ||
807 | _exit(0); | |
808 | } | |
809 | ||
810 | if (!nochdir) | |
811 | chdir("/"); | |
812 | ||
813 | if (!noclose) | |
814 | { | |
815 | closeall(0); | |
816 | open("/dev/null",O_RDWR); | |
817 | dup(0); dup(0); | |
818 | } | |
819 | ||
820 | return 0; | |
821 | } | |
822 | ||
823 | ||
824 | /* closeall() -- close all FDs >= a specified value */ | |
825 | ||
826 | void closeall(int fd) | |
827 | { | |
828 | int fdlimit = sysconf(_SC_OPEN_MAX); | |
829 | ||
830 | while (fd < fdlimit) | |
831 | close(fd++); | |
832 | } | |
833 | ||
15838737 | 834 | void prettyIntro() |
835 | { | |
836 | cout << endl; | |
837 | cout << " GGGG AAA MM MM EEEEEEE SSSSS EEEEEEE RRRRRR VV VV " << endl; | |
838 | cout << " GG GG AAAAA MMM MMM EE SS EE RR RR VV VV " << endl; | |
839 | cout << "GG AA AA MM MM MM EEEEE SSSSS EEEEE RRRRRR VV VV " << endl; | |
840 | cout << "GG GGG AAAAAAA MM MM EE SS EE RR RR VV VV " << endl; | |
841 | cout << "G G AA AA MM MM EEEEEEE SSSSS EEEEEEE RR RR VVV" << endl; | |
842 | cout << " GGGGG V\n\n" << endl; | |
843 | cout << "Version: " << VERSION << endl; | |
844 | } |