]>
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 char * PACKAGE
= "GameServ" ;
29 char * VERSION
= "1.1.2" ;
34 List
< aClient
> clients
;
36 void save_timestamp ();
37 void load_timestamp ();
39 int main ( int argc
, char * argv
[])
41 char buffer
[ 1024 ], buf
[ 1024 ];
43 char * cmd
, * source
= NULL
;
50 argv
[ 1 ] = remoteserver
;
54 fprintf ( stderr
, "Usage: tcpclient host port \n " );
55 fprintf ( stderr
, "where host is the machine which is running the \n " );
56 fprintf ( stderr
, "tcpserver program, and port is the port it is \n " );
57 fprintf ( stderr
, "listening on. \n " );
61 sock
= make_connection ( argv
[ 2 ], SOCK_STREAM
, argv
[ 1 ]);
63 fprintf ( stderr
, "make_connection failed. \n " );
69 raw ( "PROTOCTL NICKv2 VHP" );
70 raw ( "PASS : %s " , remotepass
);
71 raw ( "SERVER %s 1 : %s " , servername
, servername
);
72 raw ( "NICK %S 1 %d %S %s %s %d +owghraAxNt %s : %s v %s " , time ( NULL
), gshost
,
73 servername
, time ( NULL
), gshost
, PACKAGE
, VERSION
);
74 raw ( ": %S JOIN %s " , c_Forest
);
75 raw ( ": %S MODE %s +mtn" , c_Forest
);
76 #elif defined(BAHAMUT)
77 raw ( "PASS %s :TS" , remotepass
);
78 raw ( "SERVER %s 1 : %s " , servername
, servername
);
79 raw ( "NICK %S 1 %d +o %s %s %s 0 :GameServ" , time ( NULL
), gsident
, gshost
,
81 raw ( ": %s SJOIN %d %d %s +mnt :@ %S " , servername
, time ( NULL
), time ( NULL
), c_Forest
);
83 raw ( ": %S MODE %s +o %S " , c_Forest
);
84 raw ( ": %S TOPIC %s : %s " , c_Forest
, c_ForestTopic
);
86 sock_gets ( sock
, buffer
, sizeof ( buffer
)- 1 ); /* -1 added thanks to
87 David Duchene <dave@ltd.com> for pointing out the possible
88 buffer overflow resulting from the linefeed added below. */
91 printf ( "Server: %s \n " , buffer
);
96 int loadtime
= time ( NULL
);
100 if ( sock_gets ( sock
, buffer
, sizeof ( buffer
)) == - 1 ) {
105 if ( buffer
[ 0 ] == ':' )
107 source
= strtok ( buf
, " " );
108 cmd
= strtok ( NULL
, " " );
111 cmd
= strtok ( buf
, " " );
113 cout
<< "Server: " << buffer
<< endl
<< flush
;
115 // Wait five seconds then we're loaded.
118 if ( time ( NULL
) >= 5 + loadtime
)
122 if ( stricmp ( cmd
, "PING" ) == 0 ) {
124 timestamp
= strtok ( NULL
, "" );
125 raw ( "PONG %s " , timestamp
);
126 } else if ( stricmp ( cmd
, "VERSION" ) == 0 ) {
128 server
= strtok ( NULL
, " " );
130 //351 GameServ Unreal3.2-beta18. irc.the-irc.org :FhiXOo [Linux shell.the-irc.org
131 //2.4.16-010stab017.17.777-smp #1 SMP Wed Mar 19 16:25:17 MSK 2003 i686 unknown=2303]
133 raw ( ": %s 351 %s %s %s . %s " , servername
, source
+ 1 , PACKAGE
, VERSION
, servername
);
134 } else if ( strncmp ( cmd
, "NICK" , 4 ) == 0 ) {
135 if ( buffer
[ 0 ] == ':' )
138 if (( tempPtr
= find (( source
+ 1 ))))
141 nick
= strtok ( NULL
, " " );
142 tempPtr
-> setNick ( nick
);
149 nick
= strtok ( NULL
, " " );
150 newuser
= new aClient ( nick
);
152 notice ( s_GameServ
, nick
, "Hello, %s ! This network utilizes a services package called GameServ. For info on how to play the game, type /msg %S help." , source
+ 1 );
154 clients
. insertAtBack ( newuser
);
157 } else if ( stricmp ( cmd
, "QUIT" ) == 0 ) {
159 if (( quitter
= find ( source
+ 1 )))
160 clients
. remove ( quitter
);
161 if (( quitter
= findplayer ( source
+ 1 )))
162 quitter
-> setNick ( "NULL" );
164 } else if ( stricmp ( cmd
, "PRIVMSG" ) == 0 ) {
166 dest
= strtok ( NULL
, " " );
167 rest
= strtok ( NULL
, "" );
168 if ( strnicmp ( dest
, s_GameServ
, strlen ( s_GameServ
)) == 0 )
169 gameserv ( source
, rest
);
170 else if ( stricmp ( dest
, c_Forest
) == 0 )
171 forest ( source
, rest
);
172 } else if ( stricmp ( cmd
, "JOIN" ) == 0 ) {
174 channel
= strtok ( NULL
, " " );
175 if ( stricmp ( channel
, c_Forest
) == 0 && is_playing ( source
+ 1 ))
176 raw ( ": %S MODE %s +v %s " , c_Forest
, ( source
+ 1 ));
179 } else if ( stricmp ( cmd
, "SJOIN" ) == 0 ) {
180 char * channel
, * nick
;
181 strtok ( NULL
, " " ); // Ignore the TS
182 strtok ( NULL
, " " ); // Ignore the TS
183 channel
= strtok ( NULL
, " " );
185 nick
= strtok ( NULL
, " " );
186 nick
++; // Get rid of the :
187 if ( stricmp ( channel
, c_Forest
) == 0 && is_playing ( nick
))
188 raw ( ": %S MODE %s +v %s " , channel
, nick
);
192 // cout << "Unrecognized Message: cmd = " << cmd << setw(30) << "source = " <<
201 printf ( "<CLOSED> \n " );
203 unload_config_file ();
207 aClient
* find ( char * nick
)
209 return findbynick ( nick
);
212 aClient
* find ( const char * nick
)
214 return findbynick ( nick
);
218 aClient
* findbynick ( char * nick
)
220 ListNode
< aClient
> * newPtr
;
221 newPtr
= clients
. First ();
223 aClient
* client
= NULL
;
227 client
= newPtr
-> getData ();
228 if ( stricmp ( client
-> getNick (), nick
) == 0 )
231 newPtr
= newPtr
-> Next ();
236 aClient
* findplayer ( const char * name
)
238 ListNode
< aClient
> * newPtr
;
241 for ( newPtr
= players
. First (); newPtr
; newPtr
= newPtr
-> Next ())
243 p
= newPtr
-> getData ()-> stats
;
244 if ( stricmp ( p
-> name
, name
) == 0 )
245 return newPtr
-> getData ();
251 aClient
* findbynick ( const char * nick
)
253 ListNode
< aClient
> * newPtr
;
254 newPtr
= clients
. First ();
256 aClient
* client
= NULL
;
260 client
= newPtr
-> getData ();
261 if ( stricmp ( client
-> getNick (), nick
) == 0 )
264 newPtr
= newPtr
-> Next ();
269 void load_timestamp ()
273 infile
. open ( ".gstimestamp" );
277 cout
<< "Error opening .gstimestamp" << endl
;
278 cout
<< "Generating new timestamp" << endl
;
280 timestamp
= midnight ();
287 if ( timestamp
< 1000000 )
291 void save_timestamp ()
295 outfile
. open ( ".gstimestamp" );
299 cout
<< "Error creating new file." << endl
;
303 outfile
<< timestamp
<< endl
;
308 long int midnight ( long int offset
)
310 return ( time ( NULL
) - ( time ( NULL
) % 86400 )) + ( offset
* 3600 );