]> jfr.im git - irc/gameservirc.git/blame - gameserv/tcpclient.cpp
Fixed a few things with the version and ping.
[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"
16#include "list.h"
17#include "aClient.h"
18#include "extern.h"
19#include <stdio.h>
20#include <unistd.h>
21#include <string.h>
22#include <iostream.h>
44ea29f7 23#include <fstream.h>
85ce9d3e 24#include <iomanip.h>
85ce9d3e 25#include <stdlib.h>
26
85ce9d3e 27int sock;
44ea29f7 28long timestamp;
29
85ce9d3e 30List<aClient> clients;
31
44ea29f7 32void save_timestamp();
33void load_timestamp();
34
85ce9d3e 35int main(int argc, char *argv[])
36{
28f552b8 37 char buffer[1024], buf[1024];
85ce9d3e 38 int connected = 1;
39 char *cmd, *source = NULL;
40 srand(time(NULL));
44ea29f7 41
42 load_config_file();
85ce9d3e 43
44 if (argc == 1) {
45 argc = 3;
46 argv[1] = remoteserver;
47 argv[2] = remoteport;
48 }
49 if (argc != 3) {
50 fprintf(stderr,"Usage: tcpclient host port\n");
51 fprintf(stderr,"where host is the machine which is running the\n");
52 fprintf(stderr,"tcpserver program, and port is the port it is\n");
53 fprintf(stderr,"listening on.\n");
54 exit(EXIT_FAILURE);
55 }
56 ignore_pipe();
57 sock = make_connection(argv[2], SOCK_STREAM, argv[1]);
58 if (sock == -1) {
59 fprintf(stderr,"make_connection failed.\n");
60 unload_config_file();
61 return -1;
62 }
63
64 raw("PROTOCTL NICKv2 VHP");
65 raw("PASS :%s", remotepass);
66 raw("SERVER %s 1 :Testing Server", servername);
67 raw("NICK %S 1 %d %S %s %s %d +owghraAxNt %s :GameServ", time(NULL), gshost,
68 servername, time(NULL), gshost);
69 raw(":%S JOIN %s", c_Forest);
70 raw(":%S MODE %s +o %S", c_Forest);
71 raw(":%S MODE %s +ntm", c_Forest);
72
73 sock_gets(sock,buffer,sizeof(buffer)-1); /* -1 added thanks to
74 David Duchene <dave@ltd.com> for pointing out the possible
75 buffer overflow resulting from the linefeed added below. */
76
77
78 printf("Server: %s\n",buffer);
79 init_monsters();
ab4f4ec0 80 init_masters();
ad7dfaa0 81 load_gs_dbase();
44ea29f7 82 load_timestamp();
83
85ce9d3e 84 while (connected) {
85 if (sock_gets(sock,buffer,sizeof(buffer)) == -1) {
86 connected = 0;
87 }
88 strcpy(buf, buffer);
89
90 if (buffer[0] == ':')
91 {
92 source = strtok(buf, " ");
93 cmd = strtok(NULL, " ");
94 }
95 else
96 cmd = strtok(buf, " ");
97
0a1518fa 98 cout << "Server: " << buffer << endl << flush;
85ce9d3e 99 if (stricmp(cmd, "PING") == 0) {
0a1518fa 100 char *timestamp;
101 timestamp = strtok(NULL, "");
102 raw("PONG %s", timestamp);
85ce9d3e 103 } else if (strncmp(cmd, "NICK", 4) == 0) {
104 if (buffer[0] == ':')
105 {
106 aClient *tempPtr;
28f552b8 107 if ((tempPtr = find((source + 1))))
85ce9d3e 108 {
109 char *nick;
110 nick = strtok(NULL, " ");
111 tempPtr->setNick(nick);
112 }
113 }
114 else
115 {
116 char *nick;
117 aClient *newuser;
118 nick = strtok(NULL, " ");
119 newuser = new aClient(nick);
120 clients.insertAtBack(newuser);
121 delete newuser;
122 }
123 } else if (stricmp(cmd, "QUIT") == 0) {
124 aClient *quitter;
28f552b8 125 if ((quitter = find(source + 1)))
85ce9d3e 126 clients.remove(quitter);
28f552b8 127 if ((quitter = findplayer(source + 1)))
dff2bbf4 128 quitter->setNick("NULL");
85ce9d3e 129
130 } else if (stricmp(cmd, "PRIVMSG") == 0) {
131 char *rest, *dest;
132 dest = strtok(NULL, " ");
133 rest = strtok(NULL, "");
ad7dfaa0 134 if (strnicmp(dest, s_GameServ, strlen(s_GameServ)) == 0)
85ce9d3e 135 gameserv(source, rest);
136 else if (stricmp(dest, c_Forest) == 0)
137 forest(source, rest);
138 } else if (stricmp(cmd, "JOIN") == 0) {
139 char *channel;
140 channel = strtok(NULL, " ");
141 if (stricmp(channel, c_Forest) == 0 && is_playing(source + 1))
142 raw(":%S MODE %s +v %s", c_Forest, (source + 1));
143 } else {
144 // cout << "Unrecognized Message: cmd = " << cmd << setw(30) << "source = " <<
145 // source << endl;
146 }
147 }
c8ada07e 148 save_gs_dbase();
44ea29f7 149 save_timestamp();
c8ada07e 150 delete_monsters();
151 delete_masters();
152
85ce9d3e 153 printf("<CLOSED>\n");
154 close(sock);
155 unload_config_file();
156 return 0;
157}
158
159aClient *find(char *nick)
160{
161 return findbynick(nick);
162}
163
164aClient *find(const char *nick)
165{
166 return findbynick(nick);
167}
168
169
170aClient *findbynick(char *nick)
171{
172 ListNode <aClient> *newPtr;
173 newPtr = clients.First();
174
175 aClient *client = NULL;
176
177 while (newPtr)
178 {
179 client = newPtr->getData();
180 if (stricmp(client->getNick(), nick) == 0)
181 return client;
182 client = NULL;
183 newPtr = newPtr->Next();
184 }
185 return client;
186}
187
0a1518fa 188aClient *findplayer(const char *name)
189{
190 ListNode <aClient> *newPtr;
191 Player *p = NULL;
192
193 for (newPtr = players.First(); newPtr; newPtr = newPtr->Next())
194 {
195 p = newPtr->getData()->stats;
196 if (stricmp(p->name, name) == 0)
197 return newPtr->getData();
198 p = NULL;
199 }
200 return NULL;
201}
202
85ce9d3e 203aClient *findbynick(const char *nick)
204{
205 ListNode <aClient> *newPtr;
206 newPtr = clients.First();
207
208 aClient *client = NULL;
209
210 while (newPtr)
211 {
212 client = newPtr->getData();
213 if (stricmp(client->getNick(), nick) == 0)
214 return client;
215 client = NULL;
216 newPtr = newPtr->Next();
217 }
218 return client;
219}
220
44ea29f7 221void load_timestamp()
222{
223 ifstream infile;
224
225 infile.open(".gstimestamp");
226
227 if (infile.fail())
228 {
229 cerr << "Error opening .gstimestamp" << endl;
230 cerr << "Generating new timestamp" << endl;
231 generate:
232 timestamp = midnight();
233 save_timestamp();
234 return;
235 }
236
237 infile >> timestamp;
238 infile.close();
239 if (timestamp < 1000000)
240 goto generate;
241}
242
243void save_timestamp()
244{
245 ofstream outfile;
246
247 outfile.open(".gstimestamp");
248
249 if (outfile.fail())
250 {
251 cerr << "Error creating new file." << endl;
252 return;
253 }
254
255 outfile << timestamp << endl;
256
257 outfile.close();
258}
259
260long int midnight(long int offset)
261{
262 return (time(NULL) - (time(NULL) % 86400)) + (offset * 3600);
263}