]> jfr.im git - irc/gameservirc.git/blame - gameserv/tcpclient.cpp
* Added a Reset command to GameServ that functions the same was as the
[irc/gameservirc.git] / gameserv / tcpclient.cpp
CommitLineData
85ce9d3e 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"
c7340cbd 16#include "options.h"
85ce9d3e 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 <iostream.h>
44ea29f7 24#include <fstream.h>
85ce9d3e 25#include <iomanip.h>
85ce9d3e 26#include <stdlib.h>
27
91c0b563 28char *PACKAGE = "GameServ";
758ca8b3 29char *VERSION = "1.1.4";
173302fe 30
85ce9d3e 31int sock;
44ea29f7 32long timestamp;
33
85ce9d3e 34List<aClient> clients;
35
44ea29f7 36void save_timestamp();
37void load_timestamp();
38
85ce9d3e 39int main(int argc, char *argv[])
40{
28f552b8 41 char buffer[1024], buf[1024];
85ce9d3e 42 int connected = 1;
43 char *cmd, *source = NULL;
44 srand(time(NULL));
44ea29f7 45
46 load_config_file();
85ce9d3e 47
48 if (argc == 1) {
49 argc = 3;
50 argv[1] = remoteserver;
51 argv[2] = remoteport;
52 }
53 if (argc != 3) {
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");
58 exit(EXIT_FAILURE);
59 }
60 ignore_pipe();
61 sock = make_connection(argv[2], SOCK_STREAM, argv[1]);
62 if (sock == -1) {
63 fprintf(stderr,"make_connection failed.\n");
64 unload_config_file();
65 return -1;
66 }
67
c7340cbd 68#ifdef UNREAL
85ce9d3e 69 raw("PROTOCTL NICKv2 VHP");
70 raw("PASS :%s", remotepass);
c7340cbd 71 raw("SERVER %s 1 :%s", servername, servername);
173302fe 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);
85ce9d3e 74 raw(":%S JOIN %s", c_Forest);
c7340cbd 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,
80 servername);
81 raw(":%s SJOIN %d %d %s +mnt :@%S", servername, time(NULL), time(NULL), c_Forest);
82#endif
85ce9d3e 83 raw(":%S MODE %s +o %S", c_Forest);
c7340cbd 84 raw(":%S TOPIC %s :%s", c_Forest, c_ForestTopic);
85ce9d3e 85
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. */
89
90
91 printf("Server: %s\n",buffer);
ab4f4ec0 92 init_masters();
ad7dfaa0 93 load_gs_dbase();
44ea29f7 94 load_timestamp();
922daad7 95 long int loadtime = time(NULL);
96 long int currentTime;
97 long int oldTime = loadtime;
5963944b 98 bool loaded = false;
44ea29f7 99
4dde2ed9 100 if (load_monsters() == false)
101 goto end;
102
85ce9d3e 103 while (connected) {
104 if (sock_gets(sock,buffer,sizeof(buffer)) == -1) {
105 connected = 0;
106 }
107 strcpy(buf, buffer);
108
109 if (buffer[0] == ':')
110 {
111 source = strtok(buf, " ");
112 cmd = strtok(NULL, " ");
113 }
114 else
115 cmd = strtok(buf, " ");
116
0a1518fa 117 cout << "Server: " << buffer << endl << flush;
5963944b 118
bf2cabcd 119 // Wait N seconds then we're loaded.
5963944b 120 if (!loaded)
121 {
922daad7 122 if (time(NULL) >= welcomedelay + loadtime)
5963944b 123 loaded = true;
124 }
125
922daad7 126 // Save the player data every updateperiod seconds
127 currentTime = time(NULL);
128 if (currentTime - oldTime >= updateperiod)
129 {
130 oldTime = currentTime;
131 save_gs_dbase();
132 }
133
85ce9d3e 134 if (stricmp(cmd, "PING") == 0) {
0a1518fa 135 char *timestamp;
136 timestamp = strtok(NULL, "");
137 raw("PONG %s", timestamp);
0501fe18 138 } else if (stricmp(cmd, "VERSION") == 0) {
139 char *server;
140 server = strtok(NULL, " ");
141 server++;
0501fe18 142 raw(":%s 351 %s %s %s. %s", servername, source+1, PACKAGE, VERSION, servername);
85ce9d3e 143 } else if (strncmp(cmd, "NICK", 4) == 0) {
144 if (buffer[0] == ':')
145 {
146 aClient *tempPtr;
28f552b8 147 if ((tempPtr = find((source + 1))))
85ce9d3e 148 {
149 char *nick;
150 nick = strtok(NULL, " ");
151 tempPtr->setNick(nick);
152 }
153 }
154 else
155 {
156 char *nick;
157 aClient *newuser;
158 nick = strtok(NULL, " ");
159 newuser = new aClient(nick);
5963944b 160 if (loaded)
bf2cabcd 161 notice(s_GameServ, nick, welcomemsg, nick);
b0a5c536 162
85ce9d3e 163 clients.insertAtBack(newuser);
164 delete newuser;
165 }
166 } else if (stricmp(cmd, "QUIT") == 0) {
167 aClient *quitter;
28f552b8 168 if ((quitter = find(source + 1)))
85ce9d3e 169 clients.remove(quitter);
ee38284f 170 if ((quitter = findIRCplayer(source + 1)))
171 {
172 quitter->setNick("!NULL!");
173 quitter->stats->user = NULL; // Unidentify them
174 }
85ce9d3e 175
176 } else if (stricmp(cmd, "PRIVMSG") == 0) {
177 char *rest, *dest;
178 dest = strtok(NULL, " ");
179 rest = strtok(NULL, "");
ad7dfaa0 180 if (strnicmp(dest, s_GameServ, strlen(s_GameServ)) == 0)
85ce9d3e 181 gameserv(source, rest);
182 else if (stricmp(dest, c_Forest) == 0)
183 forest(source, rest);
184 } else if (stricmp(cmd, "JOIN") == 0) {
185 char *channel;
186 channel = strtok(NULL, " ");
187 if (stricmp(channel, c_Forest) == 0 && is_playing(source + 1))
188 raw(":%S MODE %s +v %s", c_Forest, (source + 1));
c7340cbd 189
190 #if defined(BAHAMUT)
191 } else if (stricmp(cmd, "SJOIN") == 0) {
192 char *channel, *nick;
193 strtok(NULL, " "); // Ignore the TS
194 strtok(NULL, " "); // Ignore the TS
195 channel = strtok(NULL, " ");
196 strtok(NULL, " ");
197 nick = strtok(NULL, " ");
198 nick++; // Get rid of the :
199 if (stricmp(channel, c_Forest) == 0 && is_playing(nick))
200 raw(":%S MODE %s +v %s", channel, nick);
201 #endif
202
85ce9d3e 203 } else {
204 // cout << "Unrecognized Message: cmd = " << cmd << setw(30) << "source = " <<
205 // source << endl;
206 }
207 }
4dde2ed9 208
209 end:
210
c8ada07e 211 save_gs_dbase();
44ea29f7 212 save_timestamp();
4dde2ed9 213
c8ada07e 214 delete_monsters();
215 delete_masters();
216
85ce9d3e 217 printf("<CLOSED>\n");
218 close(sock);
219 unload_config_file();
220 return 0;
221}
222
223aClient *find(char *nick)
224{
225 return findbynick(nick);
226}
227
228aClient *find(const char *nick)
229{
230 return findbynick(nick);
231}
232
233
234aClient *findbynick(char *nick)
235{
236 ListNode <aClient> *newPtr;
237 newPtr = clients.First();
238
239 aClient *client = NULL;
240
241 while (newPtr)
242 {
243 client = newPtr->getData();
244 if (stricmp(client->getNick(), nick) == 0)
245 return client;
246 client = NULL;
247 newPtr = newPtr->Next();
248 }
249 return client;
250}
251
ee38284f 252aClient *findIRCplayer(const char *nick)
253{
254 ListNode <aClient> *newPtr;
255 aClient *p = NULL;
256
257 for (newPtr = players.First(); newPtr; newPtr = newPtr->Next())
258 {
259 p = newPtr->getData();
260 if (stricmp(p->getNick(), nick) == 0)
261 return p;
262 p = NULL;
263 }
264 return NULL;
265}
0a1518fa 266aClient *findplayer(const char *name)
267{
268 ListNode <aClient> *newPtr;
269 Player *p = NULL;
270
271 for (newPtr = players.First(); newPtr; newPtr = newPtr->Next())
272 {
273 p = newPtr->getData()->stats;
274 if (stricmp(p->name, name) == 0)
275 return newPtr->getData();
276 p = NULL;
277 }
278 return NULL;
279}
280
85ce9d3e 281aClient *findbynick(const char *nick)
282{
283 ListNode <aClient> *newPtr;
284 newPtr = clients.First();
285
286 aClient *client = NULL;
287
288 while (newPtr)
289 {
290 client = newPtr->getData();
291 if (stricmp(client->getNick(), nick) == 0)
292 return client;
293 client = NULL;
294 newPtr = newPtr->Next();
295 }
296 return client;
297}
298
44ea29f7 299void load_timestamp()
300{
301 ifstream infile;
302
303 infile.open(".gstimestamp");
304
305 if (infile.fail())
306 {
9cc5ab57 307 cout << "Error opening .gstimestamp" << endl;
308 cout << "Generating new timestamp" << endl;
44ea29f7 309 generate:
310 timestamp = midnight();
311 save_timestamp();
312 return;
313 }
314
315 infile >> timestamp;
316 infile.close();
317 if (timestamp < 1000000)
318 goto generate;
319}
320
321void save_timestamp()
322{
323 ofstream outfile;
324
325 outfile.open(".gstimestamp");
326
327 if (outfile.fail())
328 {
9cc5ab57 329 cout << "Error creating new file." << endl;
44ea29f7 330 return;
331 }
332
333 outfile << timestamp << endl;
334
335 outfile.close();
336}
337
338long int midnight(long int offset)
339{
340 return (time(NULL) - (time(NULL) % 86400)) + (offset * 3600);
341}