]>
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
);
78 if (sock_gets(sock
,buffer
,sizeof(buffer
)) == -1) {
85 source
= strtok(buf
, " ");
86 cmd
= strtok(NULL
, " ");
89 cmd
= strtok(buf
, " ");
91 cout
<< "Server: " << buffer
<< endl
<< flush
;
92 if (stricmp(cmd
, "PING") == 0) {
94 timestamp
= strtok(NULL
, "");
95 raw("PONG %s", timestamp
);
96 } else if (strncmp(cmd
, "NICK", 4) == 0) {
100 if (tempPtr
= find((source
+ 1)))
103 nick
= strtok(NULL
, " ");
104 tempPtr
->setNick(nick
);
111 nick
= strtok(NULL
, " ");
112 newuser
= new aClient(nick
);
113 clients
.insertAtBack(newuser
);
116 } else if (stricmp(cmd
, "QUIT") == 0) {
118 if (quitter
= find(source
+ 1))
119 clients
.remove(quitter
);
120 if (quitter
= findplayer(source
+ 1))
121 players
.remove(quitter
);
123 } else if (stricmp(cmd
, "PRIVMSG") == 0) {
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) {
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));
137 // cout << "Unrecognized Message: cmd = " << cmd << setw(30) << "source = " <<
145 printf("<CLOSED>\n");
147 unload_config_file();
151 aClient
*find(char *nick
)
153 return findbynick(nick
);
156 aClient
*find(const char *nick
)
158 return findbynick(nick
);
162 aClient
*findbynick(char *nick
)
164 ListNode
<aClient
> *newPtr
;
165 newPtr
= clients
.First();
167 aClient
*client
= NULL
;
171 client
= newPtr
->getData();
172 if (stricmp(client
->getNick(), nick
) == 0)
175 newPtr
= newPtr
->Next();
180 aClient
*findplayer(const char *name
)
182 ListNode
<aClient
> *newPtr
;
185 for (newPtr
= players
.First(); newPtr
; newPtr
= newPtr
->Next())
187 p
= newPtr
->getData()->stats
;
188 if (stricmp(p
->name
, name
) == 0)
189 return newPtr
->getData();
195 aClient
*findbynick(const char *nick
)
197 ListNode
<aClient
> *newPtr
;
198 newPtr
= clients
.First();
200 aClient
*client
= NULL
;
204 client
= newPtr
->getData();
205 if (stricmp(client
->getNick(), nick
) == 0)
208 newPtr
= newPtr
->Next();