]>
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
);
95 int loadtime
= time(NULL
);
98 if (load_monsters() == false)
102 if (sock_gets(sock
,buffer
,sizeof(buffer
)) == -1) {
107 if (buffer
[0] == ':')
109 source
= strtok(buf
, " ");
110 cmd
= strtok(NULL
, " ");
113 cmd
= strtok(buf
, " ");
115 cout
<< "Server: " << buffer
<< endl
<< flush
;
117 // Wait N seconds then we're loaded.
120 if (time(NULL
) >= confloadtime
+ loadtime
)
124 if (stricmp(cmd
, "PING") == 0) {
126 timestamp
= strtok(NULL
, "");
127 raw("PONG %s", timestamp
);
128 } else if (stricmp(cmd
, "VERSION") == 0) {
130 server
= strtok(NULL
, " ");
132 raw(":%s 351 %s %s %s. %s", servername
, source
+1, PACKAGE
, VERSION
, servername
);
133 } else if (strncmp(cmd
, "NICK", 4) == 0) {
134 if (buffer
[0] == ':')
137 if ((tempPtr
= find((source
+ 1))))
140 nick
= strtok(NULL
, " ");
141 tempPtr
->setNick(nick
);
148 nick
= strtok(NULL
, " ");
149 newuser
= new aClient(nick
);
151 notice(s_GameServ
, nick
, welcomemsg
, nick
);
153 clients
.insertAtBack(newuser
);
156 } else if (stricmp(cmd
, "QUIT") == 0) {
158 if ((quitter
= find(source
+ 1)))
159 clients
.remove(quitter
);
160 if ((quitter
= findplayer(source
+ 1)))
161 quitter
->setNick("NULL");
163 } else if (stricmp(cmd
, "PRIVMSG") == 0) {
165 dest
= strtok(NULL
, " ");
166 rest
= strtok(NULL
, "");
167 if (strnicmp(dest
, s_GameServ
, strlen(s_GameServ
)) == 0)
168 gameserv(source
, rest
);
169 else if (stricmp(dest
, c_Forest
) == 0)
170 forest(source
, rest
);
171 } else if (stricmp(cmd
, "JOIN") == 0) {
173 channel
= strtok(NULL
, " ");
174 if (stricmp(channel
, c_Forest
) == 0 && is_playing(source
+ 1))
175 raw(":%S MODE %s +v %s", c_Forest
, (source
+ 1));
178 } else if (stricmp(cmd
, "SJOIN") == 0) {
179 char *channel
, *nick
;
180 strtok(NULL
, " "); // Ignore the TS
181 strtok(NULL
, " "); // Ignore the TS
182 channel
= strtok(NULL
, " ");
184 nick
= strtok(NULL
, " ");
185 nick
++; // Get rid of the :
186 if (stricmp(channel
, c_Forest
) == 0 && is_playing(nick
))
187 raw(":%S MODE %s +v %s", channel
, nick
);
191 // cout << "Unrecognized Message: cmd = " << cmd << setw(30) << "source = " <<
204 printf("<CLOSED>\n");
206 unload_config_file();
210 aClient
*find(char *nick
)
212 return findbynick(nick
);
215 aClient
*find(const char *nick
)
217 return findbynick(nick
);
221 aClient
*findbynick(char *nick
)
223 ListNode
<aClient
> *newPtr
;
224 newPtr
= clients
.First();
226 aClient
*client
= NULL
;
230 client
= newPtr
->getData();
231 if (stricmp(client
->getNick(), nick
) == 0)
234 newPtr
= newPtr
->Next();
239 aClient
*findplayer(const char *name
)
241 ListNode
<aClient
> *newPtr
;
244 for (newPtr
= players
.First(); newPtr
; newPtr
= newPtr
->Next())
246 p
= newPtr
->getData()->stats
;
247 if (stricmp(p
->name
, name
) == 0)
248 return newPtr
->getData();
254 aClient
*findbynick(const char *nick
)
256 ListNode
<aClient
> *newPtr
;
257 newPtr
= clients
.First();
259 aClient
*client
= NULL
;
263 client
= newPtr
->getData();
264 if (stricmp(client
->getNick(), nick
) == 0)
267 newPtr
= newPtr
->Next();
272 void load_timestamp()
276 infile
.open(".gstimestamp");
280 cout
<< "Error opening .gstimestamp" << endl
;
281 cout
<< "Generating new timestamp" << endl
;
283 timestamp
= midnight();
290 if (timestamp
< 1000000)
294 void save_timestamp()
298 outfile
.open(".gstimestamp");
302 cout
<< "Error creating new file." << endl
;
306 outfile
<< timestamp
<< endl
;
311 long int midnight(long int offset
)
313 return (time(NULL
) - (time(NULL
) % 86400)) + (offset
* 3600);