]>
jfr.im git - irc/gameservirc.git/blob - gameserv/tcpclient.cpp
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.
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.
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/.
28 List
<aClient
> clients
;
30 int main(int argc
, char *argv
[])
32 char buffer
[1024], buf
[1024], input
[1024], uplink
[80], kb
[1024];
34 char *cmd
, *source
= NULL
;
41 argv
[1] = remoteserver
;
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");
52 sock
= make_connection(argv
[2], SOCK_STREAM
, argv
[1]);
54 fprintf(stderr
,"make_connection failed.\n");
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
);
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. */
73 printf("Server: %s\n",buffer
);
77 if (sock_gets(sock
,buffer
,sizeof(buffer
)) == -1) {
84 source
= strtok(buf
, " ");
85 cmd
= strtok(NULL
, " ");
88 cmd
= strtok(buf
, " ");
90 cout
<< "Server: " << buffer
<< endl
<< flush
;
91 if (stricmp(cmd
, "PING") == 0) {
93 timestamp
= strtok(NULL
, "");
94 raw("PONG %s", timestamp
);
95 } else if (strncmp(cmd
, "NICK", 4) == 0) {
99 if (tempPtr
= find((source
+ 1)))
102 nick
= strtok(NULL
, " ");
103 tempPtr
->setNick(nick
);
110 nick
= strtok(NULL
, " ");
111 newuser
= new aClient(nick
);
112 clients
.insertAtBack(newuser
);
115 } else if (stricmp(cmd
, "QUIT") == 0) {
117 if (quitter
= find(source
+ 1))
118 clients
.remove(quitter
);
119 if (quitter
= findplayer(source
+ 1))
120 players
.remove(quitter
);
122 } else if (stricmp(cmd
, "PRIVMSG") == 0) {
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) {
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));
136 // cout << "Unrecognized Message: cmd = " << cmd << setw(30) << "source = " <<
144 printf("<CLOSED>\n");
146 unload_config_file();
150 aClient
*find(char *nick
)
152 return findbynick(nick
);
155 aClient
*find(const char *nick
)
157 return findbynick(nick
);
161 aClient
*findbynick(char *nick
)
163 ListNode
<aClient
> *newPtr
;
164 newPtr
= clients
.First();
166 aClient
*client
= NULL
;
170 client
= newPtr
->getData();
171 if (stricmp(client
->getNick(), nick
) == 0)
174 newPtr
= newPtr
->Next();
179 aClient
*findplayer(const char *name
)
181 ListNode
<aClient
> *newPtr
;
184 for (newPtr
= players
.First(); newPtr
; newPtr
= newPtr
->Next())
186 p
= newPtr
->getData()->stats
;
187 if (stricmp(p
->name
, name
) == 0)
188 return newPtr
->getData();
194 aClient
*findbynick(const char *nick
)
196 ListNode
<aClient
> *newPtr
;
197 newPtr
= clients
.First();
199 aClient
*client
= NULL
;
203 client
= newPtr
->getData();
204 if (stricmp(client
->getNick(), nick
) == 0)
207 newPtr
= newPtr
->Next();