]> jfr.im git - irc/gameservirc.git/blame - gameserv/tcpclient.cpp
Now saving inventory to players.dat. Updates made in an attempt to use old databases.
[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);
28f552b8 170 if ((quitter = findplayer(source + 1)))
dff2bbf4 171 quitter->setNick("NULL");
85ce9d3e 172
173 } else if (stricmp(cmd, "PRIVMSG") == 0) {
174 char *rest, *dest;
175 dest = strtok(NULL, " ");
176 rest = strtok(NULL, "");
ad7dfaa0 177 if (strnicmp(dest, s_GameServ, strlen(s_GameServ)) == 0)
85ce9d3e 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));
c7340cbd 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
85ce9d3e 200 } else {
201 // cout << "Unrecognized Message: cmd = " << cmd << setw(30) << "source = " <<
202 // source << endl;
203 }
204 }
4dde2ed9 205
206 end:
207
c8ada07e 208 save_gs_dbase();
44ea29f7 209 save_timestamp();
4dde2ed9 210
c8ada07e 211 delete_monsters();
212 delete_masters();
213
85ce9d3e 214 printf("<CLOSED>\n");
215 close(sock);
216 unload_config_file();
217 return 0;
218}
219
220aClient *find(char *nick)
221{
222 return findbynick(nick);
223}
224
225aClient *find(const char *nick)
226{
227 return findbynick(nick);
228}
229
230
231aClient *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
0a1518fa 249aClient *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
85ce9d3e 264aClient *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
44ea29f7 282void load_timestamp()
283{
284 ifstream infile;
285
286 infile.open(".gstimestamp");
287
288 if (infile.fail())
289 {
9cc5ab57 290 cout << "Error opening .gstimestamp" << endl;
291 cout << "Generating new timestamp" << endl;
44ea29f7 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
304void save_timestamp()
305{
306 ofstream outfile;
307
308 outfile.open(".gstimestamp");
309
310 if (outfile.fail())
311 {
9cc5ab57 312 cout << "Error creating new file." << endl;
44ea29f7 313 return;
314 }
315
316 outfile << timestamp << endl;
317
318 outfile.close();
319}
320
321long int midnight(long int offset)
322{
323 return (time(NULL) - (time(NULL) % 86400)) + (offset * 3600);
324}