]> jfr.im git - irc/gameservirc.git/blob - gameserv/tcpclient.cpp
Added a bunch of empty monster structures to add stability in case for some reason...
[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 "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>
23 #include <iomanip.h>
24 #include <time.h>
25 #include <stdlib.h>
26
27 int sock;
28 List<aClient> clients;
29
30 int main(int argc, char *argv[])
31 {
32 char buffer[1024], buf[1024], input[1024], uplink[80], kb[1024];
33 int connected = 1;
34 char *cmd, *source = NULL;
35 srand(time(NULL));
36
37 load_config_file();
38
39 if (argc == 1) {
40 argc = 3;
41 argv[1] = remoteserver;
42 argv[2] = remoteport;
43 }
44 if (argc != 3) {
45 fprintf(stderr,"Usage: tcpclient host port\n");
46 fprintf(stderr,"where host is the machine which is running the\n");
47 fprintf(stderr,"tcpserver program, and port is the port it is\n");
48 fprintf(stderr,"listening on.\n");
49 exit(EXIT_FAILURE);
50 }
51 ignore_pipe();
52 sock = make_connection(argv[2], SOCK_STREAM, argv[1]);
53 if (sock == -1) {
54 fprintf(stderr,"make_connection failed.\n");
55 unload_config_file();
56 return -1;
57 }
58
59 raw("PROTOCTL NICKv2 VHP");
60 raw("PASS :%s", remotepass);
61 raw("SERVER %s 1 :Testing Server", servername);
62 raw("NICK %S 1 %d %S %s %s %d +owghraAxNt %s :GameServ", time(NULL), gshost,
63 servername, time(NULL), gshost);
64 raw(":%S JOIN %s", c_Forest);
65 raw(":%S MODE %s +o %S", c_Forest);
66 raw(":%S MODE %s +ntm", c_Forest);
67
68 sock_gets(sock,buffer,sizeof(buffer)-1); /* -1 added thanks to
69 David Duchene <dave@ltd.com> for pointing out the possible
70 buffer overflow resulting from the linefeed added below. */
71
72
73 printf("Server: %s\n",buffer);
74 init_monsters();
75 load_gs_dbase();
76 while (connected) {
77 if (sock_gets(sock,buffer,sizeof(buffer)) == -1) {
78 connected = 0;
79 }
80 strcpy(buf, buffer);
81
82 if (buffer[0] == ':')
83 {
84 source = strtok(buf, " ");
85 cmd = strtok(NULL, " ");
86 }
87 else
88 cmd = strtok(buf, " ");
89
90 cout << "Server: " << buffer << endl << flush;
91 if (stricmp(cmd, "PING") == 0) {
92 char *timestamp;
93 timestamp = strtok(NULL, "");
94 raw("PONG %s", timestamp);
95 } else if (strncmp(cmd, "NICK", 4) == 0) {
96 if (buffer[0] == ':')
97 {
98 aClient *tempPtr;
99 if (tempPtr = find((source + 1)))
100 {
101 char *nick;
102 nick = strtok(NULL, " ");
103 tempPtr->setNick(nick);
104 }
105 }
106 else
107 {
108 char *nick;
109 aClient *newuser;
110 nick = strtok(NULL, " ");
111 newuser = new aClient(nick);
112 clients.insertAtBack(newuser);
113 delete newuser;
114 }
115 } else if (stricmp(cmd, "QUIT") == 0) {
116 aClient *quitter;
117 if (quitter = find(source + 1))
118 clients.remove(quitter);
119 if (quitter = findplayer(source + 1))
120 players.remove(quitter);
121
122 } else if (stricmp(cmd, "PRIVMSG") == 0) {
123 char *rest, *dest;
124 dest = strtok(NULL, " ");
125 rest = strtok(NULL, "");
126 if (strnicmp(dest, s_GameServ, strlen(s_GameServ)) == 0)
127 gameserv(source, rest);
128 else if (stricmp(dest, c_Forest) == 0)
129 forest(source, rest);
130 } else if (stricmp(cmd, "JOIN") == 0) {
131 char *channel;
132 channel = strtok(NULL, " ");
133 if (stricmp(channel, c_Forest) == 0 && is_playing(source + 1))
134 raw(":%S MODE %s +v %s", c_Forest, (source + 1));
135 } else {
136 // cout << "Unrecognized Message: cmd = " << cmd << setw(30) << "source = " <<
137 // source << endl;
138 }
139 }
140 save_gs_dbase();
141 delete_monsters();
142 delete_masters();
143
144 printf("<CLOSED>\n");
145 close(sock);
146 unload_config_file();
147 return 0;
148 }
149
150 aClient *find(char *nick)
151 {
152 return findbynick(nick);
153 }
154
155 aClient *find(const char *nick)
156 {
157 return findbynick(nick);
158 }
159
160
161 aClient *findbynick(char *nick)
162 {
163 ListNode <aClient> *newPtr;
164 newPtr = clients.First();
165
166 aClient *client = NULL;
167
168 while (newPtr)
169 {
170 client = newPtr->getData();
171 if (stricmp(client->getNick(), nick) == 0)
172 return client;
173 client = NULL;
174 newPtr = newPtr->Next();
175 }
176 return client;
177 }
178
179 aClient *findplayer(const char *name)
180 {
181 ListNode <aClient> *newPtr;
182 Player *p = NULL;
183
184 for (newPtr = players.First(); newPtr; newPtr = newPtr->Next())
185 {
186 p = newPtr->getData()->stats;
187 if (stricmp(p->name, name) == 0)
188 return newPtr->getData();
189 p = NULL;
190 }
191 return NULL;
192 }
193
194 aClient *findbynick(const char *nick)
195 {
196 ListNode <aClient> *newPtr;
197 newPtr = clients.First();
198
199 aClient *client = NULL;
200
201 while (newPtr)
202 {
203 client = newPtr->getData();
204 if (stricmp(client->getNick(), nick) == 0)
205 return client;
206 client = NULL;
207 newPtr = newPtr->Next();
208 }
209 return client;
210 }
211