]> jfr.im git - irc/gameservirc.git/blame - gameserv/tcpclient.cpp
Implemented refresh period... now works and added to changelog
[irc/gameservirc.git] / gameserv / tcpclient.cpp
CommitLineData
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 32using std::ofstream;
33using std::ifstream;
ce61cdfa 34using std::cerr;
35using std::endl;
fb37ecc7 36
91c0b563 37char *PACKAGE = "GameServ";
4f7dd884 38char *VERSION = "1.2.3 +devel";
173302fe 39
85ce9d3e 40int sock;
2edcd222 41long lastrefresh;
44ea29f7 42
7996e5fd 43List<aClient> clients[U_TABLE_SIZE];
85ce9d3e 44
2edcd222 45void save_lastrefresh();
46void load_lastrefresh();
15838737 47void prettyIntro();
40251952 48void check_idles();
44ea29f7 49
ce61cdfa 50// Make this a daemon
51int daemon(int nochdir, int noclose);
52
53// Close all file descriptors from >= fd
54void closeall(int fd);
55
624c0352 56int 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
100 if (daemon(1,0) < 0)
101 {
102 perror("Could not turn into a daemon");
448a1531 103 exit(3);
ce61cdfa 104 }
666feac6 105
05c527e6 106 init_masters();
107 load_gs_dbase();
5aa1d28d 108 loadNews(newsdata, todaysnews);
05c527e6 109
110 if (load_monsters() == false)
111 goto end;
112
448a1531 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 }
05c527e6 124
125 strcpy(boss.name, "Red Dragon");
126 strcpy(boss.weapon, "Breath of Unholy Fire");
68379f96 127 boss.strength = 2500;
05c527e6 128 boss.gold = 2000000000;
129 boss.exp = 2000000000;
6d053d90 130 boss.maxhp = 6667;
131 boss.hp = 6667;
05c527e6 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
448a1531 136
05c527e6 137 // This loop will retry the connection 3 times
138 for (int retry = 0; retry < 3 && !shuttingdown; retry++)
139 {
0b6098d5 140 connected = 1;
2edcd222 141 load_lastrefresh();
05c527e6 142
448a1531 143
0b6098d5 144 long int loadtime = time(NULL);
145 long int currentTime;
146 long int oldTime = loadtime;
40251952 147
0b6098d5 148 lastidlecheck = loadtime;
05c527e6 149
0b6098d5 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");
85ce9d3e 159 unload_config_file();
160 return -1;
161 }
448a1531 162 log("%S socket connected.");
85ce9d3e 163
c7340cbd 164#ifdef UNREAL
85ce9d3e 165 raw("PROTOCTL NICKv2 VHP");
166 raw("PASS :%s", remotepass);
c7340cbd 167 raw("SERVER %s 1 :%s", servername, servername);
6f727d4c 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);
85ce9d3e 170 raw(":%S JOIN %s", c_Forest);
03a4bdbb 171 raw(":%S MODE %s +tn", c_Forest);
c7340cbd 172#elif defined(BAHAMUT)
173 raw("PASS %s :TS", remotepass);
174 raw("SERVER %s 1 :%s", servername, servername);
6f727d4c 175 raw("NICK %S 1 %d +w%s %s %s %s 0 :GameServ", time(NULL), (isBOper() ? "o" : ""),
176 gsident, gshost, servername);
03a4bdbb 177 raw(":%s SJOIN %d %d %s +nt :@%S", servername, time(NULL), time(NULL), c_Forest);
581ec09e 178#elif defined(HYBRID)
179 raw("PASS %s :TS", remotepass);
180 raw("SERVER %s 1 :%s", servername, servername);
6f727d4c 181 raw("NICK %S 1 %d +w%s %s %s %s :GameServ", time(NULL), (isBOper() ? "o" : ""),
182 gsident, gshost, servername);
448a1531 183 raw(":%s SJOIN %ld %s +nt :@%S", servername, time(NULL), c_Forest);
7cc338f6 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);
e1c41a84 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);
6f727d4c 196 raw("[] N %S 1 %d %s %s %s DAqAoB %s :%S", time(NULL), gsident, gshost,
c8117c0f 197 (isBOper() ? "+o" : ""), gsnum);
03a4bdbb 198 raw("[] B %s %d +tn %s:o", c_Forest, time(NULL) - 864000, gsnum);
c7340cbd 199#endif
e1c41a84 200
201#if defined(P10)
202 raw("%s T %s :%s", gsnum, c_Forest, c_ForestTopic);
fc9d2643 203 raw("[] EB"); // End burst
581ec09e 204#else
7cc338f6 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
c7340cbd 213 raw(":%S TOPIC %s :%s", c_Forest, c_ForestTopic);
e1c41a84 214#endif
85ce9d3e 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
9f8c2acc 221 #ifdef DEBUGMODE
222 log("Server: %s",buffer);
223 #endif
224
85ce9d3e 225 while (connected) {
226 if (sock_gets(sock,buffer,sizeof(buffer)) == -1) {
227 connected = 0;
228 }
229 strcpy(buf, buffer);
230
e1c41a84 231 #if !defined(P10)
85ce9d3e 232 if (buffer[0] == ':')
233 {
234 source = strtok(buf, " ");
235 cmd = strtok(NULL, " ");
236 }
237 else
238 cmd = strtok(buf, " ");
e1c41a84 239 #else
240 source = strtok(buf, " ");
241 cmd = strtok(NULL, " ");
242 #endif
85ce9d3e 243
9f8c2acc 244 #ifdef DEBUGMODE
245 log("Server: %s", buffer);
246 #endif
5963944b 247
bf2cabcd 248 // Wait N seconds then we're loaded.
5963944b 249 if (!loaded)
250 {
922daad7 251 if (time(NULL) >= welcomedelay + loadtime)
05c527e6 252 {
5963944b 253 loaded = true;
05c527e6 254 retry = 0; // Start the reconnection cycle over
255 }
5963944b 256 }
40251952 257 else
258 {
259 long TIME = time(NULL);
260 if (TIME - lastidlecheck >= idlecheckperiod)
261 {
262 check_idles();
263 lastidlecheck = TIME;
264 }
265 }
5963944b 266
2edcd222 267 // Refresh players and clear news if the time is up
0b9f4972 268 currentTime = time(NULL);
269 if (currentTime - lastrefresh >= refreshperiod)
2edcd222 270 {
271 refreshall();
272 clearNews(todaysnews);
273 saveNews(newsdata, todaysnews);
0b9f4972 274 lastrefresh = currentTime;
2edcd222 275 save_lastrefresh();
0b9f4972 276 notice(s_GameServ, c_Forest, "Refreshing all players "\
277 "and resetting news! %ld", refreshperiod);
2edcd222 278 }
279
922daad7 280 // Save the player data every updateperiod seconds
281 currentTime = time(NULL);
282 if (currentTime - oldTime >= updateperiod)
283 {
284 oldTime = currentTime;
0b6098d5 285 log("Saving to %s", playerdata);
922daad7 286 save_gs_dbase();
5aa1d28d 287 saveNews(newsdata, todaysnews);
922daad7 288 }
289
e1c41a84 290
291 #if !defined(P10)
85ce9d3e 292 if (stricmp(cmd, "PING") == 0) {
0a1518fa 293 char *timestamp;
294 timestamp = strtok(NULL, "");
295 raw("PONG %s", timestamp);
e1c41a84 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
fc9d2643 302 #ifdef P10
303 } else if (stricmp(cmd, "EB") == 0) {
304 raw("[] EA");
305 #endif
0501fe18 306 } else if (stricmp(cmd, "VERSION") == 0) {
307 char *server;
308 server = strtok(NULL, " ");
309 server++;
2e9db9c4 310 raw(":%s 351 %s %s_%s. %s", servername, source+1, PACKAGE, VERSION, servername);
ba2a880f 311 #if !defined(P10)
85ce9d3e 312 } else if (strncmp(cmd, "NICK", 4) == 0) {
313 if (buffer[0] == ':')
314 {
315 aClient *tempPtr;
28f552b8 316 if ((tempPtr = find((source + 1))))
85ce9d3e 317 {
318 char *nick;
59dc3990 319 unsigned long oldhv, newhv;
85ce9d3e 320 nick = strtok(NULL, " ");
448a1531 321 oldhv = iHASH((unsigned char *) tempPtr->getNick());
322 newhv = iHASH((unsigned char *) nick);
85ce9d3e 323 tempPtr->setNick(nick);
59dc3990 324 clients[oldhv].remove(tempPtr);
325 clients[newhv].insertAtBack(tempPtr);
85ce9d3e 326 }
327 }
328 else
329 {
330 char *nick;
ba2a880f 331 #else
332 } else if (stricmp(cmd, "N") == 0 && strlen(source) == 2) {
333 {
ce61cdfa 334 char *nick, *realnick;
335 realnick = strtok(NULL, " ");
ba2a880f 336
ce61cdfa 337 for (int x = 0; x < 5; x++)
ba2a880f 338 nick = strtok(NULL, " ");
fc9d2643 339
ba2a880f 340 if (nick[0] == '+')
fc9d2643 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 }
ba2a880f 357 #endif
448a1531 358 aClient *newuser, *temp;
ba2a880f 359
85ce9d3e 360 nick = strtok(NULL, " ");
ba2a880f 361
ce61cdfa 362 #ifdef P10
363 newuser = new aClient(nick, realnick);
364 #else
365 newuser = new aClient(nick);
366 #endif
367
368
5963944b 369 if (loaded)
18b84d11 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 }
448a1531 379 #ifdef P10
c8117c0f 380 unsigned long hv = sHASH((unsigned char *) nick);
448a1531 381 #else
382 unsigned long hv = iHASH((unsigned char *) nick);
383 #endif
384
385 temp = clients[hv].insertAtBack(newuser);
386
7cc338f6 387 #if defined(HYBRID) || defined(BAHAMUT) || defined(ULTIMATE2)
448a1531 388 char *nickserver;
389 strtok(NULL, " ");
390 strtok(NULL, " ");
391 nickserver = strtok(NULL, " ");
392 if (nickserver[0] == '+')
393 strtok(NULL, " ");
394 strtok(NULL, " ");
903cd861 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, " ");
448a1531 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
85ce9d3e 420 delete newuser;
421 }
ba2a880f 422 #if defined(P10)
423 } else if (stricmp(cmd, "Q") == 0) {
0b259dff 424// unsigned long hv = sHASH((unsigned char *) source);
ba2a880f 425 #else
85ce9d3e 426 } else if (stricmp(cmd, "QUIT") == 0) {
0b259dff 427// unsigned long hv = iHASH((unsigned char *) source);
ba2a880f 428 #endif
85ce9d3e 429 aClient *quitter;
fc9d2643 430 char z = source[0];
431
432 if (z == ':')
433 source++;
434
3f243b0b 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
fc9d2643 448 if ((quitter = find(source)))
7996e5fd 449 clients[hv].remove(quitter);
fc9d2643 450 if ((quitter = findIRCplayer(source)))
ee38284f 451 {
7f17db99 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
85bcf836 465 quitter->setNick("Not Playing");
a5316c52 466 #ifdef P10
85bcf836 467 quitter->setRealNick("Not Playing");
a5316c52 468 #endif
85bcf836 469 quitter->stats->client = NULL; // Unidentify them
ee38284f 470 }
3f243b0b 471 */
fc9d2643 472
ba2a880f 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);
85ce9d3e 480
ba2a880f 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 }
6f727d4c 488 else if (stricmp(dest, c_Forest) == 0 && isListenOnCF())
ba2a880f 489 {
490 delete [] longname;
491 forest(source, rest);
492 }
493 #else
85ce9d3e 494 } else if (stricmp(cmd, "PRIVMSG") == 0) {
495 char *rest, *dest;
496 dest = strtok(NULL, " ");
497 rest = strtok(NULL, "");
ad7dfaa0 498 if (strnicmp(dest, s_GameServ, strlen(s_GameServ)) == 0)
85ce9d3e 499 gameserv(source, rest);
1546c3da 500 else if (stricmp(dest, c_Forest) == 0 && isListenOnCF())
85ce9d3e 501 forest(source, rest);
ba2a880f 502 #endif
f2072f1a 503 #if defined(P10)
504 } else if (stricmp(cmd, "J") == 0) {
505 #else
85ce9d3e 506 } else if (stricmp(cmd, "JOIN") == 0) {
f2072f1a 507 #endif
85ce9d3e 508 char *channel;
f2072f1a 509 aClient *joiner;
85ce9d3e 510 channel = strtok(NULL, " ");
f2072f1a 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--;
c7340cbd 536
537 #if defined(BAHAMUT)
538 } else if (stricmp(cmd, "SJOIN") == 0) {
581ec09e 539 char *channel, *nick, *tmp, *rest;
c7340cbd 540 strtok(NULL, " "); // Ignore the TS
581ec09e 541#ifndef HYBRID
c7340cbd 542 strtok(NULL, " "); // Ignore the TS
581ec09e 543#endif
c7340cbd 544 channel = strtok(NULL, " ");
581ec09e 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
85ce9d3e 562 } else {
9f8c2acc 563 #ifdef DEBUGMODE
564 log("Unrecognized Message: cmd = %s source = %s", cmd, source);
565 #endif
85ce9d3e 566 }
567 }
4dde2ed9 568
05c527e6 569 } // for loop for connection retry
570
4dde2ed9 571 end:
572
c8ada07e 573 save_gs_dbase();
5aa1d28d 574 saveNews(newsdata, todaysnews);
4dde2ed9 575
c8ada07e 576 delete_monsters();
577 delete_masters();
578
9f8c2acc 579 #ifdef DEBUGMODE
580 log("<CLOSED>");
581 #endif
582
85ce9d3e 583 close(sock);
584 unload_config_file();
585 return 0;
586}
587
588aClient *find(char *nick)
589{
590 return findbynick(nick);
591}
592
593aClient *find(const char *nick)
594{
595 return findbynick(nick);
596}
597
ce61cdfa 598#ifdef P10
599
600aClient *findbyrealnick(char *realnick)
601{
602 ListNode <aClient> *newPtr;
448a1531 603 unsigned long hv = sHASH((unsigned char *) realnick);
7996e5fd 604 newPtr = clients[hv].First();
ce61cdfa 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}
4e5760fd 618
619#else
620
621aClient *findbyrealnick(char *realnick)
622{
623 return findbynick(realnick);
624}
625
ce61cdfa 626#endif
85ce9d3e 627
628aClient *findbynick(char *nick)
629{
630 ListNode <aClient> *newPtr;
448a1531 631 #ifdef P10
632 unsigned long hv = sHASH((unsigned char *) nick);
633 #else
634 unsigned long hv = iHASH((unsigned char *) nick);
635 #endif
636
7996e5fd 637 newPtr = clients[hv].First();
85ce9d3e 638
639 aClient *client = NULL;
640
641 while (newPtr)
642 {
643 client = newPtr->getData();
23ec3ff4 644 #ifdef P10
23ec3ff4 645 if (strcmp(client->getNick(), nick) == 0)
646 #else
647 if (stricmp(client->getNick(), nick) == 0)
648 #endif
85ce9d3e 649 return client;
650 client = NULL;
651 newPtr = newPtr->Next();
652 }
653 return client;
654}
655
ee38284f 656aClient *findIRCplayer(const char *nick)
657{
658 ListNode <aClient> *newPtr;
659 aClient *p = NULL;
660
448a1531 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
7996e5fd 668 for (newPtr = players[hv].First(); newPtr; newPtr = newPtr->Next())
ee38284f 669 {
670 p = newPtr->getData();
23ec3ff4 671 #ifdef P10
672 if (strcmp(p->getNick(), nick) == 0)
673 #else
674 if (stricmp(p->getNick(), nick) == 0)
675 #endif
ee38284f 676 return p;
677 p = NULL;
678 }
679 return NULL;
680}
40251952 681
0a1518fa 682aClient *findplayer(const char *name)
683{
684 ListNode <aClient> *newPtr;
685 Player *p = NULL;
448a1531 686 unsigned long hv = iHASH((unsigned char *) name);
7996e5fd 687 for (newPtr = players[hv].First(); newPtr; newPtr = newPtr->Next())
0a1518fa 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
40251952 697void 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 if (timedOut(p))
708 {
709 timeOutEvent(p);
710 }
711 }
712 }
713}
714
85ce9d3e 715aClient *findbynick(const char *nick)
716{
717 ListNode <aClient> *newPtr;
448a1531 718 #ifdef P10
719 unsigned long hv = sHASH((unsigned char *) nick);
720 #else
721 unsigned long hv = iHASH((unsigned char *) nick);
722 #endif
723
7996e5fd 724 newPtr = clients[hv].First();
85ce9d3e 725
726 aClient *client = NULL;
727
728 while (newPtr)
729 {
730 client = newPtr->getData();
23ec3ff4 731 #ifdef P10
732 if (strcmp(client->getNick(), nick) == 0)
733 #else
734 if (stricmp(client->getNick(), nick) == 0)
735 #endif
85ce9d3e 736 return client;
737 client = NULL;
738 newPtr = newPtr->Next();
739 }
740 return client;
741}
742
ce61cdfa 743/* daemon() - detach process from user and disappear into the background
744 * returns -1 on failure, but you can't do much except exit in that case
745 * since we may already have forked. This is based on the BSD version,
746 * so the caller is responsible for things like the umask, etc.
747 */
748
749/* believed to work on all Posix systems */
750
751int daemon(int nochdir, int noclose)
752{
753 pid_t pid;
754 switch (pid = fork())
755 {
756 case 0: break;
757 case -1: return -1;
758 default: _exit(0); /* exit the original process */
759 }
760
761 if (setsid() < 0) /* shoudn't fail */
762 return -1;
763
764 /* dyke out this switch if you want to acquire a control tty in */
765 /* the future -- not normally advisable for daemons */
766
767 switch (pid = fork())
768 {
769 case 0: break;
770 case -1: return -1;
771 default:
772 ofstream outfile;
69ae096c 773 outfile.open(pidfile);
ce61cdfa 774 if (outfile.fail())
69ae096c 775 cerr << "Unable to open " << pidfile << endl;
ce61cdfa 776 outfile << pid << endl;
777 outfile.close();
778
779 _exit(0);
780 }
781
782 if (!nochdir)
783 chdir("/");
784
785 if (!noclose)
786 {
787 closeall(0);
788 open("/dev/null",O_RDWR);
789 dup(0); dup(0);
790 }
791
792 return 0;
793}
794
795
796/* closeall() -- close all FDs >= a specified value */
797
798void closeall(int fd)
799{
800 int fdlimit = sysconf(_SC_OPEN_MAX);
801
802 while (fd < fdlimit)
803 close(fd++);
804}
805
15838737 806void prettyIntro()
807{
808cout << endl;
809cout << " GGGG AAA MM MM EEEEEEE SSSSS EEEEEEE RRRRRR VV VV " << endl;
810cout << " GG GG AAAAA MMM MMM EE SS EE RR RR VV VV " << endl;
811cout << "GG AA AA MM MM MM EEEEE SSSSS EEEEE RRRRRR VV VV " << endl;
812cout << "GG GGG AAAAAAA MM MM EE SS EE RR RR VV VV " << endl;
813cout << "G G AA AA MM MM EEEEEEE SSSSS EEEEEEE RR RR VVV" << endl;
814cout << " GGGGG V\n\n" << endl;
815cout << "Version: " << VERSION << endl;
816}
2edcd222 817
818void load_lastrefresh()
819{
820 ifstream infile;
821 infile.open(".gsrefresh");
822 if (infile.fail())
823 {
824 #ifdef DEBUGMODE
825 log("Error opening .gsrefresh");
826 #endif
827
828 generate:
829 #ifdef DEBUGMODE
830 log("Generating new refresh time");
831 #endif
832 lastrefresh = time(NULL);
833 refreshall();
834 save_lastrefresh();
835 return;
836 }
837 infile >> lastrefresh;
838 infile.close();
839 if (lastrefresh < 0)
840 goto generate;
841}
842
843void save_lastrefresh()
844{
845 ofstream outfile;
846
847 outfile.open(".gsrefresh");
848
849 if (outfile.fail())
850 {
851 log("Error creating new file .gsrefresh");
852 return;
853 }
854 outfile << lastrefresh << endl;
855
856 outfile.close();
857}