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