]> jfr.im git - irc/gameservirc.git/blob - gameserv/tcpclient.cpp
Bugfix relating to player fights (in Changes file) and fixed the run script.
[irc/gameservirc.git] / gameserv / tcpclient.cpp
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"
16 #include "options.h"
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>
24 #include <fstream.h>
25 #include <iomanip.h>
26 #include <stdlib.h>
27
28 char *PACKAGE = "GameServ";
29 char *VERSION = "1.1.4";
30
31 int sock;
32 long timestamp;
33
34 List<aClient> clients;
35
36 void save_timestamp();
37 void load_timestamp();
38
39 int main(int argc, char *argv[])
40 {
41 char buffer[1024], buf[1024];
42 int connected = 1;
43 char *cmd, *source = NULL;
44 srand(time(NULL));
45
46 load_config_file();
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
68 #ifdef UNREAL
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,
80 servername);
81 raw(":%s SJOIN %d %d %s +mnt :@%S", servername, time(NULL), time(NULL), c_Forest);
82 #endif
83 raw(":%S MODE %s +o %S", c_Forest);
84 raw(":%S TOPIC %s :%s", c_Forest, c_ForestTopic);
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);
92 init_masters();
93 load_gs_dbase();
94 load_timestamp();
95 long int loadtime = time(NULL);
96 long int currentTime;
97 long int oldTime = loadtime;
98 bool loaded = false;
99
100 if (load_monsters() == false)
101 goto end;
102
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
117 cout << "Server: " << buffer << endl << flush;
118
119 // Wait N seconds then we're loaded.
120 if (!loaded)
121 {
122 if (time(NULL) >= welcomedelay + loadtime)
123 loaded = true;
124 }
125
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
134 if (stricmp(cmd, "PING") == 0) {
135 char *timestamp;
136 timestamp = strtok(NULL, "");
137 raw("PONG %s", timestamp);
138 } else if (stricmp(cmd, "VERSION") == 0) {
139 char *server;
140 server = strtok(NULL, " ");
141 server++;
142 raw(":%s 351 %s %s %s. %s", servername, source+1, PACKAGE, VERSION, servername);
143 } else if (strncmp(cmd, "NICK", 4) == 0) {
144 if (buffer[0] == ':')
145 {
146 aClient *tempPtr;
147 if ((tempPtr = find((source + 1))))
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);
160 if (loaded)
161 notice(s_GameServ, nick, welcomemsg, nick);
162
163 clients.insertAtBack(newuser);
164 delete newuser;
165 }
166 } else if (stricmp(cmd, "QUIT") == 0) {
167 aClient *quitter;
168 if ((quitter = find(source + 1)))
169 clients.remove(quitter);
170 if ((quitter = findplayer(source + 1)))
171 quitter->setNick("NULL");
172
173 } else if (stricmp(cmd, "PRIVMSG") == 0) {
174 char *rest, *dest;
175 dest = strtok(NULL, " ");
176 rest = strtok(NULL, "");
177 if (strnicmp(dest, s_GameServ, strlen(s_GameServ)) == 0)
178 gameserv(source, rest);
179 else if (stricmp(dest, c_Forest) == 0)
180 forest(source, rest);
181 } else if (stricmp(cmd, "JOIN") == 0) {
182 char *channel;
183 channel = strtok(NULL, " ");
184 if (stricmp(channel, c_Forest) == 0 && is_playing(source + 1))
185 raw(":%S MODE %s +v %s", c_Forest, (source + 1));
186
187 #if defined(BAHAMUT)
188 } else if (stricmp(cmd, "SJOIN") == 0) {
189 char *channel, *nick;
190 strtok(NULL, " "); // Ignore the TS
191 strtok(NULL, " "); // Ignore the TS
192 channel = strtok(NULL, " ");
193 strtok(NULL, " ");
194 nick = strtok(NULL, " ");
195 nick++; // Get rid of the :
196 if (stricmp(channel, c_Forest) == 0 && is_playing(nick))
197 raw(":%S MODE %s +v %s", channel, nick);
198 #endif
199
200 } else {
201 // cout << "Unrecognized Message: cmd = " << cmd << setw(30) << "source = " <<
202 // source << endl;
203 }
204 }
205
206 end:
207
208 save_gs_dbase();
209 save_timestamp();
210
211 delete_monsters();
212 delete_masters();
213
214 printf("<CLOSED>\n");
215 close(sock);
216 unload_config_file();
217 return 0;
218 }
219
220 aClient *find(char *nick)
221 {
222 return findbynick(nick);
223 }
224
225 aClient *find(const char *nick)
226 {
227 return findbynick(nick);
228 }
229
230
231 aClient *findbynick(char *nick)
232 {
233 ListNode <aClient> *newPtr;
234 newPtr = clients.First();
235
236 aClient *client = NULL;
237
238 while (newPtr)
239 {
240 client = newPtr->getData();
241 if (stricmp(client->getNick(), nick) == 0)
242 return client;
243 client = NULL;
244 newPtr = newPtr->Next();
245 }
246 return client;
247 }
248
249 aClient *findplayer(const char *name)
250 {
251 ListNode <aClient> *newPtr;
252 Player *p = NULL;
253
254 for (newPtr = players.First(); newPtr; newPtr = newPtr->Next())
255 {
256 p = newPtr->getData()->stats;
257 if (stricmp(p->name, name) == 0)
258 return newPtr->getData();
259 p = NULL;
260 }
261 return NULL;
262 }
263
264 aClient *findbynick(const char *nick)
265 {
266 ListNode <aClient> *newPtr;
267 newPtr = clients.First();
268
269 aClient *client = NULL;
270
271 while (newPtr)
272 {
273 client = newPtr->getData();
274 if (stricmp(client->getNick(), nick) == 0)
275 return client;
276 client = NULL;
277 newPtr = newPtr->Next();
278 }
279 return client;
280 }
281
282 void load_timestamp()
283 {
284 ifstream infile;
285
286 infile.open(".gstimestamp");
287
288 if (infile.fail())
289 {
290 cout << "Error opening .gstimestamp" << endl;
291 cout << "Generating new timestamp" << endl;
292 generate:
293 timestamp = midnight();
294 save_timestamp();
295 return;
296 }
297
298 infile >> timestamp;
299 infile.close();
300 if (timestamp < 1000000)
301 goto generate;
302 }
303
304 void save_timestamp()
305 {
306 ofstream outfile;
307
308 outfile.open(".gstimestamp");
309
310 if (outfile.fail())
311 {
312 cout << "Error creating new file." << endl;
313 return;
314 }
315
316 outfile << timestamp << endl;
317
318 outfile.close();
319 }
320
321 long int midnight(long int offset)
322 {
323 return (time(NULL) - (time(NULL) % 86400)) + (offset * 3600);
324 }