]> jfr.im git - irc/gameservirc.git/blame - gameserv/tcpclient.cpp
Finally got level.h/cpp compiling into the project... beginning to store
[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 "\
a6c68981 277 "and resetting news!");
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);
10e38380 286
922daad7 287 save_gs_dbase();
5aa1d28d 288 saveNews(newsdata, todaysnews);
10e38380 289 // Send notice to the channel of the update
290 notice(s_GameServ, c_Forest, "%S player data saved");
922daad7 291 }
292
e1c41a84 293
294 #if !defined(P10)
85ce9d3e 295 if (stricmp(cmd, "PING") == 0) {
0a1518fa 296 char *timestamp;
297 timestamp = strtok(NULL, "");
298 raw("PONG %s", timestamp);
e1c41a84 299 #else
300 if (stricmp(cmd, "G") == 0) {
301 char *timestamp;
302 timestamp = strtok(NULL, " ");
303 raw("[] Z [] %s 0 %s", timestamp + 1, timestamp);
304 #endif
fc9d2643 305 #ifdef P10
306 } else if (stricmp(cmd, "EB") == 0) {
307 raw("[] EA");
308 #endif
0501fe18 309 } else if (stricmp(cmd, "VERSION") == 0) {
310 char *server;
311 server = strtok(NULL, " ");
312 server++;
2e9db9c4 313 raw(":%s 351 %s %s_%s. %s", servername, source+1, PACKAGE, VERSION, servername);
ba2a880f 314 #if !defined(P10)
85ce9d3e 315 } else if (strncmp(cmd, "NICK", 4) == 0) {
316 if (buffer[0] == ':')
317 {
318 aClient *tempPtr;
28f552b8 319 if ((tempPtr = find((source + 1))))
85ce9d3e 320 {
321 char *nick;
59dc3990 322 unsigned long oldhv, newhv;
85ce9d3e 323 nick = strtok(NULL, " ");
448a1531 324 oldhv = iHASH((unsigned char *) tempPtr->getNick());
325 newhv = iHASH((unsigned char *) nick);
85ce9d3e 326 tempPtr->setNick(nick);
59dc3990 327 clients[oldhv].remove(tempPtr);
328 clients[newhv].insertAtBack(tempPtr);
85ce9d3e 329 }
330 }
331 else
332 {
333 char *nick;
ba2a880f 334 #else
335 } else if (stricmp(cmd, "N") == 0 && strlen(source) == 2) {
336 {
ce61cdfa 337 char *nick, *realnick;
338 realnick = strtok(NULL, " ");
ba2a880f 339
ce61cdfa 340 for (int x = 0; x < 5; x++)
ba2a880f 341 nick = strtok(NULL, " ");
fc9d2643 342
ba2a880f 343 if (nick[0] == '+')
fc9d2643 344 {
345 #ifdef DEBUGMODE
346 log ("aClient has modes");
347 #endif
348
349 // Searching for the +r mode (extra parameter)
350 for (unsigned int count = 1; count < strlen(nick); count++)
351 {
352 if (nick[count] == 'r')
353 {
354 nick = strtok(NULL, " ");
355 break;
356 }
357 }
358 nick = strtok(NULL, " ");
359 }
ba2a880f 360 #endif
448a1531 361 aClient *newuser, *temp;
ba2a880f 362
85ce9d3e 363 nick = strtok(NULL, " ");
ba2a880f 364
ce61cdfa 365 #ifdef P10
366 newuser = new aClient(nick, realnick);
367 #else
368 newuser = new aClient(nick);
369 #endif
370
371
5963944b 372 if (loaded)
18b84d11 373
374 if (isWelcome())
375 {
376 #ifdef P10
377 notice(s_GameServ, nick, welcomemsg, realnick);
378 #else
379 notice(s_GameServ, nick, welcomemsg, nick);
380 #endif
381 }
448a1531 382 #ifdef P10
c8117c0f 383 unsigned long hv = sHASH((unsigned char *) nick);
448a1531 384 #else
385 unsigned long hv = iHASH((unsigned char *) nick);
386 #endif
387
388 temp = clients[hv].insertAtBack(newuser);
389
7cc338f6 390 #if defined(HYBRID) || defined(BAHAMUT) || defined(ULTIMATE2)
448a1531 391 char *nickserver;
392 strtok(NULL, " ");
393 strtok(NULL, " ");
394 nickserver = strtok(NULL, " ");
395 if (nickserver[0] == '+')
396 strtok(NULL, " ");
397 strtok(NULL, " ");
903cd861 398 nickserver = strtok(NULL, " ");
399 for (int x = 0; x < 32; x++)
400 {
401 if (stricmp(ignoreservers[x], nickserver) == 0)
402 {
403 setIgnore(temp);
404 break;
405 }
406 }
407 #elif defined(UNREAL)
408 char *nickserver;
409 strtok(NULL, " ");
410 strtok(NULL, " ");
411 strtok(NULL, " ");
412 strtok(NULL, " ");
448a1531 413 nickserver = strtok(NULL, " ");
414 for (int x = 0; x < 32; x++)
415 {
416 if (stricmp(ignoreservers[x], nickserver) == 0)
417 {
418 setIgnore(temp);
419 break;
420 }
421 }
422 #endif
85ce9d3e 423 delete newuser;
424 }
ba2a880f 425 #if defined(P10)
426 } else if (stricmp(cmd, "Q") == 0) {
0b259dff 427// unsigned long hv = sHASH((unsigned char *) source);
ba2a880f 428 #else
85ce9d3e 429 } else if (stricmp(cmd, "QUIT") == 0) {
0b259dff 430// unsigned long hv = iHASH((unsigned char *) source);
ba2a880f 431 #endif
85ce9d3e 432 aClient *quitter;
fc9d2643 433 char z = source[0];
434
435 if (z == ':')
436 source++;
437
3f243b0b 438 if (!(quitter = find(source)))
439 {
440 log("Fatal Error: could not find %s in the "\
441 "clients list", source);
442 goto end;
443 }
444
445 logout(quitter);
446
447 if (z == ':')
448 source--;
449
450 /* Attempting to use the logout() function
fc9d2643 451 if ((quitter = find(source)))
7996e5fd 452 clients[hv].remove(quitter);
fc9d2643 453 if ((quitter = findIRCplayer(source)))
ee38284f 454 {
7f17db99 455 if (player_fight(quitter))
456 {
457 // Stop the fight on the other client
458 aClient *otherplayer = quitter->stats->battle;
459 otherplayer->stats->battle = NULL;
460 notice(s_GameServ, otherplayer->getNick(), "%s "\
461 "has quit IRC. The fight stops here.",
462 quitter->stats->name);
463 }
464 quitter->stats->battle = NULL;
465 quitter->stats->fight = NULL;
466 quitter->stats->master = NULL;
467
85bcf836 468 quitter->setNick("Not Playing");
a5316c52 469 #ifdef P10
85bcf836 470 quitter->setRealNick("Not Playing");
a5316c52 471 #endif
85bcf836 472 quitter->stats->client = NULL; // Unidentify them
ee38284f 473 }
3f243b0b 474 */
fc9d2643 475
ba2a880f 476 #if defined(P10)
477 } else if (stricmp(cmd, "P") == 0) {
478 char *rest, *dest;
479 char *longname;
480 longname = new char[strlen(s_GameServ) + strlen(servername) + 2];
481
482 sprintf(longname, "%S@%s", servername);
85ce9d3e 483
ba2a880f 484 dest = strtok(NULL, " ");
485 rest = strtok(NULL, "");
486 if (stricmp(dest, gsnum) == 0 || stricmp(dest, longname) == 0)
487 {
488 delete [] longname;
489 gameserv(source, rest);
490 }
6f727d4c 491 else if (stricmp(dest, c_Forest) == 0 && isListenOnCF())
ba2a880f 492 {
493 delete [] longname;
494 forest(source, rest);
495 }
496 #else
85ce9d3e 497 } else if (stricmp(cmd, "PRIVMSG") == 0) {
498 char *rest, *dest;
499 dest = strtok(NULL, " ");
500 rest = strtok(NULL, "");
ad7dfaa0 501 if (strnicmp(dest, s_GameServ, strlen(s_GameServ)) == 0)
85ce9d3e 502 gameserv(source, rest);
1546c3da 503 else if (stricmp(dest, c_Forest) == 0 && isListenOnCF())
85ce9d3e 504 forest(source, rest);
ba2a880f 505 #endif
f2072f1a 506 #if defined(P10)
507 } else if (stricmp(cmd, "J") == 0) {
508 #else
85ce9d3e 509 } else if (stricmp(cmd, "JOIN") == 0) {
f2072f1a 510 #endif
85ce9d3e 511 char *channel;
f2072f1a 512 aClient *joiner;
85ce9d3e 513 channel = strtok(NULL, " ");
f2072f1a 514
515 char z = source[0];
516
517 if (z == ':')
518 source++;
519
520 joiner = find(source);
521
522 if (stricmp(channel, c_Forest) == 0 && is_playing(joiner))
523 {
524 #ifdef DEBUGMODE
525 log("Player %s (IRC: %s) joined %s",
526 joiner->stats->name,
527 #ifdef P10
528 joiner->getRealNick(),
529 #else
530 joiner->getNick(),
531 #endif
532 c_Forest);
533 #endif
534 raw(":%S MODE %s +v %s", c_Forest, (source));
535 }
536
537 if (z == ':')
538 source--;
c7340cbd 539
540 #if defined(BAHAMUT)
541 } else if (stricmp(cmd, "SJOIN") == 0) {
581ec09e 542 char *channel, *nick, *tmp, *rest;
c7340cbd 543 strtok(NULL, " "); // Ignore the TS
581ec09e 544#ifndef HYBRID
c7340cbd 545 strtok(NULL, " "); // Ignore the TS
581ec09e 546#endif
c7340cbd 547 channel = strtok(NULL, " ");
581ec09e 548 rest = strtok(NULL, "");
549 tmp = strchr(rest, ':');
550 tmp++;
551 nick = strtok(tmp, " ");
552 while (nick != NULL)
553 {
554 if (*nick == '@')
555 nick++;
556 if (*nick == '+')
557 nick++; // Assume for users set op and voice, they
558 // are never passed as +@nick
559 if (stricmp(channel, c_Forest) == 0 && is_playing(nick))
560 raw(":%S MODE %s +v %s", channel, nick);
561
562 nick = strtok(NULL, " ");
563 }
564#endif
85ce9d3e 565 } else {
9f8c2acc 566 #ifdef DEBUGMODE
567 log("Unrecognized Message: cmd = %s source = %s", cmd, source);
568 #endif
85ce9d3e 569 }
570 }
4dde2ed9 571
05c527e6 572 } // for loop for connection retry
573
4dde2ed9 574 end:
575
c8ada07e 576 save_gs_dbase();
5aa1d28d 577 saveNews(newsdata, todaysnews);
4dde2ed9 578
c8ada07e 579 delete_monsters();
580 delete_masters();
581
9f8c2acc 582 #ifdef DEBUGMODE
583 log("<CLOSED>");
584 #endif
585
85ce9d3e 586 close(sock);
587 unload_config_file();
588 return 0;
589}
590
591aClient *find(char *nick)
592{
593 return findbynick(nick);
594}
595
596aClient *find(const char *nick)
597{
598 return findbynick(nick);
599}
600
ce61cdfa 601#ifdef P10
602
603aClient *findbyrealnick(char *realnick)
604{
605 ListNode <aClient> *newPtr;
448a1531 606 unsigned long hv = sHASH((unsigned char *) realnick);
7996e5fd 607 newPtr = clients[hv].First();
ce61cdfa 608
609 aClient *client = NULL;
610
611 while (newPtr)
612 {
613 client = newPtr->getData();
614 if (stricmp(client->getRealNick(), realnick) == 0)
615 return client;
616 client = NULL;
617 newPtr = newPtr->Next();
618 }
619 return client;
620}
4e5760fd 621
622#else
623
624aClient *findbyrealnick(char *realnick)
625{
626 return findbynick(realnick);
627}
628
ce61cdfa 629#endif
85ce9d3e 630
631aClient *findbynick(char *nick)
632{
633 ListNode <aClient> *newPtr;
448a1531 634 #ifdef P10
635 unsigned long hv = sHASH((unsigned char *) nick);
636 #else
637 unsigned long hv = iHASH((unsigned char *) nick);
638 #endif
639
7996e5fd 640 newPtr = clients[hv].First();
85ce9d3e 641
642 aClient *client = NULL;
643
644 while (newPtr)
645 {
646 client = newPtr->getData();
23ec3ff4 647 #ifdef P10
23ec3ff4 648 if (strcmp(client->getNick(), nick) == 0)
649 #else
650 if (stricmp(client->getNick(), nick) == 0)
651 #endif
85ce9d3e 652 return client;
653 client = NULL;
654 newPtr = newPtr->Next();
655 }
656 return client;
657}
658
ee38284f 659aClient *findIRCplayer(const char *nick)
660{
661 ListNode <aClient> *newPtr;
662 aClient *p = NULL;
663
448a1531 664 p = find(nick);
665
666 if (!is_playing(p))
667 return NULL;
668
669 unsigned long hv = iHASH((unsigned char *) p->stats->name);
670
7996e5fd 671 for (newPtr = players[hv].First(); newPtr; newPtr = newPtr->Next())
ee38284f 672 {
673 p = newPtr->getData();
23ec3ff4 674 #ifdef P10
675 if (strcmp(p->getNick(), nick) == 0)
676 #else
677 if (stricmp(p->getNick(), nick) == 0)
678 #endif
ee38284f 679 return p;
680 p = NULL;
681 }
682 return NULL;
683}
40251952 684
0a1518fa 685aClient *findplayer(const char *name)
686{
687 ListNode <aClient> *newPtr;
688 Player *p = NULL;
448a1531 689 unsigned long hv = iHASH((unsigned char *) name);
7996e5fd 690 for (newPtr = players[hv].First(); newPtr; newPtr = newPtr->Next())
0a1518fa 691 {
692 p = newPtr->getData()->stats;
693 if (stricmp(p->name, name) == 0)
694 return newPtr->getData();
695 p = NULL;
696 }
697 return NULL;
698}
699
40251952 700void check_idles()
701{
702 ListNode <aClient> *newPtr;
703 Player *p = NULL;
704
705 for (int x = 0; x < U_TABLE_SIZE; x++)
706 {
707 for (newPtr = players[x].First(); newPtr; newPtr = newPtr->Next())
708 {
709 p = newPtr->getData()->stats;
14e24ba1 710 switch(p->level)
711 {
712 case 1:
713 if ((time(NULL) - p->lastlogin) / 86400 >= level1expire)
714 {
152a0a93 715 logout(newPtr->getData());
14e24ba1 716 players[x].remove(newPtr->getData());
717 return;
718 }
719 break;
720
721 default:
722 if ((time(NULL) - p->lastlogin) / 86400 >= defaultexpire)
723 {
152a0a93 724 logout(newPtr->getData());
14e24ba1 725 players[x].remove(newPtr->getData());
726 return;
727 }
728 break;
729 }
40251952 730 if (timedOut(p))
731 {
732 timeOutEvent(p);
733 }
734 }
735 }
736}
737
85ce9d3e 738aClient *findbynick(const char *nick)
739{
740 ListNode <aClient> *newPtr;
448a1531 741 #ifdef P10
742 unsigned long hv = sHASH((unsigned char *) nick);
743 #else
744 unsigned long hv = iHASH((unsigned char *) nick);
745 #endif
746
7996e5fd 747 newPtr = clients[hv].First();
85ce9d3e 748
749 aClient *client = NULL;
750
751 while (newPtr)
752 {
753 client = newPtr->getData();
23ec3ff4 754 #ifdef P10
755 if (strcmp(client->getNick(), nick) == 0)
756 #else
757 if (stricmp(client->getNick(), nick) == 0)
758 #endif
85ce9d3e 759 return client;
760 client = NULL;
761 newPtr = newPtr->Next();
762 }
763 return client;
764}
765
ce61cdfa 766/* daemon() - detach process from user and disappear into the background
767 * returns -1 on failure, but you can't do much except exit in that case
768 * since we may already have forked. This is based on the BSD version,
769 * so the caller is responsible for things like the umask, etc.
770 */
771
772/* believed to work on all Posix systems */
773
774int daemon(int nochdir, int noclose)
775{
776 pid_t pid;
777 switch (pid = fork())
778 {
779 case 0: break;
780 case -1: return -1;
781 default: _exit(0); /* exit the original process */
782 }
783
784 if (setsid() < 0) /* shoudn't fail */
785 return -1;
786
787 /* dyke out this switch if you want to acquire a control tty in */
788 /* the future -- not normally advisable for daemons */
789
790 switch (pid = fork())
791 {
792 case 0: break;
793 case -1: return -1;
794 default:
795 ofstream outfile;
69ae096c 796 outfile.open(pidfile);
ce61cdfa 797 if (outfile.fail())
69ae096c 798 cerr << "Unable to open " << pidfile << endl;
ce61cdfa 799 outfile << pid << endl;
800 outfile.close();
801
802 _exit(0);
803 }
804
805 if (!nochdir)
806 chdir("/");
807
808 if (!noclose)
809 {
810 closeall(0);
811 open("/dev/null",O_RDWR);
812 dup(0); dup(0);
813 }
814
815 return 0;
816}
817
818
819/* closeall() -- close all FDs >= a specified value */
820
821void closeall(int fd)
822{
823 int fdlimit = sysconf(_SC_OPEN_MAX);
824
825 while (fd < fdlimit)
826 close(fd++);
827}
828
15838737 829void prettyIntro()
830{
831cout << endl;
832cout << " GGGG AAA MM MM EEEEEEE SSSSS EEEEEEE RRRRRR VV VV " << endl;
833cout << " GG GG AAAAA MMM MMM EE SS EE RR RR VV VV " << endl;
834cout << "GG AA AA MM MM MM EEEEE SSSSS EEEEE RRRRRR VV VV " << endl;
835cout << "GG GGG AAAAAAA MM MM EE SS EE RR RR VV VV " << endl;
836cout << "G G AA AA MM MM EEEEEEE SSSSS EEEEEEE RR RR VVV" << endl;
837cout << " GGGGG V\n\n" << endl;
838cout << "Version: " << VERSION << endl;
839}
2edcd222 840
841void load_lastrefresh()
842{
843 ifstream infile;
844 infile.open(".gsrefresh");
845 if (infile.fail())
846 {
847 #ifdef DEBUGMODE
848 log("Error opening .gsrefresh");
849 #endif
850
851 generate:
14e24ba1 852 long mytime = time(NULL);
2edcd222 853 #ifdef DEBUGMODE
854 log("Generating new refresh time");
855 #endif
14e24ba1 856
857 // Just a safety measure... tho no one should
858 // get anywhere near the time as their refreshperiod
859 if (refreshperiod >= mytime)
860 refreshperiod = 86400;
861
862 lastrefresh = mytime - (mytime % refreshperiod);
2edcd222 863 refreshall();
864 save_lastrefresh();
865 return;
866 }
867 infile >> lastrefresh;
868 infile.close();
869 if (lastrefresh < 0)
870 goto generate;
871}
872
873void save_lastrefresh()
874{
875 ofstream outfile;
876
877 outfile.open(".gsrefresh");
878
879 if (outfile.fail())
880 {
881 log("Error creating new file .gsrefresh");
882 return;
883 }
884 outfile << lastrefresh << endl;
885
886 outfile.close();
887}