]>
Commit | Line | Data |
---|---|---|
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" | |
16 | #include "options.h" | |
17 | #include "list.h" | |
18 | #include "aClient.h" | |
19 | #include "extern.h" | |
20 | #include "flags.h" | |
21 | #include <stdio.h> | |
22 | #include <unistd.h> | |
23 | #include <string.h> | |
24 | #include <fstream> | |
25 | #include <stdlib.h> | |
26 | #include <fcntl.h> | |
27 | #include <signal.h> | |
28 | ||
29 | //#include <sys/types.h> | |
30 | //#include <sys/wait.h> | |
31 | //#include <errno.h> | |
32 | ||
33 | using namespace std; | |
34 | ||
35 | char *PACKAGE = "GameServ"; | |
36 | char *VERSION = "1.3.0 +devel"; | |
37 | ||
38 | int sock; | |
39 | long lastrefresh; | |
40 | long lastrollover; | |
41 | ||
42 | List<aClient> clients[U_TABLE_SIZE]; | |
43 | ||
44 | void save_lastrefresh(); | |
45 | void load_lastrefresh(); | |
46 | void load_lastrollover(); | |
47 | void save_lastrollover(); | |
48 | void prettyIntro(); | |
49 | void check_idles(); | |
50 | ||
51 | // Make this a daemon | |
52 | int daemon(int nochdir, int noclose); | |
53 | ||
54 | // Close all file descriptors from >= fd | |
55 | void closeall(int fd); | |
56 | ||
57 | int main(int argc, char *argv[]) | |
58 | { | |
59 | char buffer[1024], buf[1024]; | |
60 | memset(buffer, 0, 1024); | |
61 | memset(buf, 0, 1024); | |
62 | int connected; | |
63 | long lastidlecheck; | |
64 | char *cmd, *source = NULL, *conf; | |
65 | srand(time(NULL)); | |
66 | conf = new char[16]; | |
67 | strcpy(conf, "gameserv.conf"); | |
68 | ||
69 | /* | |
70 | * This needs to be fixed to work for any number of arguments in any | |
71 | * order | |
72 | * | |
73 | */ | |
74 | if (argc > 1) | |
75 | { | |
76 | if ( argc > 2 || stricmp(argv[1], "--help") == 0) | |
77 | { | |
78 | cout << "Usage: gameserv [options] [configfile]" << endl; | |
79 | cout << "Options:" << endl; | |
80 | cout << "--help Displays this help dialogue" << endl; | |
81 | return 1; | |
82 | } | |
83 | delete []conf; | |
84 | conf = argv[1]; | |
85 | } | |
86 | ||
87 | prettyIntro(); | |
88 | ||
89 | if (load_config_file(conf)) | |
90 | { | |
91 | cout << "Config file loaded ok...\n" | |
92 | << "Turning into a daemon" << endl; | |
93 | } | |
94 | else | |
95 | exit(2); | |
96 | ||
97 | if (argc <= 1) | |
98 | delete []conf; | |
99 | ||
100 | // Turn into a daemon | |
101 | if (daemon(1,0) < 0) | |
102 | { | |
103 | perror("Could not turn into a daemon"); | |
104 | exit(3); | |
105 | } | |
106 | if (load_items() == 0) | |
107 | { | |
108 | log("Error loading items"); | |
109 | goto end; | |
110 | } | |
111 | ||
112 | if (load_store() == 0) | |
113 | { | |
114 | log("Error loading store"); | |
115 | goto end; | |
116 | } | |
117 | if (load_tavern() == 0) | |
118 | { | |
119 | log("Error loading tavern"); | |
120 | goto end; | |
121 | } | |
122 | ||
123 | load_gs_dbase(); | |
124 | loadNews(newsdata, todaysnews); | |
125 | ||
126 | ||
127 | if (load_masters() == false) | |
128 | { | |
129 | log("Error loading masters"); | |
130 | goto end; | |
131 | } | |
132 | ||
133 | if (load_monsters() == false) | |
134 | { | |
135 | log("Error loading monsters"); | |
136 | goto end; | |
137 | } | |
138 | ||
139 | if (!load_dragon()) | |
140 | { | |
141 | log("Error loading dragon"); | |
142 | goto end; | |
143 | } | |
144 | ||
145 | if (load_levels() == false) | |
146 | { | |
147 | log("Error loading levels"); | |
148 | goto end; | |
149 | } | |
150 | ||
151 | shuttingdown = false; | |
152 | ||
153 | char ignoreservers[32][256]; | |
154 | char *currentserver; | |
155 | currentserver = strtok(ignoreserverslist, " "); | |
156 | for (int server = 0; server < 32 && currentserver != NULL; server++) | |
157 | { | |
158 | strncpy(ignoreservers[server], currentserver, 255); | |
159 | log("Placing %s on the server ignore list", currentserver); | |
160 | currentserver = strtok(NULL, " "); | |
161 | } | |
162 | ||
163 | // This loop will retry the connection 3 times | |
164 | for (int retry = 0; retry < 3 && !shuttingdown; retry++) | |
165 | { | |
166 | connected = 1; | |
167 | load_lastrefresh(); | |
168 | load_lastrollover(); | |
169 | ||
170 | long int loadtime = time(NULL); | |
171 | long int currentTime; | |
172 | long int oldTime = loadtime; | |
173 | ||
174 | lastidlecheck = loadtime; | |
175 | ||
176 | #ifdef DEBUGMODE | |
177 | log("Setting primary Idle Check timestamp: %ld", lastidlecheck); | |
178 | #endif | |
179 | bool loaded = false; | |
180 | ||
181 | ignore_pipe(); | |
182 | sock = conn(remoteserver, remoteport, localhost, 0); | |
183 | // sock = make_connection(remoteport, SOCK_STREAM, remoteserver); | |
184 | if (sock == -1) { | |
185 | fprintf(stderr,"make_connection failed.\n"); | |
186 | unload_config_file(); | |
187 | return -1; | |
188 | } | |
189 | log("%S socket connected."); | |
190 | ||
191 | #ifdef UNREAL | |
192 | raw("PASS :%s", remotepass); | |
193 | raw("SERVER %s 1 :%s", servername, servername); | |
194 | raw("NICK %S 1 %d %S %s %s %d :%s v%s", time(NULL), gshost, | |
195 | servername, time(NULL), PACKAGE, VERSION); | |
196 | raw(":%S JOIN %s", c_Forest); | |
197 | raw(":%S MODE %s +tn", c_Forest); | |
198 | #elif defined(BAHAMUT) | |
199 | raw("PASS %s :TS", remotepass); | |
200 | raw("SERVER %s 1 :%s", servername, servername); | |
201 | raw("NICK %S 1 %d +w%s %s %s %s 0 :GameServ", time(NULL), (isBOper() ? "o" : ""), | |
202 | gsident, gshost, servername); | |
203 | raw(":%s SJOIN %d %d %s +nt :@%S", servername, time(NULL), time(NULL), c_Forest); | |
204 | #elif defined(HYBRID) | |
205 | raw("PASS %s :TS", remotepass); | |
206 | raw("SERVER %s 1 :%s", servername, servername); | |
207 | raw("NICK %S 1 %d +w%s %s %s %s :GameServ", time(NULL), (isBOper() ? "o" : ""), | |
208 | gsident, gshost, servername); | |
209 | raw(":%s SJOIN %ld %s +nt :@%S", servername, time(NULL), c_Forest); | |
210 | #elif defined(ULTIMATE2) | |
211 | raw("PASS %s :TS", remotepass); | |
212 | raw("SERVER %s 1 :%s", servername, servername); | |
213 | raw("NICK %S 1 %d %s %s %s 0 :GameServ", | |
214 | time(NULL), gsident, gshost, servername); | |
215 | if (isBOper()) | |
216 | raw(":%S mode %S +o"); | |
217 | raw(":%S JOIN %s", c_Forest); | |
218 | #elif defined(P10) | |
219 | // Server numeric is: [] <-- must be unique | |
220 | raw("PASS :%s", remotepass); | |
221 | raw("SERVER %s 1 %d %d P10 []AAF :%s", servername, time(NULL), time(NULL), servername); | |
222 | raw("[] N %S 1 %d %s %s %s DAqAoB %s :%S", time(NULL), gsident, gshost, | |
223 | (isBOper() ? "+o" : ""), gsnum); | |
224 | raw("[] B %s %d +tn %s:o", c_Forest, time(NULL) - 864000, gsnum); | |
225 | #endif | |
226 | ||
227 | #if defined(P10) | |
228 | raw("%s T %s :%s", gsnum, c_Forest, c_ForestTopic); | |
229 | raw("[] EB"); // End burst | |
230 | #else | |
231 | #ifndef HYBRID | |
232 | #if defined(ULTIMATE2) | |
233 | raw(":%s MODE %s +o %S %ld", servername, c_Forest, | |
234 | time(NULL)); | |
235 | #else | |
236 | raw(":%S MODE %s +o %S", c_Forest); | |
237 | #endif | |
238 | #endif | |
239 | raw(":%S TOPIC %s :%s", c_Forest, c_ForestTopic); | |
240 | #endif | |
241 | ||
242 | #ifndef P10 | |
243 | if (isUseNickServ()) | |
244 | { | |
245 | raw(":%S PRIVMSG %s :IDENTIFY %s", nsname, nspass); | |
246 | } | |
247 | #endif | |
248 | sock_gets(sock,buffer,sizeof(buffer)-1); /* -1 added thanks to | |
249 | David Duchene <dave@ltd.com> for pointing out the possible | |
250 | buffer overflow resulting from the linefeed added below. */ | |
251 | ||
252 | ||
253 | #ifdef DEBUGMODE | |
254 | log("Server: %s",buffer); | |
255 | #endif | |
256 | ||
257 | while (connected) { | |
258 | if (sock_gets(sock,buffer,sizeof(buffer)) == -1) { | |
259 | connected = 0; | |
260 | } | |
261 | strcpy(buf, buffer); | |
262 | ||
263 | #if !defined(P10) | |
264 | if (buffer[0] == ':') | |
265 | { | |
266 | source = strtok(buf, " "); | |
267 | cmd = strtok(NULL, " "); | |
268 | } | |
269 | else | |
270 | cmd = strtok(buf, " "); | |
271 | #else | |
272 | source = strtok(buf, " "); | |
273 | cmd = strtok(NULL, " "); | |
274 | #endif | |
275 | ||
276 | #ifdef DEBUGMODE | |
277 | log("Server: %s", buffer); | |
278 | #endif | |
279 | ||
280 | // Wait N seconds then we're loaded. | |
281 | if (!loaded) | |
282 | { | |
283 | if (time(NULL) >= welcomedelay + loadtime) | |
284 | { | |
285 | loaded = true; | |
286 | retry = 0; // Start the reconnection cycle over | |
287 | } | |
288 | } | |
289 | else | |
290 | { | |
291 | long TIME = time(NULL); | |
292 | if (TIME - lastidlecheck >= idlecheckperiod) | |
293 | { | |
294 | check_idles(); | |
295 | lastidlecheck = TIME; | |
296 | } | |
297 | } | |
298 | ||
299 | // Refresh players and clear news if the time is up | |
300 | currentTime = time(NULL); | |
301 | ||
302 | if (isRolloverForestFights()) | |
303 | { | |
304 | if (currentTime - lastrollover >= rolloverperiod) | |
305 | { | |
306 | rolloverall(); | |
307 | lastrollover = currentTime; | |
308 | save_lastrollover(); | |
309 | notice(s_GameServ, c_Forest, "Adding %d forest fights to all players!", forestfights); | |
310 | } | |
311 | } | |
312 | ||
313 | if (currentTime - lastrefresh >= refreshperiod) | |
314 | { | |
315 | refreshall(); | |
316 | clearNews(todaysnews); | |
317 | saveNews(newsdata, todaysnews); | |
318 | lastrefresh = currentTime; | |
319 | save_lastrefresh(); | |
320 | notice(s_GameServ, c_Forest, "Refreshing all players "\ | |
321 | "and resetting news!"); | |
322 | } | |
323 | ||
324 | // Save the player data every updateperiod seconds | |
325 | if (currentTime - oldTime >= updateperiod) | |
326 | { | |
327 | oldTime = currentTime; | |
328 | log("Saving to %s", playerdata); | |
329 | ||
330 | save_gs_dbase(); | |
331 | saveNews(newsdata, todaysnews); | |
332 | if (isSavedNotice()) | |
333 | { | |
334 | // Send notice to the channel of the update | |
335 | notice(s_GameServ, c_Forest, "%S player data saved"); | |
336 | } | |
337 | } | |
338 | ||
339 | ||
340 | #if !defined(P10) | |
341 | if (stricmp(cmd, "PING") == 0) { | |
342 | char *timestamp; | |
343 | timestamp = strtok(NULL, ""); | |
344 | raw("PONG %s", timestamp); | |
345 | #else | |
346 | if (stricmp(cmd, "G") == 0) { | |
347 | char *timestamp; | |
348 | timestamp = strtok(NULL, " "); | |
349 | raw("[] Z [] %s 0 %s", timestamp + 1, timestamp); | |
350 | #endif | |
351 | #ifdef P10 | |
352 | } else if (stricmp(cmd, "EB") == 0) { | |
353 | raw("[] EA"); | |
354 | #endif | |
355 | } else if (stricmp(cmd, "VERSION") == 0) { | |
356 | char *server; | |
357 | server = strtok(NULL, " "); | |
358 | server++; | |
359 | raw(":%s 351 %s %s_%s. %s", servername, source+1, PACKAGE, VERSION, servername); | |
360 | #if !defined(P10) | |
361 | } else if (strncmp(cmd, "NICK", 4) == 0) { | |
362 | if (buffer[0] == ':') | |
363 | { | |
364 | aClient *tempPtr; | |
365 | if ((tempPtr = find((source + 1)))) | |
366 | { | |
367 | char *nick; | |
368 | unsigned long oldhv, newhv; | |
369 | nick = strtok(NULL, " "); | |
370 | oldhv = iHASH((unsigned char *) tempPtr->getNick()); | |
371 | newhv = iHASH((unsigned char *) nick); | |
372 | tempPtr->setNick(nick); | |
373 | clients[oldhv].remove(tempPtr); | |
374 | clients[newhv].insertAtBack(tempPtr); | |
375 | } | |
376 | } | |
377 | else | |
378 | { | |
379 | char *nick; | |
380 | #else | |
381 | } else if (stricmp(cmd, "N") == 0 && strlen(source) == 2) { | |
382 | { | |
383 | char *nick, *realnick; | |
384 | realnick = strtok(NULL, " "); | |
385 | ||
386 | for (int x = 0; x < 5; x++) | |
387 | nick = strtok(NULL, " "); | |
388 | ||
389 | if (nick[0] == '+') | |
390 | { | |
391 | #ifdef DEBUGMODE | |
392 | log ("aClient has modes"); | |
393 | #endif | |
394 | ||
395 | // Searching for the +r mode (extra parameter) | |
396 | for (unsigned int count = 1; count < strlen(nick); count++) | |
397 | { | |
398 | if (nick[count] == 'r') | |
399 | { | |
400 | nick = strtok(NULL, " "); | |
401 | break; | |
402 | } | |
403 | } | |
404 | nick = strtok(NULL, " "); | |
405 | } | |
406 | #endif | |
407 | aClient *newuser, *temp; | |
408 | ||
409 | nick = strtok(NULL, " "); | |
410 | ||
411 | #ifdef P10 | |
412 | newuser = new aClient(nick, realnick); | |
413 | #else | |
414 | newuser = new aClient(nick); | |
415 | #endif | |
416 | ||
417 | ||
418 | if (loaded) | |
419 | ||
420 | if (isWelcome()) | |
421 | { | |
422 | #ifdef P10 | |
423 | notice(s_GameServ, nick, welcomemsg, realnick); | |
424 | #else | |
425 | notice(s_GameServ, nick, welcomemsg, nick); | |
426 | #endif | |
427 | } | |
428 | #ifdef P10 | |
429 | unsigned long hv = sHASH((unsigned char *) nick); | |
430 | #else | |
431 | unsigned long hv = iHASH((unsigned char *) nick); | |
432 | #endif | |
433 | ||
434 | temp = clients[hv].insertAtBack(newuser); | |
435 | ||
436 | #if defined(HYBRID) || defined(BAHAMUT) || defined(ULTIMATE2) | |
437 | char *nickserver; | |
438 | strtok(NULL, " "); | |
439 | strtok(NULL, " "); | |
440 | nickserver = strtok(NULL, " "); | |
441 | if (nickserver[0] == '+') | |
442 | strtok(NULL, " "); | |
443 | strtok(NULL, " "); | |
444 | nickserver = strtok(NULL, " "); | |
445 | for (int x = 0; x < 32; x++) | |
446 | { | |
447 | if (stricmp(ignoreservers[x], nickserver) == 0) | |
448 | { | |
449 | setIgnore(temp); | |
450 | break; | |
451 | } | |
452 | } | |
453 | #elif defined(UNREAL) | |
454 | char *nickserver; | |
455 | strtok(NULL, " "); | |
456 | strtok(NULL, " "); | |
457 | strtok(NULL, " "); | |
458 | strtok(NULL, " "); | |
459 | nickserver = strtok(NULL, " "); | |
460 | for (int x = 0; x < 32; x++) | |
461 | { | |
462 | if (stricmp(ignoreservers[x], nickserver) == 0) | |
463 | { | |
464 | setIgnore(temp); | |
465 | break; | |
466 | } | |
467 | } | |
468 | #endif | |
469 | delete newuser; | |
470 | } | |
471 | #if defined(P10) | |
472 | } else if (stricmp(cmd, "Q") == 0) { | |
473 | // unsigned long hv = sHASH((unsigned char *) source); | |
474 | #else | |
475 | } else if (stricmp(cmd, "QUIT") == 0) { | |
476 | // unsigned long hv = iHASH((unsigned char *) source); | |
477 | #endif | |
478 | aClient *quitter; | |
479 | char z = source[0]; | |
480 | ||
481 | if (z == ':') | |
482 | source++; | |
483 | ||
484 | if (!(quitter = find(source))) | |
485 | { | |
486 | log("Fatal Error: could not find %s in the "\ | |
487 | "clients list", source); | |
488 | goto end; | |
489 | } | |
490 | ||
491 | logout(quitter); | |
492 | ||
493 | if (z == ':') | |
494 | source--; | |
495 | ||
496 | /* Attempting to use the logout() function | |
497 | if ((quitter = find(source))) | |
498 | clients[hv].remove(quitter); | |
499 | if ((quitter = findIRCplayer(source))) | |
500 | { | |
501 | if (player_fight(quitter)) | |
502 | { | |
503 | // Stop the fight on the other client | |
504 | aClient *otherplayer = quitter->stats->battle; | |
505 | otherplayer->stats->battle = NULL; | |
506 | notice(s_GameServ, otherplayer->getNick(), "%s "\ | |
507 | "has quit IRC. The fight stops here.", | |
508 | quitter->stats->name); | |
509 | } | |
510 | quitter->stats->battle = NULL; | |
511 | quitter->stats->fight = NULL; | |
512 | quitter->stats->master = NULL; | |
513 | ||
514 | quitter->setNick("Not Playing"); | |
515 | #ifdef P10 | |
516 | quitter->setRealNick("Not Playing"); | |
517 | #endif | |
518 | quitter->stats->client = NULL; // Unidentify them | |
519 | } | |
520 | */ | |
521 | ||
522 | #if defined(P10) | |
523 | } else if (stricmp(cmd, "P") == 0) { | |
524 | char *rest, *dest; | |
525 | char *longname; | |
526 | longname = new char[strlen(s_GameServ) + strlen(servername) + 2]; | |
527 | ||
528 | sprintf(longname, "%S@%s", servername); | |
529 | ||
530 | dest = strtok(NULL, " "); | |
531 | rest = strtok(NULL, ""); | |
532 | if (stricmp(dest, gsnum) == 0 || stricmp(dest, longname) == 0) | |
533 | { | |
534 | delete [] longname; | |
535 | gameserv(source, rest); | |
536 | } | |
537 | else if (stricmp(dest, c_Forest) == 0 && isListenOnCF()) | |
538 | { | |
539 | delete [] longname; | |
540 | forest(source, rest); | |
541 | } | |
542 | #else | |
543 | } else if (stricmp(cmd, "PRIVMSG") == 0) { | |
544 | char *rest, *dest; | |
545 | dest = strtok(NULL, " "); | |
546 | rest = strtok(NULL, ""); | |
547 | if (strnicmp(dest, s_GameServ, strlen(s_GameServ)) == 0) | |
548 | gameserv(source, rest); | |
549 | else if (stricmp(dest, c_Forest) == 0 && isListenOnCF()) | |
550 | forest(source, rest); | |
551 | #endif | |
552 | #if defined(P10) | |
553 | } else if (stricmp(cmd, "J") == 0) { | |
554 | #else | |
555 | } else if (stricmp(cmd, "JOIN") == 0) { | |
556 | #endif | |
557 | char *channel; | |
558 | aClient *joiner; | |
559 | channel = strtok(NULL, " "); | |
560 | ||
561 | char z = source[0]; | |
562 | ||
563 | if (z == ':') | |
564 | source++; | |
565 | ||
566 | joiner = find(source); | |
567 | ||
568 | if (stricmp(channel, c_Forest) == 0 && is_playing(joiner)) | |
569 | { | |
570 | #ifdef DEBUGMODE | |
571 | log("Player %s (IRC: %s) joined %s", | |
572 | joiner->stats->name.c_str(), | |
573 | #ifdef P10 | |
574 | joiner->getRealNick(), | |
575 | #else | |
576 | joiner->getNick(), | |
577 | #endif | |
578 | c_Forest); | |
579 | #endif | |
580 | raw(":%S MODE %s +v %s", c_Forest, (source)); | |
581 | } | |
582 | ||
583 | if (z == ':') | |
584 | source--; | |
585 | ||
586 | #if defined(BAHAMUT) | |
587 | } else if (stricmp(cmd, "SJOIN") == 0) { | |
588 | char *channel, *nick, *tmp, *rest; | |
589 | strtok(NULL, " "); // Ignore the TS | |
590 | #ifndef HYBRID | |
591 | strtok(NULL, " "); // Ignore the TS | |
592 | #endif | |
593 | channel = strtok(NULL, " "); | |
594 | rest = strtok(NULL, ""); | |
595 | tmp = strchr(rest, ':'); | |
596 | tmp++; | |
597 | nick = strtok(tmp, " "); | |
598 | while (nick != NULL) | |
599 | { | |
600 | if (*nick == '@') | |
601 | nick++; | |
602 | if (*nick == '+') | |
603 | nick++; // Assume for users set op and voice, they | |
604 | // are never passed as +@nick | |
605 | if (stricmp(channel, c_Forest) == 0 && is_playing(nick)) | |
606 | raw(":%S MODE %s +v %s", channel, nick); | |
607 | ||
608 | nick = strtok(NULL, " "); | |
609 | } | |
610 | #endif | |
611 | } else { | |
612 | #ifdef DEBUGMODE | |
613 | log("Unrecognized Message: cmd = %s source = %s", cmd, source); | |
614 | #endif | |
615 | } | |
616 | } | |
617 | ||
618 | } // for loop for connection retry | |
619 | ||
620 | end: | |
621 | ||
622 | save_gs_dbase(); | |
623 | save_dragon(); | |
624 | saveNews(newsdata, todaysnews); | |
625 | ||
626 | delete_monsters(); | |
627 | ||
628 | #ifdef DEBUGMODE | |
629 | log("<CLOSED>"); | |
630 | #endif | |
631 | ||
632 | close(sock); | |
633 | unload_config_file(); | |
634 | return 0; | |
635 | } | |
636 | ||
637 | aClient *find(char *nick) | |
638 | { | |
639 | return findbynick(nick); | |
640 | } | |
641 | ||
642 | aClient *find(const char *nick) | |
643 | { | |
644 | return findbynick(nick); | |
645 | } | |
646 | ||
647 | #ifdef P10 | |
648 | ||
649 | aClient *findbyrealnick(char *realnick) | |
650 | { | |
651 | ListNode <aClient> *newPtr; | |
652 | unsigned long hv = sHASH((unsigned char *) realnick); | |
653 | newPtr = clients[hv].First(); | |
654 | ||
655 | aClient *client = NULL; | |
656 | ||
657 | while (newPtr) | |
658 | { | |
659 | client = newPtr->getData(); | |
660 | if (stricmp(client->getRealNick(), realnick) == 0) | |
661 | return client; | |
662 | client = NULL; | |
663 | newPtr = newPtr->Next(); | |
664 | } | |
665 | return client; | |
666 | } | |
667 | ||
668 | #else | |
669 | ||
670 | aClient *findbyrealnick(char *realnick) | |
671 | { | |
672 | return findbynick(realnick); | |
673 | } | |
674 | ||
675 | #endif | |
676 | ||
677 | aClient *findbynick(char *nick) | |
678 | { | |
679 | ListNode <aClient> *newPtr; | |
680 | #ifdef P10 | |
681 | unsigned long hv = sHASH((unsigned char *) nick); | |
682 | #else | |
683 | unsigned long hv = iHASH((unsigned char *) nick); | |
684 | #endif | |
685 | ||
686 | newPtr = clients[hv].First(); | |
687 | ||
688 | aClient *client = NULL; | |
689 | ||
690 | while (newPtr) | |
691 | { | |
692 | client = newPtr->getData(); | |
693 | #ifdef P10 | |
694 | if (strcmp(client->getNick(), nick) == 0) | |
695 | #else | |
696 | if (stricmp(client->getNick(), nick) == 0) | |
697 | #endif | |
698 | return client; | |
699 | client = NULL; | |
700 | newPtr = newPtr->Next(); | |
701 | } | |
702 | return client; | |
703 | } | |
704 | ||
705 | aClient *findIRCplayer(const char *nick) | |
706 | { | |
707 | ListNode <aClient> *newPtr; | |
708 | aClient *p = NULL; | |
709 | ||
710 | p = find(nick); | |
711 | ||
712 | if (!is_playing(p)) | |
713 | return NULL; | |
714 | ||
715 | unsigned long hv = iHASH((unsigned char *) p->stats->name.c_str()); | |
716 | ||
717 | for (newPtr = players[hv].First(); newPtr; newPtr = newPtr->Next()) | |
718 | { | |
719 | p = newPtr->getData(); | |
720 | #ifdef P10 | |
721 | if (strcmp(p->getNick(), nick) == 0) | |
722 | #else | |
723 | if (stricmp(p->getNick(), nick) == 0) | |
724 | #endif | |
725 | return p; | |
726 | p = NULL; | |
727 | } | |
728 | return NULL; | |
729 | } | |
730 | ||
731 | aClient *findplayer(const char *name) | |
732 | { | |
733 | ListNode <aClient> *newPtr; | |
734 | Player *p = NULL; | |
735 | unsigned long hv = iHASH((unsigned char *) name); | |
736 | for (newPtr = players[hv].First(); newPtr; newPtr = newPtr->Next()) | |
737 | { | |
738 | p = newPtr->getData()->stats; | |
739 | if (stricmp(p->name.c_str(), name) == 0) | |
740 | return newPtr->getData(); | |
741 | p = NULL; | |
742 | } | |
743 | return NULL; | |
744 | } | |
745 | ||
746 | void check_idles() | |
747 | { | |
748 | ListNode <aClient> *newPtr; | |
749 | Player *p = NULL; | |
750 | ||
751 | for (int x = 0; x < U_TABLE_SIZE; x++) | |
752 | { | |
753 | for (newPtr = players[x].First(); newPtr; newPtr = newPtr->Next()) | |
754 | { | |
755 | p = newPtr->getData()->stats; | |
756 | switch(p->level) | |
757 | { | |
758 | case 1: | |
759 | if ((time(NULL) - p->lastlogin) / 86400 >= level1expire) | |
760 | { | |
761 | logout(newPtr->getData()); | |
762 | players[x].remove(newPtr->getData()); | |
763 | return; | |
764 | } | |
765 | break; | |
766 | ||
767 | default: | |
768 | if ((time(NULL) - p->lastlogin) / 86400 >= defaultexpire) | |
769 | { | |
770 | logout(newPtr->getData()); | |
771 | players[x].remove(newPtr->getData()); | |
772 | return; | |
773 | } | |
774 | break; | |
775 | } | |
776 | if (timedOut(p)) | |
777 | { | |
778 | timeOutEvent(p); | |
779 | } | |
780 | } | |
781 | } | |
782 | } | |
783 | ||
784 | aClient *findbynick(const char *nick) | |
785 | { | |
786 | ListNode <aClient> *newPtr; | |
787 | #ifdef P10 | |
788 | unsigned long hv = sHASH((unsigned char *) nick); | |
789 | #else | |
790 | unsigned long hv = iHASH((unsigned char *) nick); | |
791 | #endif | |
792 | ||
793 | newPtr = clients[hv].First(); | |
794 | ||
795 | aClient *client = NULL; | |
796 | ||
797 | while (newPtr) | |
798 | { | |
799 | client = newPtr->getData(); | |
800 | #ifdef P10 | |
801 | if (strcmp(client->getNick(), nick) == 0) | |
802 | #else | |
803 | if (stricmp(client->getNick(), nick) == 0) | |
804 | #endif | |
805 | return client; | |
806 | client = NULL; | |
807 | newPtr = newPtr->Next(); | |
808 | } | |
809 | return client; | |
810 | } | |
811 | ||
812 | /* daemon() - detach process from user and disappear into the background | |
813 | * returns -1 on failure, but you can't do much except exit in that case | |
814 | * since we may already have forked. This is based on the BSD version, | |
815 | * so the caller is responsible for things like the umask, etc. | |
816 | */ | |
817 | ||
818 | /* believed to work on all Posix systems */ | |
819 | ||
820 | int daemon(int nochdir, int noclose) | |
821 | { | |
822 | pid_t pid; | |
823 | switch (pid = fork()) | |
824 | { | |
825 | case 0: break; | |
826 | case -1: return -1; | |
827 | default: _exit(0); /* exit the original process */ | |
828 | } | |
829 | ||
830 | if (setsid() < 0) /* shoudn't fail */ | |
831 | return -1; | |
832 | ||
833 | /* dyke out this switch if you want to acquire a control tty in */ | |
834 | /* the future -- not normally advisable for daemons */ | |
835 | ||
836 | switch (pid = fork()) | |
837 | { | |
838 | case 0: break; | |
839 | case -1: return -1; | |
840 | default: | |
841 | ofstream outfile; | |
842 | outfile.open(pidfile); | |
843 | if (outfile.fail()) | |
844 | cerr << "Unable to open " << pidfile << endl; | |
845 | outfile << pid << endl; | |
846 | outfile.close(); | |
847 | ||
848 | _exit(0); | |
849 | } | |
850 | ||
851 | if (!nochdir) | |
852 | chdir("/"); | |
853 | ||
854 | if (!noclose) | |
855 | { | |
856 | closeall(0); | |
857 | open("/dev/null",O_RDWR); | |
858 | dup(0); dup(0); | |
859 | } | |
860 | ||
861 | return 0; | |
862 | } | |
863 | ||
864 | ||
865 | /* closeall() -- close all FDs >= a specified value */ | |
866 | ||
867 | void closeall(int fd) | |
868 | { | |
869 | int fdlimit = sysconf(_SC_OPEN_MAX); | |
870 | ||
871 | while (fd < fdlimit) | |
872 | close(fd++); | |
873 | } | |
874 | ||
875 | void prettyIntro() | |
876 | { | |
877 | cout << endl; | |
878 | cout << " GGGG AAA MM MM EEEEEEE SSSSS EEEEEEE RRRRRR VV VV " << endl; | |
879 | cout << " GG GG AAAAA MMM MMM EE SS EE RR RR VV VV " << endl; | |
880 | cout << "GG AA AA MM MM MM EEEEE SSSSS EEEEE RRRRRR VV VV " << endl; | |
881 | cout << "GG GGG AAAAAAA MM MM EE SS EE RR RR VV VV " << endl; | |
882 | cout << "G G AA AA MM MM EEEEEEE SSSSS EEEEEEE RR RR VVV" << endl; | |
883 | cout << " GGGGG V\n\n" << endl; | |
884 | cout << "Version: " << VERSION << endl; | |
885 | cout << "http://www.gameserv.us - http://www.sourceforge.net/projects/gameservirc" << endl; | |
886 | } | |
887 | ||
888 | void load_lastrefresh() | |
889 | { | |
890 | ifstream infile; | |
891 | infile.open(".gsrefresh"); | |
892 | if (infile.fail()) | |
893 | { | |
894 | #ifdef DEBUGMODE | |
895 | log("Error opening .gsrefresh"); | |
896 | #endif | |
897 | ||
898 | generate: | |
899 | long mytime = time(NULL); | |
900 | #ifdef DEBUGMODE | |
901 | log("Generating new refresh time"); | |
902 | #endif | |
903 | ||
904 | // Just a safety measure... tho no one should | |
905 | // get anywhere near the actual time as their refreshperiod | |
906 | if (refreshperiod >= mytime) | |
907 | { | |
908 | log("Refresh period is greater than or equal to the actual time... setting it to 86400"); | |
909 | refreshperiod = 86400; | |
910 | } | |
911 | ||
912 | lastrefresh = mytime - (mytime % refreshperiod); | |
913 | ||
914 | refreshall(); | |
915 | save_lastrefresh(); | |
916 | return; | |
917 | } | |
918 | infile >> lastrefresh; | |
919 | ||
920 | infile.close(); | |
921 | if (lastrefresh < 0) | |
922 | goto generate; | |
923 | } | |
924 | ||
925 | void load_lastrollover() | |
926 | { | |
927 | ifstream infile; | |
928 | infile.open(".gsrollover"); | |
929 | if (infile.fail()) | |
930 | { | |
931 | #ifdef DEBUGMODE | |
932 | log("Error opening .gsrollover"); | |
933 | #endif | |
934 | ||
935 | generate: | |
936 | long mytime = time(NULL); | |
937 | #ifdef DEBUGMODE | |
938 | log("Generating new rollover time"); | |
939 | #endif | |
940 | lastrollover = mytime; | |
941 | return; | |
942 | } | |
943 | infile >> lastrollover; | |
944 | ||
945 | infile.close(); | |
946 | if (lastrollover < 0) | |
947 | goto generate; | |
948 | } | |
949 | ||
950 | void save_lastrefresh() | |
951 | { | |
952 | ofstream outfile; | |
953 | ||
954 | outfile.open(".gsrefresh"); | |
955 | ||
956 | if (outfile.fail()) | |
957 | { | |
958 | log("Error creating new file .gsrefresh"); | |
959 | return; | |
960 | } | |
961 | outfile << lastrefresh << endl << lastrollover; | |
962 | ||
963 | outfile.close(); | |
964 | } | |
965 | ||
966 | void save_lastrollover() | |
967 | { | |
968 | ofstream outfile; | |
969 | ||
970 | outfile.open(".gsrollover"); | |
971 | ||
972 | if (outfile.fail()) | |
973 | { | |
974 | log("Error creating new file .gsrollover"); | |
975 | return; | |
976 | } | |
977 | outfile << lastrollover << endl; | |
978 | outfile.close(); | |
979 | } |