]>
jfr.im git - irc/gameservirc.git/blob - gameserv/tcpclient.cpp
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.
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.
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/.
28 //#include <sys/types.h>
29 //#include <sys/wait.h>
37 char *PACKAGE
= "GameServ";
38 char *VERSION
= "1.2.1 +devel";
43 List
<aClient
> clients
[U_TABLE_SIZE
];
51 int daemon(int nochdir
, int noclose
);
53 // Close all file descriptors from >= fd
54 void closeall(int fd
);
56 int main(int argc
, char *argv
[])
58 char buffer
[1024], buf
[1024];
59 memset(buffer
, 0, 1024);
63 char *cmd
, *source
= NULL
, *conf
;
66 strcpy(conf
, "gameserv.conf");
69 * This needs to be fixed to work for any number of arguments in any
75 if ( argc
> 2 || stricmp(argv
[1], "--help") == 0)
77 cout
<< "Usage: gameserv [options] [configfile]" << endl
;
78 cout
<< "Options:" << endl
;
79 cout
<< "--help Displays this help dialogue" << endl
;
88 if (load_config_file(conf
))
90 cout
<< "Config file loaded ok...\n"
91 << "Turning into a daemon" << endl
;
102 perror("Could not turn into a daemon");
108 if (load_monsters() == false)
111 shuttingdown
= false;
113 char ignoreservers
[32][256];
115 currentserver
= strtok(ignoreserverslist
, " ");
116 for (int server
= 0; server
< 32 && currentserver
!= NULL
; server
++)
118 strncpy(ignoreservers
[server
], currentserver
, 255);
119 log("Placing %s on the server ignore list", currentserver
);
120 currentserver
= strtok(NULL
, " ");
123 strcpy(boss
.name
, "Red Dragon");
124 strcpy(boss
.weapon
, "Breath of Unholy Fire");
125 boss
.strength
= 1000;
126 boss
.gold
= 2000000000;
127 boss
.exp
= 2000000000;
130 strcpy(boss
.death
, "You finally snuff out the deadly murderous "\
131 "dragon's dark flames. You have freed the land of its terror "\
132 "filled reign from above!");
135 // This loop will retry the connection 3 times
136 for (int retry
= 0; retry
< 3 && !shuttingdown
; retry
++)
142 long int loadtime
= time(NULL
);
143 long int currentTime
;
144 long int oldTime
= loadtime
;
146 lastidlecheck
= loadtime
;
149 log("Setting primary Idle Check timestamp: %ld", lastidlecheck
);
154 sock
= make_connection(remoteport
, SOCK_STREAM
, remoteserver
);
156 fprintf(stderr
,"make_connection failed.\n");
157 unload_config_file();
160 log("%S socket connected.");
163 raw("PROTOCTL NICKv2 VHP");
164 raw("PASS :%s", remotepass
);
165 raw("SERVER %s 1 :%s", servername
, servername
);
166 raw("NICK %S 1 %d %S %s %s %d +w%s %s :%s v%s", time(NULL
), gshost
,
167 servername
, time(NULL
), (isBOper() ? "o" : ""), gshost
, PACKAGE
, VERSION
);
168 raw(":%S JOIN %s", c_Forest
);
169 raw(":%S MODE %s +tn", c_Forest
);
170 #elif defined(BAHAMUT)
171 raw("PASS %s :TS", remotepass
);
172 raw("SERVER %s 1 :%s", servername
, servername
);
173 raw("NICK %S 1 %d +w%s %s %s %s 0 :GameServ", time(NULL
), (isBOper() ? "o" : ""),
174 gsident
, gshost
, servername
);
175 raw(":%s SJOIN %d %d %s +nt :@%S", servername
, time(NULL
), time(NULL
), c_Forest
);
176 #elif defined(HYBRID)
177 raw("PASS %s :TS", remotepass
);
178 raw("SERVER %s 1 :%s", servername
, servername
);
179 raw("NICK %S 1 %d +w%s %s %s %s :GameServ", time(NULL
), (isBOper() ? "o" : ""),
180 gsident
, gshost
, servername
);
181 raw(":%s SJOIN %ld %s +nt :@%S", servername
, time(NULL
), c_Forest
);
183 // Server numeric is: [] <-- must be unique
184 raw("PASS :%s", remotepass
);
185 raw("SERVER %s 1 %d %d P10 []AAF :%s", servername
, time(NULL
), time(NULL
), servername
);
186 raw("[] N %S 1 %d %s %s %s DAqAoB %s :%S", time(NULL
), gsident
, gshost
,
187 (isBOper() ? "+o" : ""), gsnum
);
188 raw("[] B %s %d +tn %s:o", c_Forest
, time(NULL
) - 864000, gsnum
);
192 raw("%s T %s :%s", gsnum
, c_Forest
, c_ForestTopic
);
193 raw("[] EB"); // End burst
196 raw(":%S MODE %s +o %S", c_Forest
);
198 raw(":%S TOPIC %s :%s", c_Forest
, c_ForestTopic
);
201 sock_gets(sock
,buffer
,sizeof(buffer
)-1); /* -1 added thanks to
202 David Duchene <dave@ltd.com> for pointing out the possible
203 buffer overflow resulting from the linefeed added below. */
207 log("Server: %s",buffer
);
211 if (sock_gets(sock
,buffer
,sizeof(buffer
)) == -1) {
217 if (buffer
[0] == ':')
219 source
= strtok(buf
, " ");
220 cmd
= strtok(NULL
, " ");
223 cmd
= strtok(buf
, " ");
225 source
= strtok(buf
, " ");
226 cmd
= strtok(NULL
, " ");
230 log("Server: %s", buffer
);
233 // Wait N seconds then we're loaded.
236 if (time(NULL
) >= welcomedelay
+ loadtime
)
239 retry
= 0; // Start the reconnection cycle over
244 long TIME
= time(NULL
);
245 if (TIME
- lastidlecheck
>= idlecheckperiod
)
248 lastidlecheck
= TIME
;
252 // Save the player data every updateperiod seconds
253 currentTime
= time(NULL
);
254 if (currentTime
- oldTime
>= updateperiod
)
256 oldTime
= currentTime
;
257 log("Saving to %s", playerdata
);
263 if (stricmp(cmd
, "PING") == 0) {
265 timestamp
= strtok(NULL
, "");
266 raw("PONG %s", timestamp
);
268 if (stricmp(cmd
, "G") == 0) {
270 timestamp
= strtok(NULL
, " ");
271 raw("[] Z [] %s 0 %s", timestamp
+ 1, timestamp
);
274 } else if (stricmp(cmd
, "EB") == 0) {
277 } else if (stricmp(cmd
, "VERSION") == 0) {
279 server
= strtok(NULL
, " ");
281 raw(":%s 351 %s %s_%s. %s", servername
, source
+1, PACKAGE
, VERSION
, servername
);
283 } else if (strncmp(cmd
, "NICK", 4) == 0) {
284 if (buffer
[0] == ':')
287 if ((tempPtr
= find((source
+ 1))))
290 unsigned long oldhv
, newhv
;
291 nick
= strtok(NULL
, " ");
292 oldhv
= iHASH((unsigned char *) tempPtr
->getNick());
293 newhv
= iHASH((unsigned char *) nick
);
294 tempPtr
->setNick(nick
);
295 clients
[oldhv
].remove(tempPtr
);
296 clients
[newhv
].insertAtBack(tempPtr
);
303 } else if (stricmp(cmd
, "N") == 0 && strlen(source
) == 2) {
305 char *nick
, *realnick
;
306 realnick
= strtok(NULL
, " ");
308 for (int x
= 0; x
< 5; x
++)
309 nick
= strtok(NULL
, " ");
314 log ("aClient has modes");
317 // Searching for the +r mode (extra parameter)
318 for (unsigned int count
= 1; count
< strlen(nick
); count
++)
320 if (nick
[count
] == 'r')
322 nick
= strtok(NULL
, " ");
326 nick
= strtok(NULL
, " ");
329 aClient
*newuser
, *temp
;
331 nick
= strtok(NULL
, " ");
334 newuser
= new aClient(nick
, realnick
);
336 newuser
= new aClient(nick
);
345 notice(s_GameServ
, nick
, welcomemsg
, realnick
);
347 notice(s_GameServ
, nick
, welcomemsg
, nick
);
351 unsigned long hv
= sHASH((unsigned char *) nick
);
353 unsigned long hv
= iHASH((unsigned char *) nick
);
356 temp
= clients
[hv
].insertAtBack(newuser
);
358 #if defined(HYBRID) || defined(BAHAMUT)
362 nickserver
= strtok(NULL
, " ");
363 if (nickserver
[0] == '+')
367 nickserver
= strtok(NULL
, " ");
368 for (int x
= 0; x
< 32; x
++)
370 if (stricmp(ignoreservers
[x
], nickserver
) == 0)
376 #elif defined(UNREAL)
382 nickserver
= strtok(NULL
, " ");
383 for (int x
= 0; x
< 32; x
++)
385 if (stricmp(ignoreservers
[x
], nickserver
) == 0)
395 } else if (stricmp(cmd
, "Q") == 0) {
396 // unsigned long hv = sHASH((unsigned char *) source);
398 } else if (stricmp(cmd
, "QUIT") == 0) {
399 // unsigned long hv = iHASH((unsigned char *) source);
407 if (!(quitter
= find(source
)))
409 log("Fatal Error: could not find %s in the "\
410 "clients list", source
);
419 /* Attempting to use the logout() function
420 if ((quitter = find(source)))
421 clients[hv].remove(quitter);
422 if ((quitter = findIRCplayer(source)))
424 if (player_fight(quitter))
426 // Stop the fight on the other client
427 aClient *otherplayer = quitter->stats->battle;
428 otherplayer->stats->battle = NULL;
429 notice(s_GameServ, otherplayer->getNick(), "%s "\
430 "has quit IRC. The fight stops here.",
431 quitter->stats->name);
433 quitter->stats->battle = NULL;
434 quitter->stats->fight = NULL;
435 quitter->stats->master = NULL;
437 quitter->setNick("Not Playing");
439 quitter->setRealNick("Not Playing");
441 quitter->stats->client = NULL; // Unidentify them
446 } else if (stricmp(cmd
, "P") == 0) {
449 longname
= new char[strlen(s_GameServ
) + strlen(servername
) + 2];
451 sprintf(longname
, "%S@%s", servername
);
453 dest
= strtok(NULL
, " ");
454 rest
= strtok(NULL
, "");
455 if (stricmp(dest
, gsnum
) == 0 || stricmp(dest
, longname
) == 0)
458 gameserv(source
, rest
);
460 else if (stricmp(dest
, c_Forest
) == 0 && isListenOnCF())
463 forest(source
, rest
);
466 } else if (stricmp(cmd
, "PRIVMSG") == 0) {
468 dest
= strtok(NULL
, " ");
469 rest
= strtok(NULL
, "");
470 if (strnicmp(dest
, s_GameServ
, strlen(s_GameServ
)) == 0)
471 gameserv(source
, rest
);
472 else if (stricmp(dest
, c_Forest
) == 0 && isListenOnCF())
473 forest(source
, rest
);
476 } else if (stricmp(cmd
, "J") == 0) {
478 } else if (stricmp(cmd
, "JOIN") == 0) {
482 channel
= strtok(NULL
, " ");
489 joiner
= find(source
);
491 if (stricmp(channel
, c_Forest
) == 0 && is_playing(joiner
))
494 log("Player %s (IRC: %s) joined %s",
497 joiner
->getRealNick(),
503 raw(":%S MODE %s +v %s", c_Forest
, (source
));
510 } else if (stricmp(cmd
, "SJOIN") == 0) {
511 char *channel
, *nick
, *tmp
, *rest
;
512 strtok(NULL
, " "); // Ignore the TS
514 strtok(NULL
, " "); // Ignore the TS
516 channel
= strtok(NULL
, " ");
517 rest
= strtok(NULL
, "");
518 tmp
= strchr(rest
, ':');
520 nick
= strtok(tmp
, " ");
526 nick
++; // Assume for users set op and voice, they
527 // are never passed as +@nick
528 if (stricmp(channel
, c_Forest
) == 0 && is_playing(nick
))
529 raw(":%S MODE %s +v %s", channel
, nick
);
531 nick
= strtok(NULL
, " ");
536 log("Unrecognized Message: cmd = %s source = %s", cmd
, source
);
541 } // for loop for connection retry
556 unload_config_file();
560 aClient
*find(char *nick
)
562 return findbynick(nick
);
565 aClient
*find(const char *nick
)
567 return findbynick(nick
);
572 aClient
*findbyrealnick(char *realnick
)
574 ListNode
<aClient
> *newPtr
;
575 unsigned long hv
= sHASH((unsigned char *) realnick
);
576 newPtr
= clients
[hv
].First();
578 aClient
*client
= NULL
;
582 client
= newPtr
->getData();
583 if (stricmp(client
->getRealNick(), realnick
) == 0)
586 newPtr
= newPtr
->Next();
593 aClient
*findbyrealnick(char *realnick
)
595 return findbynick(realnick
);
600 aClient
*findbynick(char *nick
)
602 ListNode
<aClient
> *newPtr
;
604 unsigned long hv
= sHASH((unsigned char *) nick
);
606 unsigned long hv
= iHASH((unsigned char *) nick
);
609 newPtr
= clients
[hv
].First();
611 aClient
*client
= NULL
;
615 client
= newPtr
->getData();
617 if (strcmp(client
->getNick(), nick
) == 0)
619 if (stricmp(client
->getNick(), nick
) == 0)
623 newPtr
= newPtr
->Next();
628 aClient
*findIRCplayer(const char *nick
)
630 ListNode
<aClient
> *newPtr
;
638 unsigned long hv
= iHASH((unsigned char *) p
->stats
->name
);
640 for (newPtr
= players
[hv
].First(); newPtr
; newPtr
= newPtr
->Next())
642 p
= newPtr
->getData();
644 if (strcmp(p
->getNick(), nick
) == 0)
646 if (stricmp(p
->getNick(), nick
) == 0)
654 aClient
*findplayer(const char *name
)
656 ListNode
<aClient
> *newPtr
;
658 unsigned long hv
= iHASH((unsigned char *) name
);
659 for (newPtr
= players
[hv
].First(); newPtr
; newPtr
= newPtr
->Next())
661 p
= newPtr
->getData()->stats
;
662 if (stricmp(p
->name
, name
) == 0)
663 return newPtr
->getData();
671 ListNode
<aClient
> *newPtr
;
674 for (int x
= 0; x
< U_TABLE_SIZE
; x
++)
676 for (newPtr
= players
[x
].First(); newPtr
; newPtr
= newPtr
->Next())
678 p
= newPtr
->getData()->stats
;
687 aClient
*findbynick(const char *nick
)
689 ListNode
<aClient
> *newPtr
;
691 unsigned long hv
= sHASH((unsigned char *) nick
);
693 unsigned long hv
= iHASH((unsigned char *) nick
);
696 newPtr
= clients
[hv
].First();
698 aClient
*client
= NULL
;
702 client
= newPtr
->getData();
704 if (strcmp(client
->getNick(), nick
) == 0)
706 if (stricmp(client
->getNick(), nick
) == 0)
710 newPtr
= newPtr
->Next();
719 infile
.open(".gsday");
724 log("Error opening .gsday");
729 log("Generating new day");
744 if (day
< 1 || day
> 31)
752 outfile
.open(".gsday");
756 log("Error creating new file .gsday");
760 outfile
<< day
<< endl
;
765 /* daemon() - detach process from user and disappear into the background
766 * returns -1 on failure, but you can't do much except exit in that case
767 * since we may already have forked. This is based on the BSD version,
768 * so the caller is responsible for things like the umask, etc.
771 /* believed to work on all Posix systems */
773 int daemon(int nochdir
, int noclose
)
776 switch (pid
= fork())
780 default: _exit(0); /* exit the original process */
783 if (setsid() < 0) /* shoudn't fail */
786 /* dyke out this switch if you want to acquire a control tty in */
787 /* the future -- not normally advisable for daemons */
789 switch (pid
= fork())
795 outfile
.open(pidfile
);
797 cerr
<< "Unable to open " << pidfile
<< endl
;
798 outfile
<< pid
<< endl
;
810 open("/dev/null",O_RDWR
);
818 /* closeall() -- close all FDs >= a specified value */
820 void closeall(int fd
)
822 int fdlimit
= sysconf(_SC_OPEN_MAX
);
831 cout
<< " GGGG AAA MM MM EEEEEEE SSSSS EEEEEEE RRRRRR VV VV " << endl
;
832 cout
<< " GG GG AAAAA MMM MMM EE SS EE RR RR VV VV " << endl
;
833 cout
<< "GG AA AA MM MM MM EEEEE SSSSS EEEEE RRRRRR VV VV " << endl
;
834 cout
<< "GG GGG AAAAAAA MM MM EE SS EE RR RR VV VV " << endl
;
835 cout
<< "G G AA AA MM MM EEEEEEE SSSSS EEEEEEE RR RR VVV" << endl
;
836 cout
<< " GGGGG V\n\n" << endl
;
837 cout
<< "Version: " << VERSION
<< endl
;