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