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