]> jfr.im git - irc/gameservirc.git/blob - gameserv/tcpclient.cpp
Fixed a bug with RUN, where if a user was not logged in (playing), the
[irc/gameservirc.git] / gameserv / tcpclient.cpp
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 <stdio.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <fstream>
24 #include <stdlib.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 //#include <sys/types.h>
28 //#include <sys/wait.h>
29 //#include <errno.h>
30
31
32
33 using std::ofstream;
34 using std::ifstream;
35 using std::cerr;
36 using std::endl;
37
38 char *PACKAGE = "GameServ";
39 char *VERSION = "1.1.9";
40
41 int sock;
42 int day;
43
44 List<aClient> clients;
45
46 void save_day();
47 void load_day();
48 void prettyIntro();
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 int connected = 1;
60 char *cmd, *source = NULL, *conf = "gameserv.conf";
61 srand(time(NULL));
62
63 if (argc > 1)
64 {
65 if ( argc > 2 || stricmp(argv[1], "--help") == 0)
66 {
67 cout << "Usage: gameserv [options] [configfile]" << endl;
68 cout << "Options:" << endl;
69 cout << "--help Displays this help dialogue" << endl;
70 return 1;
71 }
72 conf = new char[strlen(argv[1])];
73 strcpy(conf, argv[1]);
74 }
75
76 prettyIntro();
77
78 if (load_config_file(conf))
79 {
80 cout << "Config file loaded ok...\n"
81 << "Turning into a daemon" << endl;
82 }
83 else
84 exit(2);
85
86 // Turn into a daemon
87 if (daemon(1,0) < 0)
88 {
89 perror("Could not turn into a daemon");
90 exit(2);
91 }
92
93 ignore_pipe();
94 sock = make_connection(remoteport, SOCK_STREAM, remoteserver);
95 if (sock == -1) {
96 fprintf(stderr,"make_connection failed.\n");
97 unload_config_file();
98 return -1;
99 }
100
101 #ifdef UNREAL
102 raw("PROTOCTL NICKv2 VHP");
103 raw("PASS :%s", remotepass);
104 raw("SERVER %s 1 :%s", servername, servername);
105 raw("NICK %S 1 %d %S %s %s %d +owghraAxNt %s :%s v%s", time(NULL), gshost,
106 servername, time(NULL), gshost, PACKAGE, VERSION);
107 raw(":%S JOIN %s", c_Forest);
108 raw(":%S MODE %s +mtn", c_Forest);
109 #elif defined(BAHAMUT)
110 raw("PASS %s :TS", remotepass);
111 raw("SERVER %s 1 :%s", servername, servername);
112 raw("NICK %S 1 %d +o %s %s %s 0 :GameServ", time(NULL), gsident, gshost,
113 servername);
114 raw(":%s SJOIN %d %d %s +mnt :@%S", servername, time(NULL), time(NULL), c_Forest);
115 #elif defined(HYBRID)
116 raw("PASS %s :TS", remotepass);
117 raw("SERVER %s 1 :%s", servername, servername);
118 raw("NICK %S 1 %d +o %s %s %s :GameServ", time(NULL), gsident, gshost,
119 servername);
120 // Sending a timestamp of 1 to force ops.
121 raw(":%s SJOIN 1 %s +ntm :@%S", servername, c_Forest);
122 #elif defined(P10)
123 // Server numeric is: [] <-- must be unique
124 raw("PASS :%s", remotepass);
125 raw("SERVER %s 1 %d %d P10 []AAF :%s", servername, time(NULL), time(NULL), servername);
126 raw("[] N %S 1 %d %s %s DAqAoB %s :%S", time(NULL), gsident, gshost, gsnum);
127 raw("[] B %s %d +tnm %s:o", c_Forest, time(NULL) - 864000, gsnum);
128 #endif
129
130 #if defined(P10)
131 raw("%s T %s :%s", gsnum, c_Forest, c_ForestTopic);
132 raw("[] EB"); // End burst
133 #else
134 #ifndef HYBRID
135 raw(":%S MODE %s +o %S", c_Forest);
136 #endif
137 raw(":%S TOPIC %s :%s", c_Forest, c_ForestTopic);
138 #endif
139
140 sock_gets(sock,buffer,sizeof(buffer)-1); /* -1 added thanks to
141 David Duchene <dave@ltd.com> for pointing out the possible
142 buffer overflow resulting from the linefeed added below. */
143
144
145 #ifdef DEBUGMODE
146 log("Server: %s",buffer);
147 #endif
148
149 strcpy(boss.name, "Red Dragon");
150 strcpy(boss.weapon, "Breath of Unholy Fire");
151 boss.strength = 6667;
152 boss.gold = 2000000000;
153 boss.exp = 2000000000;
154 strcpy(boss.death, "You finally snuff out the deadly murderous "\
155 "dragon's dark flames. You have freed the land of its terror "\
156 "filled reign from above!");
157
158 init_masters();
159 load_gs_dbase();
160 load_day();
161 long int loadtime = time(NULL);
162 long int currentTime;
163 long int oldTime = loadtime;
164 bool loaded = false;
165
166 if (load_monsters() == false)
167 goto end;
168
169 while (connected) {
170 if (sock_gets(sock,buffer,sizeof(buffer)) == -1) {
171 connected = 0;
172 }
173 strcpy(buf, buffer);
174
175 #if !defined(P10)
176 if (buffer[0] == ':')
177 {
178 source = strtok(buf, " ");
179 cmd = strtok(NULL, " ");
180 }
181 else
182 cmd = strtok(buf, " ");
183 #else
184 source = strtok(buf, " ");
185 cmd = strtok(NULL, " ");
186 #endif
187
188 #ifdef DEBUGMODE
189 log("Server: %s", buffer);
190 #endif
191
192 // Wait N seconds then we're loaded.
193 if (!loaded)
194 {
195 if (time(NULL) >= welcomedelay + loadtime)
196 loaded = true;
197 }
198
199 // Save the player data every updateperiod seconds
200 currentTime = time(NULL);
201 if (currentTime - oldTime >= updateperiod)
202 {
203 oldTime = currentTime;
204 save_gs_dbase();
205 }
206
207
208 #if !defined(P10)
209 if (stricmp(cmd, "PING") == 0) {
210 char *timestamp;
211 timestamp = strtok(NULL, "");
212 raw("PONG %s", timestamp);
213 #else
214 if (stricmp(cmd, "G") == 0) {
215 char *timestamp;
216 timestamp = strtok(NULL, " ");
217 raw("[] Z [] %s 0 %s", timestamp + 1, timestamp);
218 #endif
219 #ifdef P10
220 } else if (stricmp(cmd, "EB") == 0) {
221 raw("[] EA");
222 #endif
223 } else if (stricmp(cmd, "VERSION") == 0) {
224 char *server;
225 server = strtok(NULL, " ");
226 server++;
227 raw(":%s 351 %s %s_%s. %s", servername, source+1, PACKAGE, VERSION, servername);
228 #if !defined(P10)
229 } else if (strncmp(cmd, "NICK", 4) == 0) {
230 if (buffer[0] == ':')
231 {
232 aClient *tempPtr;
233 if ((tempPtr = find((source + 1))))
234 {
235 char *nick;
236 nick = strtok(NULL, " ");
237 tempPtr->setNick(nick);
238 }
239 }
240 else
241 {
242 char *nick;
243 #else
244 } else if (stricmp(cmd, "N") == 0 && strlen(source) == 2) {
245 {
246 char *nick, *realnick;
247 realnick = strtok(NULL, " ");
248
249 for (int x = 0; x < 5; x++)
250 nick = strtok(NULL, " ");
251
252 if (nick[0] == '+')
253 {
254 #ifdef DEBUGMODE
255 log ("aClient has modes");
256 #endif
257
258 // Searching for the +r mode (extra parameter)
259 for (unsigned int count = 1; count < strlen(nick); count++)
260 {
261 if (nick[count] == 'r')
262 {
263 nick = strtok(NULL, " ");
264 break;
265 }
266 }
267 nick = strtok(NULL, " ");
268 }
269 #endif
270 aClient *newuser;
271
272 nick = strtok(NULL, " ");
273
274 #ifdef P10
275 newuser = new aClient(nick, realnick);
276 #else
277 newuser = new aClient(nick);
278 #endif
279
280
281 if (loaded)
282 #ifdef P10
283 notice(s_GameServ, nick, welcomemsg, realnick);
284 #else
285 notice(s_GameServ, nick, welcomemsg, nick);
286 #endif
287
288 clients.insertAtBack(newuser);
289 delete newuser;
290 }
291 #if defined(P10)
292 } else if (stricmp(cmd, "Q") == 0) {
293 #else
294 } else if (stricmp(cmd, "QUIT") == 0) {
295 #endif
296 aClient *quitter;
297 char z = source[0];
298
299 if (z == ':')
300 source++;
301
302 if ((quitter = find(source)))
303 clients.remove(quitter);
304 if ((quitter = findIRCplayer(source)))
305 {
306 quitter->setNick("!NULL!");
307 quitter->stats->user = NULL; // Unidentify them
308 }
309
310 if (z == ':')
311 source--;
312
313 #if defined(P10)
314 } else if (stricmp(cmd, "P") == 0) {
315 char *rest, *dest;
316 char *longname;
317 longname = new char[strlen(s_GameServ) + strlen(servername) + 2];
318
319 sprintf(longname, "%S@%s", servername);
320
321 dest = strtok(NULL, " ");
322 rest = strtok(NULL, "");
323 if (stricmp(dest, gsnum) == 0 || stricmp(dest, longname) == 0)
324 {
325 delete [] longname;
326 gameserv(source, rest);
327 }
328 else if (stricmp(dest, c_Forest) == 0)
329 {
330 delete [] longname;
331 forest(source, rest);
332 }
333 #else
334 } else if (stricmp(cmd, "PRIVMSG") == 0) {
335 char *rest, *dest;
336 dest = strtok(NULL, " ");
337 rest = strtok(NULL, "");
338 if (strnicmp(dest, s_GameServ, strlen(s_GameServ)) == 0)
339 gameserv(source, rest);
340 else if (stricmp(dest, c_Forest) == 0)
341 forest(source, rest);
342 #endif
343 } else if (stricmp(cmd, "JOIN") == 0) {
344 char *channel;
345 channel = strtok(NULL, " ");
346 if (stricmp(channel, c_Forest) == 0 && is_playing(source + 1))
347 raw(":%S MODE %s +v %s", c_Forest, (source + 1));
348
349 #if defined(BAHAMUT)
350 } else if (stricmp(cmd, "SJOIN") == 0) {
351 char *channel, *nick, *tmp, *rest;
352 strtok(NULL, " "); // Ignore the TS
353 #ifndef HYBRID
354 strtok(NULL, " "); // Ignore the TS
355 #endif
356 channel = strtok(NULL, " ");
357 rest = strtok(NULL, "");
358 tmp = strchr(rest, ':');
359 tmp++;
360 nick = strtok(tmp, " ");
361 while (nick != NULL)
362 {
363 if (*nick == '@')
364 nick++;
365 if (*nick == '+')
366 nick++; // Assume for users set op and voice, they
367 // are never passed as +@nick
368 if (stricmp(channel, c_Forest) == 0 && is_playing(nick))
369 raw(":%S MODE %s +v %s", channel, nick);
370
371 nick = strtok(NULL, " ");
372 }
373 #endif
374 } else {
375 #ifdef DEBUGMODE
376 log("Unrecognized Message: cmd = %s source = %s", cmd, source);
377 #endif
378 }
379 }
380
381 end:
382
383 save_gs_dbase();
384 save_day();
385
386 delete_monsters();
387 delete_masters();
388
389 #ifdef DEBUGMODE
390 log("<CLOSED>");
391 #endif
392
393 close(sock);
394 unload_config_file();
395 return 0;
396 }
397
398 aClient *find(char *nick)
399 {
400 return findbynick(nick);
401 }
402
403 aClient *find(const char *nick)
404 {
405 return findbynick(nick);
406 }
407
408 #ifdef P10
409
410 aClient *findbyrealnick(char *realnick)
411 {
412 ListNode <aClient> *newPtr;
413 newPtr = clients.First();
414
415 aClient *client = NULL;
416
417 while (newPtr)
418 {
419 client = newPtr->getData();
420 if (stricmp(client->getRealNick(), realnick) == 0)
421 return client;
422 client = NULL;
423 newPtr = newPtr->Next();
424 }
425 return client;
426 }
427
428 #else
429
430 aClient *findbyrealnick(char *realnick)
431 {
432 return findbynick(realnick);
433 }
434
435 #endif
436
437 aClient *findbynick(char *nick)
438 {
439 ListNode <aClient> *newPtr;
440 newPtr = clients.First();
441
442 aClient *client = NULL;
443
444 while (newPtr)
445 {
446 client = newPtr->getData();
447 #ifdef P10
448 if (strcmp(client->getNick(), nick) == 0)
449 #else
450 if (stricmp(client->getNick(), nick) == 0)
451 #endif
452 return client;
453 client = NULL;
454 newPtr = newPtr->Next();
455 }
456 return client;
457 }
458
459 aClient *findIRCplayer(const char *nick)
460 {
461 ListNode <aClient> *newPtr;
462 aClient *p = NULL;
463
464 for (newPtr = players.First(); newPtr; newPtr = newPtr->Next())
465 {
466 p = newPtr->getData();
467 #ifdef P10
468 if (strcmp(p->getNick(), nick) == 0)
469 #else
470 if (stricmp(p->getNick(), nick) == 0)
471 #endif
472 return p;
473 p = NULL;
474 }
475 return NULL;
476 }
477 aClient *findplayer(const char *name)
478 {
479 ListNode <aClient> *newPtr;
480 Player *p = NULL;
481
482 for (newPtr = players.First(); newPtr; newPtr = newPtr->Next())
483 {
484 p = newPtr->getData()->stats;
485 if (stricmp(p->name, name) == 0)
486 return newPtr->getData();
487 p = NULL;
488 }
489 return NULL;
490 }
491
492 aClient *findbynick(const char *nick)
493 {
494 ListNode <aClient> *newPtr;
495 newPtr = clients.First();
496
497 aClient *client = NULL;
498
499 while (newPtr)
500 {
501 client = newPtr->getData();
502 #ifdef P10
503 if (strcmp(client->getNick(), nick) == 0)
504 #else
505 if (stricmp(client->getNick(), nick) == 0)
506 #endif
507 return client;
508 client = NULL;
509 newPtr = newPtr->Next();
510 }
511 return client;
512 }
513
514 void load_day()
515 {
516 ifstream infile;
517
518 infile.open(".gsday");
519
520 if (infile.fail())
521 {
522 #ifdef DEBUGMODE
523 log("Error opening .gsday");
524 #endif
525
526 generate:
527 #ifdef DEBUGMODE
528 log("Generating new day");
529 #endif
530 struct tm *tm;
531 time_t ti;
532 time(&ti);
533 tm = localtime(&ti);
534
535 day = tm->tm_mday;
536
537 save_day();
538 return;
539 }
540
541 infile >> day;
542 infile.close();
543 if (day < 1 || day > 31)
544 goto generate;
545 }
546
547 void save_day()
548 {
549 ofstream outfile;
550
551 outfile.open(".gsday");
552
553 if (outfile.fail())
554 {
555 log("Error creating new file .gsday");
556 return;
557 }
558
559 outfile << day << endl;
560
561 outfile.close();
562 }
563
564 /* daemon() - detach process from user and disappear into the background
565 * returns -1 on failure, but you can't do much except exit in that case
566 * since we may already have forked. This is based on the BSD version,
567 * so the caller is responsible for things like the umask, etc.
568 */
569
570 /* believed to work on all Posix systems */
571
572 int daemon(int nochdir, int noclose)
573 {
574 pid_t pid;
575 switch (pid = fork())
576 {
577 case 0: break;
578 case -1: return -1;
579 default: _exit(0); /* exit the original process */
580 }
581
582 if (setsid() < 0) /* shoudn't fail */
583 return -1;
584
585 /* dyke out this switch if you want to acquire a control tty in */
586 /* the future -- not normally advisable for daemons */
587
588 switch (pid = fork())
589 {
590 case 0: break;
591 case -1: return -1;
592 default:
593 ofstream outfile;
594 outfile.open(pidfile);
595 if (outfile.fail())
596 cerr << "Unable to open " << pidfile << endl;
597 outfile << pid << endl;
598 outfile.close();
599
600 _exit(0);
601 }
602
603 if (!nochdir)
604 chdir("/");
605
606 if (!noclose)
607 {
608 closeall(0);
609 open("/dev/null",O_RDWR);
610 dup(0); dup(0);
611 }
612
613 return 0;
614 }
615
616
617 /* closeall() -- close all FDs >= a specified value */
618
619 void closeall(int fd)
620 {
621 int fdlimit = sysconf(_SC_OPEN_MAX);
622
623 while (fd < fdlimit)
624 close(fd++);
625 }
626
627 void prettyIntro()
628 {
629 cout << endl;
630 cout << " GGGG AAA MM MM EEEEEEE SSSSS EEEEEEE RRRRRR VV VV " << endl;
631 cout << " GG GG AAAAA MMM MMM EE SS EE RR RR VV VV " << endl;
632 cout << "GG AA AA MM MM MM EEEEE SSSSS EEEEE RRRRRR VV VV " << endl;
633 cout << "GG GGG AAAAAAA MM MM EE SS EE RR RR VV VV " << endl;
634 cout << "G G AA AA MM MM EEEEEEE SSSSS EEEEEEE RR RR VVV" << endl;
635 cout << " GGGGG V\n\n" << endl;
636 cout << "Version: " << VERSION << endl;
637 }