]> jfr.im git - irc/gameservirc.git/blame - gameserv/tcpclient.cpp
do_identify is now fixed (i hope). All previous do_identify commits were buggy and...
[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>
23#include <iomanip.h>
24#include <time.h>
25#include <stdlib.h>
26
27
28int sock;
29List<aClient> clients;
30
31int main(int argc, char *argv[])
32{
33 char buffer[1024], buf[1024], input[1024], uplink[80], kb[1024];
34 int connected = 1;
35 char *cmd, *source = NULL;
36 srand(time(NULL));
37
38 load_config_file();
39
40 if (argc == 1) {
41 argc = 3;
42 argv[1] = remoteserver;
43 argv[2] = remoteport;
44 }
45 if (argc != 3) {
46 fprintf(stderr,"Usage: tcpclient host port\n");
47 fprintf(stderr,"where host is the machine which is running the\n");
48 fprintf(stderr,"tcpserver program, and port is the port it is\n");
49 fprintf(stderr,"listening on.\n");
50 exit(EXIT_FAILURE);
51 }
52 ignore_pipe();
53 sock = make_connection(argv[2], SOCK_STREAM, argv[1]);
54 if (sock == -1) {
55 fprintf(stderr,"make_connection failed.\n");
56 unload_config_file();
57 return -1;
58 }
59
60 raw("PROTOCTL NICKv2 VHP");
61 raw("PASS :%s", remotepass);
62 raw("SERVER %s 1 :Testing Server", servername);
63 raw("NICK %S 1 %d %S %s %s %d +owghraAxNt %s :GameServ", time(NULL), gshost,
64 servername, time(NULL), gshost);
65 raw(":%S JOIN %s", c_Forest);
66 raw(":%S MODE %s +o %S", c_Forest);
67 raw(":%S MODE %s +ntm", c_Forest);
68
69 sock_gets(sock,buffer,sizeof(buffer)-1); /* -1 added thanks to
70 David Duchene <dave@ltd.com> for pointing out the possible
71 buffer overflow resulting from the linefeed added below. */
72
73
74 printf("Server: %s\n",buffer);
75 init_monsters();
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
0a1518fa 91 cout << "Server: " << buffer << endl << flush;
85ce9d3e 92 if (stricmp(cmd, "PING") == 0) {
0a1518fa 93 char *timestamp;
94 timestamp = strtok(NULL, "");
95 raw("PONG %s", timestamp);
85ce9d3e 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);
0a1518fa 120 if (quitter = findplayer(source + 1))
121 players.remove(quitter);
85ce9d3e 122
123 } else if (stricmp(cmd, "PRIVMSG") == 0) {
124 char *rest, *dest;
125 dest = strtok(NULL, " ");
126 rest = strtok(NULL, "");
127 if (stricmp(dest, 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 printf("<CLOSED>\n");
142 close(sock);
143 unload_config_file();
144 return 0;
145}
146
147aClient *find(char *nick)
148{
149 return findbynick(nick);
150}
151
152aClient *find(const char *nick)
153{
154 return findbynick(nick);
155}
156
157
158aClient *findbynick(char *nick)
159{
160 ListNode <aClient> *newPtr;
161 newPtr = clients.First();
162
163 aClient *client = NULL;
164
165 while (newPtr)
166 {
167 client = newPtr->getData();
168 if (stricmp(client->getNick(), nick) == 0)
169 return client;
170 client = NULL;
171 newPtr = newPtr->Next();
172 }
173 return client;
174}
175
0a1518fa 176aClient *findplayer(const char *name)
177{
178 ListNode <aClient> *newPtr;
179 Player *p = NULL;
180
181 for (newPtr = players.First(); newPtr; newPtr = newPtr->Next())
182 {
183 p = newPtr->getData()->stats;
184 if (stricmp(p->name, name) == 0)
185 return newPtr->getData();
186 p = NULL;
187 }
188 return NULL;
189}
190
85ce9d3e 191aClient *findbynick(const char *nick)
192{
193 ListNode <aClient> *newPtr;
194 newPtr = clients.First();
195
196 aClient *client = NULL;
197
198 while (newPtr)
199 {
200 client = newPtr->getData();
201 if (stricmp(client->getNick(), nick) == 0)
202 return client;
203 client = NULL;
204 newPtr = newPtr->Next();
205 }
206 return client;
207}
208