]> jfr.im git - irc/gameservirc.git/blob - gameserv/tcpclient.cpp
Just syncing the cvs
[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 init_masters();
76 load_gs_dbase();
77 while (connected) {
78 if (sock_gets(sock,buffer,sizeof(buffer)) == -1) {
79 connected = 0;
80 }
81 strcpy(buf, buffer);
82
83 if (buffer[0] == ':')
84 {
85 source = strtok(buf, " ");
86 cmd = strtok(NULL, " ");
87 }
88 else
89 cmd = strtok(buf, " ");
90
91 cout << "Server: " << buffer << endl << flush;
92 if (stricmp(cmd, "PING") == 0) {
93 char *timestamp;
94 timestamp = strtok(NULL, "");
95 raw("PONG %s", timestamp);
96 } else if (strncmp(cmd, "NICK", 4) == 0) {
97 if (buffer[0] == ':')
98 {
99 aClient *tempPtr;
100 if (tempPtr = find((source + 1)))
101 {
102 char *nick;
103 nick = strtok(NULL, " ");
104 tempPtr->setNick(nick);
105 }
106 }
107 else
108 {
109 char *nick;
110 aClient *newuser;
111 nick = strtok(NULL, " ");
112 newuser = new aClient(nick);
113 clients.insertAtBack(newuser);
114 delete newuser;
115 }
116 } else if (stricmp(cmd, "QUIT") == 0) {
117 aClient *quitter;
118 if (quitter = find(source + 1))
119 clients.remove(quitter);
120 if (quitter = findplayer(source + 1))
121 players.remove(quitter);
122
123 } else if (stricmp(cmd, "PRIVMSG") == 0) {
124 char *rest, *dest;
125 dest = strtok(NULL, " ");
126 rest = strtok(NULL, "");
127 if (strnicmp(dest, s_GameServ, strlen(s_GameServ)) == 0)
128 gameserv(source, rest);
129 else if (stricmp(dest, c_Forest) == 0)
130 forest(source, rest);
131 } else if (stricmp(cmd, "JOIN") == 0) {
132 char *channel;
133 channel = strtok(NULL, " ");
134 if (stricmp(channel, c_Forest) == 0 && is_playing(source + 1))
135 raw(":%S MODE %s +v %s", c_Forest, (source + 1));
136 } else {
137 // cout << "Unrecognized Message: cmd = " << cmd << setw(30) << "source = " <<
138 // source << endl;
139 }
140 }
141 save_gs_dbase();
142 delete_monsters();
143 delete_masters();
144
145 printf("<CLOSED>\n");
146 close(sock);
147 unload_config_file();
148 return 0;
149 }
150
151 aClient *find(char *nick)
152 {
153 return findbynick(nick);
154 }
155
156 aClient *find(const char *nick)
157 {
158 return findbynick(nick);
159 }
160
161
162 aClient *findbynick(char *nick)
163 {
164 ListNode <aClient> *newPtr;
165 newPtr = clients.First();
166
167 aClient *client = NULL;
168
169 while (newPtr)
170 {
171 client = newPtr->getData();
172 if (stricmp(client->getNick(), nick) == 0)
173 return client;
174 client = NULL;
175 newPtr = newPtr->Next();
176 }
177 return client;
178 }
179
180 aClient *findplayer(const char *name)
181 {
182 ListNode <aClient> *newPtr;
183 Player *p = NULL;
184
185 for (newPtr = players.First(); newPtr; newPtr = newPtr->Next())
186 {
187 p = newPtr->getData()->stats;
188 if (stricmp(p->name, name) == 0)
189 return newPtr->getData();
190 p = NULL;
191 }
192 return NULL;
193 }
194
195 aClient *findbynick(const char *nick)
196 {
197 ListNode <aClient> *newPtr;
198 newPtr = clients.First();
199
200 aClient *client = NULL;
201
202 while (newPtr)
203 {
204 client = newPtr->getData();
205 if (stricmp(client->getNick(), nick) == 0)
206 return client;
207 client = NULL;
208 newPtr = newPtr->Next();
209 }
210 return client;
211 }
212