]> jfr.im git - irc/gameservirc.git/blame - gameserv/tcpclient.cpp
Initial revision
[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
91 cout << "Server: " << buffer << endl;
92 if (stricmp(cmd, "PING") == 0) {
93 char *times;
94 times = strtok(NULL, "");
95 printf("input: PONG %s \n", times);
96 raw("PONG %s ", times);
97 } else if (strncmp(cmd, "NICK", 4) == 0) {
98 if (buffer[0] == ':')
99 {
100 aClient *tempPtr;
101 if (tempPtr = find((source + 1)))
102 {
103 char *nick;
104 nick = strtok(NULL, " ");
105 tempPtr->setNick(nick);
106 }
107 }
108 else
109 {
110 char *nick;
111 aClient *newuser;
112 nick = strtok(NULL, " ");
113 newuser = new aClient(nick);
114 clients.insertAtBack(newuser);
115 delete newuser;
116 }
117 } else if (stricmp(cmd, "QUIT") == 0) {
118 aClient *quitter;
119 if (quitter = find(source + 1))
120 clients.remove(quitter);
121
122 } else if (stricmp(cmd, "PRIVMSG") == 0) {
123 char *rest, *dest;
124 dest = strtok(NULL, " ");
125 rest = strtok(NULL, "");
126 if (stricmp(dest, 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 printf("<CLOSED>\n");
141 close(sock);
142 unload_config_file();
143 return 0;
144}
145
146aClient *find(char *nick)
147{
148 return findbynick(nick);
149}
150
151aClient *find(const char *nick)
152{
153 return findbynick(nick);
154}
155
156
157aClient *findbynick(char *nick)
158{
159 ListNode <aClient> *newPtr;
160 newPtr = clients.First();
161
162 aClient *client = NULL;
163
164 while (newPtr)
165 {
166 client = newPtr->getData();
167 if (stricmp(client->getNick(), nick) == 0)
168 return client;
169 client = NULL;
170 newPtr = newPtr->Next();
171 }
172 return client;
173}
174
175aClient *findbynick(const char *nick)
176{
177 ListNode <aClient> *newPtr;
178 newPtr = clients.First();
179
180 aClient *client = NULL;
181
182 while (newPtr)
183 {
184 client = newPtr->getData();
185 if (stricmp(client->getNick(), nick) == 0)
186 return client;
187 client = NULL;
188 newPtr = newPtr->Next();
189 }
190 return client;
191}
192