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