]>
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/.
27 //#include <sys/types.h>
28 //#include <sys/wait.h>
38 char *PACKAGE
= "GameServ";
39 char *VERSION
= "1.2.0 +devel";
44 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];
60 char *cmd
, *source
= NULL
, *conf
= "gameserv.conf";
64 * This needs to be fixed to work for any number of arguments in any
70 if ( argc
> 2 || stricmp(argv
[1], "--help") == 0)
72 cout
<< "Usage: gameserv [options] [configfile]" << endl
;
73 cout
<< "Options:" << endl
;
74 cout
<< "--help Displays this help dialogue" << endl
;
77 conf
= new char[strlen(argv
[1])];
78 strcpy(conf
, argv
[1]);
83 if (load_config_file(conf
))
85 cout
<< "Config file loaded ok...\n"
86 << "Turning into a daemon" << endl
;
94 perror("Could not turn into a daemon");
99 sock
= make_connection(remoteport
, SOCK_STREAM
, remoteserver
);
101 fprintf(stderr
,"make_connection failed.\n");
102 unload_config_file();
107 raw("PROTOCTL NICKv2 VHP");
108 raw("PASS :%s", remotepass
);
109 raw("SERVER %s 1 :%s", servername
, servername
);
110 raw("NICK %S 1 %d %S %s %s %d +owghraAxNt %s :%s v%s", time(NULL
), gshost
,
111 servername
, time(NULL
), gshost
, PACKAGE
, VERSION
);
112 raw(":%S JOIN %s", c_Forest
);
113 raw(":%S MODE %s +mtn", c_Forest
);
114 #elif defined(BAHAMUT)
115 raw("PASS %s :TS", remotepass
);
116 raw("SERVER %s 1 :%s", servername
, servername
);
117 raw("NICK %S 1 %d +o %s %s %s 0 :GameServ", time(NULL
), gsident
, gshost
,
119 raw(":%s SJOIN %d %d %s +mnt :@%S", servername
, time(NULL
), time(NULL
), c_Forest
);
120 #elif defined(HYBRID)
121 raw("PASS %s :TS", remotepass
);
122 raw("SERVER %s 1 :%s", servername
, servername
);
123 raw("NICK %S 1 %d +o %s %s %s :GameServ", time(NULL
), gsident
, gshost
,
125 // Sending a timestamp of 1 to force ops.
126 raw(":%s SJOIN 1 %s +ntm :@%S", servername
, c_Forest
);
128 // Server numeric is: [] <-- must be unique
129 raw("PASS :%s", remotepass
);
130 raw("SERVER %s 1 %d %d P10 []AAF :%s", servername
, time(NULL
), time(NULL
), servername
);
131 raw("[] N %S 1 %d %s %s DAqAoB %s :%S", time(NULL
), gsident
, gshost
, gsnum
);
132 raw("[] B %s %d +tnm %s:o", c_Forest
, time(NULL
) - 864000, gsnum
);
136 raw("%s T %s :%s", gsnum
, c_Forest
, c_ForestTopic
);
137 raw("[] EB"); // End burst
140 raw(":%S MODE %s +o %S", c_Forest
);
142 raw(":%S TOPIC %s :%s", c_Forest
, c_ForestTopic
);
145 sock_gets(sock
,buffer
,sizeof(buffer
)-1); /* -1 added thanks to
146 David Duchene <dave@ltd.com> for pointing out the possible
147 buffer overflow resulting from the linefeed added below. */
151 log("Server: %s",buffer
);
154 strcpy(boss
.name
, "Red Dragon");
155 strcpy(boss
.weapon
, "Breath of Unholy Fire");
156 boss
.strength
= 6667;
157 boss
.gold
= 2000000000;
158 boss
.exp
= 2000000000;
159 strcpy(boss
.death
, "You finally snuff out the deadly murderous "\
160 "dragon's dark flames. You have freed the land of its terror "\
161 "filled reign from above!");
166 long int loadtime
= time(NULL
);
167 long int currentTime
;
168 long int oldTime
= loadtime
;
171 if (load_monsters() == false)
175 if (sock_gets(sock
,buffer
,sizeof(buffer
)) == -1) {
181 if (buffer
[0] == ':')
183 source
= strtok(buf
, " ");
184 cmd
= strtok(NULL
, " ");
187 cmd
= strtok(buf
, " ");
189 source
= strtok(buf
, " ");
190 cmd
= strtok(NULL
, " ");
194 log("Server: %s", buffer
);
197 // Wait N seconds then we're loaded.
200 if (time(NULL
) >= welcomedelay
+ loadtime
)
204 // Save the player data every updateperiod seconds
205 currentTime
= time(NULL
);
206 if (currentTime
- oldTime
>= updateperiod
)
208 oldTime
= currentTime
;
214 if (stricmp(cmd
, "PING") == 0) {
216 timestamp
= strtok(NULL
, "");
217 raw("PONG %s", timestamp
);
219 if (stricmp(cmd
, "G") == 0) {
221 timestamp
= strtok(NULL
, " ");
222 raw("[] Z [] %s 0 %s", timestamp
+ 1, timestamp
);
225 } else if (stricmp(cmd
, "EB") == 0) {
228 } else if (stricmp(cmd
, "VERSION") == 0) {
230 server
= strtok(NULL
, " ");
232 raw(":%s 351 %s %s_%s. %s", servername
, source
+1, PACKAGE
, VERSION
, servername
);
234 } else if (strncmp(cmd
, "NICK", 4) == 0) {
235 if (buffer
[0] == ':')
238 if ((tempPtr
= find((source
+ 1))))
241 nick
= strtok(NULL
, " ");
242 tempPtr
->setNick(nick
);
249 } else if (stricmp(cmd
, "N") == 0 && strlen(source
) == 2) {
251 char *nick
, *realnick
;
252 realnick
= strtok(NULL
, " ");
254 for (int x
= 0; x
< 5; x
++)
255 nick
= strtok(NULL
, " ");
260 log ("aClient has modes");
263 // Searching for the +r mode (extra parameter)
264 for (unsigned int count
= 1; count
< strlen(nick
); count
++)
266 if (nick
[count
] == 'r')
268 nick
= strtok(NULL
, " ");
272 nick
= strtok(NULL
, " ");
277 nick
= strtok(NULL
, " ");
280 newuser
= new aClient(nick
, realnick
);
282 newuser
= new aClient(nick
);
288 notice(s_GameServ
, nick
, welcomemsg
, realnick
);
290 notice(s_GameServ
, nick
, welcomemsg
, nick
);
292 unsigned long hv
= HASH((unsigned char *) nick
, U_TABLE_SIZE
);
293 clients
[hv
].insertAtBack(newuser
);
297 } else if (stricmp(cmd
, "Q") == 0) {
299 } else if (stricmp(cmd
, "QUIT") == 0) {
307 unsigned long hv
= HASH((unsigned char *) source
, U_TABLE_SIZE
);
308 if ((quitter
= find(source
)))
309 clients
[hv
].remove(quitter
);
310 if ((quitter
= findIRCplayer(source
)))
312 if (player_fight(quitter
))
314 // Stop the fight on the other client
315 aClient
*otherplayer
= quitter
->stats
->battle
;
316 otherplayer
->stats
->battle
= NULL
;
317 notice(s_GameServ
, otherplayer
->getNick(), "%s "\
318 "has quit IRC. The fight stops here.",
319 quitter
->stats
->name
);
321 quitter
->stats
->battle
= NULL
;
322 quitter
->stats
->fight
= NULL
;
323 quitter
->stats
->master
= NULL
;
325 quitter
->setNick("!NULL!");
327 quitter
->setRealNick("!NULL!");
329 quitter
->stats
->user
= NULL
; // Unidentify them
336 } else if (stricmp(cmd
, "P") == 0) {
339 longname
= new char[strlen(s_GameServ
) + strlen(servername
) + 2];
341 sprintf(longname
, "%S@%s", servername
);
343 dest
= strtok(NULL
, " ");
344 rest
= strtok(NULL
, "");
345 if (stricmp(dest
, gsnum
) == 0 || stricmp(dest
, longname
) == 0)
348 gameserv(source
, rest
);
350 else if (stricmp(dest
, c_Forest
) == 0)
353 forest(source
, rest
);
356 } else if (stricmp(cmd
, "PRIVMSG") == 0) {
358 dest
= strtok(NULL
, " ");
359 rest
= strtok(NULL
, "");
360 if (strnicmp(dest
, s_GameServ
, strlen(s_GameServ
)) == 0)
361 gameserv(source
, rest
);
362 else if (stricmp(dest
, c_Forest
) == 0)
363 forest(source
, rest
);
366 } else if (stricmp(cmd
, "J") == 0) {
368 } else if (stricmp(cmd
, "JOIN") == 0) {
372 channel
= strtok(NULL
, " ");
379 joiner
= find(source
);
381 if (stricmp(channel
, c_Forest
) == 0 && is_playing(joiner
))
384 log("Player %s (IRC: %s) joined %s",
387 joiner
->getRealNick(),
393 raw(":%S MODE %s +v %s", c_Forest
, (source
));
400 } else if (stricmp(cmd
, "SJOIN") == 0) {
401 char *channel
, *nick
, *tmp
, *rest
;
402 strtok(NULL
, " "); // Ignore the TS
404 strtok(NULL
, " "); // Ignore the TS
406 channel
= strtok(NULL
, " ");
407 rest
= strtok(NULL
, "");
408 tmp
= strchr(rest
, ':');
410 nick
= strtok(tmp
, " ");
416 nick
++; // Assume for users set op and voice, they
417 // are never passed as +@nick
418 if (stricmp(channel
, c_Forest
) == 0 && is_playing(nick
))
419 raw(":%S MODE %s +v %s", channel
, nick
);
421 nick
= strtok(NULL
, " ");
426 log("Unrecognized Message: cmd = %s source = %s", cmd
, source
);
444 unload_config_file();
448 aClient
*find(char *nick
)
450 return findbynick(nick
);
453 aClient
*find(const char *nick
)
455 return findbynick(nick
);
460 aClient
*findbyrealnick(char *realnick
)
462 ListNode
<aClient
> *newPtr
;
463 unsigned long hv
= HASH((unsigned char *) realnick
, U_TABLE_SIZE
);
464 newPtr
= clients
[hv
].First();
466 aClient
*client
= NULL
;
470 client
= newPtr
->getData();
471 if (stricmp(client
->getRealNick(), realnick
) == 0)
474 newPtr
= newPtr
->Next();
481 aClient
*findbyrealnick(char *realnick
)
483 return findbynick(realnick
);
488 aClient
*findbynick(char *nick
)
490 ListNode
<aClient
> *newPtr
;
491 unsigned long hv
= HASH((unsigned char *) nick
, U_TABLE_SIZE
);
492 newPtr
= clients
[hv
].First();
494 aClient
*client
= NULL
;
498 client
= newPtr
->getData();
500 if (strcmp(client
->getNick(), nick
) == 0)
502 if (stricmp(client
->getNick(), nick
) == 0)
506 newPtr
= newPtr
->Next();
511 aClient
*findIRCplayer(const char *nick
)
513 ListNode
<aClient
> *newPtr
;
516 unsigned long hv
= HASH((unsigned char *) nick
, U_TABLE_SIZE
);
517 for (newPtr
= players
[hv
].First(); newPtr
; newPtr
= newPtr
->Next())
519 p
= newPtr
->getData();
521 if (strcmp(p
->getNick(), nick
) == 0)
523 if (stricmp(p
->getNick(), nick
) == 0)
530 aClient
*findplayer(const char *name
)
532 ListNode
<aClient
> *newPtr
;
534 unsigned long hv
= HASH((unsigned char *) name
, U_TABLE_SIZE
);
535 for (newPtr
= players
[hv
].First(); newPtr
; newPtr
= newPtr
->Next())
537 p
= newPtr
->getData()->stats
;
538 if (stricmp(p
->name
, name
) == 0)
539 return newPtr
->getData();
545 aClient
*findbynick(const char *nick
)
547 ListNode
<aClient
> *newPtr
;
548 unsigned long hv
= HASH((unsigned char *) nick
, U_TABLE_SIZE
);
549 newPtr
= clients
[hv
].First();
551 aClient
*client
= NULL
;
555 client
= newPtr
->getData();
557 if (strcmp(client
->getNick(), nick
) == 0)
559 if (stricmp(client
->getNick(), nick
) == 0)
563 newPtr
= newPtr
->Next();
572 infile
.open(".gsday");
577 log("Error opening .gsday");
582 log("Generating new day");
597 if (day
< 1 || day
> 31)
605 outfile
.open(".gsday");
609 log("Error creating new file .gsday");
613 outfile
<< day
<< endl
;
618 /* daemon() - detach process from user and disappear into the background
619 * returns -1 on failure, but you can't do much except exit in that case
620 * since we may already have forked. This is based on the BSD version,
621 * so the caller is responsible for things like the umask, etc.
624 /* believed to work on all Posix systems */
626 int daemon(int nochdir
, int noclose
)
629 switch (pid
= fork())
633 default: _exit(0); /* exit the original process */
636 if (setsid() < 0) /* shoudn't fail */
639 /* dyke out this switch if you want to acquire a control tty in */
640 /* the future -- not normally advisable for daemons */
642 switch (pid
= fork())
648 outfile
.open(pidfile
);
650 cerr
<< "Unable to open " << pidfile
<< endl
;
651 outfile
<< pid
<< endl
;
663 open("/dev/null",O_RDWR
);
671 /* closeall() -- close all FDs >= a specified value */
673 void closeall(int fd
)
675 int fdlimit
= sysconf(_SC_OPEN_MAX
);
684 cout
<< " GGGG AAA MM MM EEEEEEE SSSSS EEEEEEE RRRRRR VV VV " << endl
;
685 cout
<< " GG GG AAAAA MMM MMM EE SS EE RR RR VV VV " << endl
;
686 cout
<< "GG AA AA MM MM MM EEEEE SSSSS EEEEE RRRRRR VV VV " << endl
;
687 cout
<< "GG GGG AAAAAAA MM MM EE SS EE RR RR VV VV " << endl
;
688 cout
<< "G G AA AA MM MM EEEEEEE SSSSS EEEEEEE RR RR VVV" << endl
;
689 cout
<< " GGGGG V\n\n" << endl
;
690 cout
<< "Version: " << VERSION
<< endl
;