Home » Renegade Discussions » Mod Forum » Learn2IRC!
Learn2IRC! [message #420116] |
Wed, 17 February 2010 05:55 |
|
reborn
Messages: 3231 Registered: September 2004 Location: uk - london
Karma: 0
|
General (3 Stars) |
|
|
I've been trying to connect to an IRC server programitically instead of using an IRC client like mIRC.
I'm not a big fan of IRC, namely because I dont understand it very much, but I've made progress (albeit slowly)...
The following code is a thread that I was trying to make connect to an IRC server...
It gets to "//Gets to here fine with no problems....
" with no issues, however, as you can see, I am truely confused by the protocol and ping/pong especially.
I'm not sure when to expect the ping/pong, so I was just checking for it all the time, I will obviously make the continous ping/pong requests seperate, this thread was just to connect as a proof of concept to build on really...
I'm pretty sure after seeing the server ping me like this: "PING : randomjunk" that I need to respond with "PONG : randomjunk", which is why the pong looks so weird, I am tokenising the ping request to get that randomjunk part and slap it on the end of my pong response...
All the code after, like trying to join channels/set name etc etc make the irc server send me a message about not being registered, until the ping request times out. I am assuming that to be registered, I have to properly respond to ping, and this is where all my issues lay...
Here, is my code, if anyone can help, then I would appreciate it (or just has a working example of connecting, or some detailed information about how the steps the irc is looking for me to do to connect properly). Don't be too mean about the code, it really was just a test to connect...
DWORD WINAPI MainThread( LPVOID lpParam ){
char buf1[1200];
char nick[] = "rebot";
char text1[4096];
int n;
WORD wsver=MAKEWORD(2, 0);
int nret=WSAStartup(wsver, &wsaData);
if(nret != 0){
printf("Startup failed, error code: %d\n",WSAGetLastError());
WSACleanup();
return false;
}
printf("Init success\n");
SOCKET kSock=socket(AF_INET, SOCK_STREAM, 0);
if(kSock == INVALID_SOCKET){
printf("Socket init failed");
return false;
}
printf("Socket initialized\n");
sockaddr_in sin;
sin.sin_port=htons(6668);
sin.sin_addr.s_addr=inet_addr("85.25.143.169");
sin.sin_family=AF_INET;
if(connect(kSock,(sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR){
printf("Startup failed, error code: %d\n",WSAGetLastError());
WSACleanup();
return false;
}
printf("Connection successful!\n\n");
//Gets to here fine with no problems....
n = recv(kSock, buf1, 1200, 0);
if ( n > 0 ) {
printf(">>Server: %s\n",buf1);
if (strstr(buf1,"PING")){
printf("I got a ping, cool!\n");
char* myStringPtr = buf1;
myStringPtr+=6;
char * pch;
pch = strtok (myStringPtr," =\n");
std::vector<std::string> str_Vector;
while (pch != NULL){
std::string strData = pch;
str_Vector.push_back(strData);
pch = strtok (NULL, " =\n");
}
sprintf(text1,"PONG :%s\r\n",str_Vector.at(0).c_str());
send(kSock, text1, sizeof(text1), 0);
printf(">>Client: %s\n",text1);
}
}
else {
printf(">>Server: No Data\n");
}
sprintf(text1, "NICK rebot\r\n");
send(kSock, text1, strlen(text1), 0);
printf(">>Client: %s\n",text1);
n = recv(kSock, buf1, 1200, 0);
if ( n > 0 ) {
printf(">>Server: %s\n",buf1);
if (strstr(buf1,"PING")){
printf("I got a ping, cool!\n");
char* myStringPtr = buf1;
myStringPtr+=6;
char * pch;
pch = strtok (myStringPtr," =\n");
std::vector<std::string> str_Vector;
while (pch != NULL){
std::string strData = pch;
str_Vector.push_back(strData);
pch = strtok (NULL, " =\n");
}
sprintf(text1,"PONG :%s\r\n",str_Vector.at(0).c_str());
send(kSock, text1, sizeof(text1), 0);
printf(">>Client: %s\n",text1);
}
}
else {
printf(">>Server: No Data\n");
}
/*
// ping and pong continuous, will be moved to own thread
while (1) {
n = recv(kSock, buf1, 1200, 0);
if ( n > 0 ) {
printf(">>Server: %s\n",buf1);
if (strstr(buf1,"PING")){
printf("I got a ping, cool!\n");
char* myStringPtr = buf1;
myStringPtr+=6;
char * pch;
pch = strtok (myStringPtr," =\n");
std::vector<std::string> str_Vector;
while (pch != NULL){
std::string strData = pch;
str_Vector.push_back(strData);
pch = strtok (NULL, " =\n");
}
sprintf(text1,"PONG :%s\n",str_Vector.at(0).c_str());
send(kSock, text1, sizeof(text1), 0);
printf(">>Client: %s\n",text1);
}
}
else {
printf(">>Server: No Data\n");
}*/
return 1;
}
|
|
|
|
|
|
|
Re: Learn2IRC! [message #420158 is a reply to message #420135] |
Wed, 17 February 2010 14:06 |
|
reborn
Messages: 3231 Registered: September 2004 Location: uk - london
Karma: 0
|
General (3 Stars) |
|
|
Sir Kane wrote on Wed, 17 February 2010 12:56 | Must be because you're using std::.
|
Come on man... You've clearly read it, you're better than I am, can't you just help?
|
|
|
Re: Learn2IRC! [message #420160 is a reply to message #420116] |
Wed, 17 February 2010 15:13 |
raven
Messages: 595 Registered: January 2007 Location: Toronto, Ontario
Karma: 0
|
Colonel |
|
|
In order to "register" your connection with the server you have to send the following data in addition to ping repsonse:
NICK nickname
USER username 0 0 :real name of client
then you will be allowed to issue raw IRC commands such as join and privmsg
-Jelly Administrator
-Exodus Administrator
|
|
|
|
Re: Learn2IRC! [message #420163 is a reply to message #420116] |
Wed, 17 February 2010 15:28 |
raven
Messages: 595 Registered: January 2007 Location: Toronto, Ontario
Karma: 0
|
Colonel |
|
|
5 years of admining of an IRC server and development of many PHP and C++ IRC bots for starters
google was my friend
-Jelly Administrator
-Exodus Administrator
[Updated on: Wed, 17 February 2010 15:28] Report message to a moderator
|
|
|
Re: Learn2IRC! [message #420164 is a reply to message #420116] |
Wed, 17 February 2010 15:41 |
|
Omar007
Messages: 1711 Registered: December 2007 Location: Amsterdam
Karma: 0
|
General (1 Star) |
|
|
LOL reborn we are working on the same thing, walking into the same problem!!
I already had ravens idea but it has already lost connection when it enters the loop.
This is what i did. I put it in 1 statement. That should work right?
memset(sendData, 0, 255);
sprintf(sendData, "NICK %s\r\nUSER %s 0 0 :OTROSSB\r\n", nick, nick);
Sock.SendData(sendData, strlen(sendData));
Also it doesnt show in the channel (rarely it does )
And does it also crashes your FDS on 'quit'??
EDIT: Oh and i think my message parser is OK too but i cant test that when it loses connection xD
Reborn, i would use a hostname instead of static IP if you want more 'normal' users to use it easily
And sorry for sort of hijacking your topic but i have the same problem xD
[Updated on: Thu, 18 February 2010 01:38] Report message to a moderator
|
|
|
|
Re: Learn2IRC! [message #420187 is a reply to message #420184] |
Thu, 18 February 2010 02:12 |
|
reborn
Messages: 3231 Registered: September 2004 Location: uk - london
Karma: 0
|
General (3 Stars) |
|
|
Omar007 wrote on Wed, 17 February 2010 17:41 | LOL reborn we are working on the same thing, walking into the same problem!!
I already had ravens idea but it has already lost connection when it enters the loop.
This is what i did. I put it in 1 statement. That should work right?
memset(sendData, 0, 255);
sprintf(sendData, "NICK %s\r\nUSER %s 0 0 :OTROSSB\r\n", nick, nick);
Sock.SendData(sendData, strlen(sendData));
Also it doesnt show in the channel (rarely it does )
And does it also crashes your FDS on 'quit'??
EDIT: Oh and i think my message parser is OK too but i cant test that when it loses connection xD
Reborn, i would use a hostname instead of static IP if you want more 'normal' users to use it easily
And sorry for sort of hijacking your topic but i have the same problem xD
|
When I've done mine, I will show you.
Sir Kane wrote on Thu, 18 February 2010 02:00 | int len;
if ((len = sprintf(sendData, "NICK %s\r\nUSER %s 0 0 :OTROSSB\r\n", nick, nick)) != -1)
Sock.SendData(sendData, len);
|
Thank you, that's kind.
After the tips in this thread, I am finally getting somewhere...
|
|
|
Re: Learn2IRC! [message #420188 is a reply to message #420116] |
Thu, 18 February 2010 04:49 |
|
reborn
Messages: 3231 Registered: September 2004 Location: uk - london
Karma: 0
|
General (3 Stars) |
|
|
Ok, so I got a little further... Basically the IRC server sends a shit tonne of information when you choose your nick (see screenshot below). However, after choosing my nick and receiving all the crap from the server, I try to join a channel, I send the command, and try to receive the response, but it doesn't actually respond, and I don't join the channel.
DWORD WINAPI MainThread( LPVOID lpParam ){
char buf1[4096];
char nick[] = "rebot";
char text1[4096];
int n;
WORD wsver=MAKEWORD(2, 0);
int nret=WSAStartup(wsver, &wsaData);
if(nret != 0){
printf("Startup failed, error code: %d\n",WSAGetLastError());
WSACleanup();
return false;
}
printf("Init success\n");
SOCKET kSock=socket(AF_INET, SOCK_STREAM, 0);
if(kSock == INVALID_SOCKET){
printf("Socket init failed");
return false;
}
printf("Socket initialized\n");
sockaddr_in sin;
sin.sin_port=htons(6668);
sin.sin_addr.s_addr=inet_addr("85.25.143.169");
sin.sin_family=AF_INET;
if(connect(kSock,(sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR){
printf("Startup failed, error code: %d\n",WSAGetLastError());
WSACleanup();
return false;
}
printf("Connection successful!\n\n");
n = recv(kSock, buf1, 4096, 0);
if ( n > 0 ) {
printf(">>Server: %s\n",buf1);
}
else {
printf(">>Server: No Data\n");
}
sprintf(text1, "NICK rebot2\r\nUSER rebot2 0 0 :rebot2\r\n");
send(kSock, text1, strlen(text1), 0);
printf(">>Client: %s\n",text1);
n = recv(kSock, buf1, 4096, 0);
if ( n > 0 ) {
printf(">>Server: %s\n",buf1);
if (strstr(buf1,"PING")){
printf("I got a ping, cool!\n");
char* myStringPtr = buf1;
myStringPtr+=6;
char * pch;
pch = strtok (myStringPtr," =\n");
std::vector<std::string> str_Vector;
while (pch != NULL){
std::string strData = pch;
str_Vector.push_back(strData);
pch = strtok (NULL, " =\n");
}
sprintf(text1,"PONG :%s\r\n",str_Vector.at(0).c_str());
send(kSock, text1, sizeof(text1), 0);
printf(">>Client: %s\n",text1);
}
}
else {
printf(">>Server: No Data\n");
}
n = recv(kSock, buf1, 4096, 0);
if ( n > 0 ) {
printf(">>Server: %s\n",buf1);
}
else {
printf(">>Server: No Data\n");
}
n = recv(kSock, buf1, 4096, 0);
if ( n > 0 ) {
printf(">>Server: %s\n",buf1);
}
else {
printf(">>Server: No Data\n");
}
n = recv(kSock, buf1, 4096, 0);
if ( n > 0 ) {
printf(">>Server: %s\n",buf1);
}
else {
printf(">>Server: No Data\n");
}
n = recv(kSock, buf1, 4096, 0);
if ( n > 0 ) {
printf(">>Server: %s\n",buf1);
}
else {
printf(">>Server: No Data\n");
}
sprintf(text1,"JOIN #lobby\r\n");
send(kSock, text1, sizeof(text1), 0);
printf(">>Client: %s\n",text1);
n = recv(kSock, buf1, 4096, 0);
if ( n > 0 ) {
printf(">>Server: %s\n",buf1);
}
else {
printf(">>Server: No Data\n");
}
/*
sprintf(text1,"PRIVMSG #lobby :Test Message\r\n");
send(kSock,text1,sizeof(text1),0);
printf(">>Client: %s\n",text1);
n = recv(kSock, buf1, 4096, 0);
if ( n > 0 ) {
printf(">>Server: %s\n",buf1);
}
else {
printf(">>Server: No Data\n");
}
*/
/*
// ping and pong, will obviously be moved...
while (1) {
recv( kSock,buf,255,0);
if (strstr(buf,"PING")) {
printf("Server sent PING\n");
send(kSock,"PONG :\r\n",128,0);
printf("Replying with PONG\n");
}
else{
printf(">>Server: %s\n",buf);
}
}
*/
return 1;
}
[Updated on: Thu, 18 February 2010 04:52] Report message to a moderator
|
|
|
Re: Learn2IRC! [message #420191 is a reply to message #420116] |
Thu, 18 February 2010 05:37 |
|
Omar007
Messages: 1711 Registered: December 2007 Location: Amsterdam
Karma: 0
|
General (1 Star) |
|
|
That is indeed alot of data xD
It is weird, your channel join code looks OK and mine joins the channel (now and then ). There is only 1 channel join command and we both wrote it the same (well beside that you only end it with '\n' and i have '\r\n' would that matter?) it should be right
Also why not use a loop for the input check?? Writing the code 4 times seems pretty useless :V
And what if there is even more input??
IE:
while(1)
{
if(recv(kSock, buf1, 4096, 0) <= 0)
{
printf(">>Server: No Data\n");
break;
}
printf(">>Server: %s\n", receiveData);
}
[Updated on: Thu, 18 February 2010 05:39] Report message to a moderator
|
|
|
Re: Learn2IRC! [message #420195 is a reply to message #420116] |
Thu, 18 February 2010 06:59 |
|
reborn
Messages: 3231 Registered: September 2004 Location: uk - london
Karma: 0
|
General (3 Stars) |
|
|
Thanks for the input, I appreciate that... I had no intention of leaving it like though, lol... I just want it to join the chan and and send a message, then I will start breaking it down to seperate functions etc etc.
I do end my JOIN with \r\n though, not sure what you mean by that?
Thank you.
[Updated on: Thu, 18 February 2010 07:01] Report message to a moderator
|
|
|
|
|
Re: Learn2IRC! [message #420200 is a reply to message #420116] |
Thu, 18 February 2010 07:48 |
|
Omar007
Messages: 1711 Registered: December 2007 Location: Amsterdam
Karma: 0
|
General (1 Star) |
|
|
According to the IRC RFC1459 it doesnt for the command JOIN, unless you use the nick first (see last option in quote).
According to that the join command is written as:
Quote: | JOIN #foobar ; join channel #foobar.
JOIN &foo fubar ; join channel &foo using key "fubar".
JOIN #foo,&bar fubar ; join channel #foo using key "fubar"
and &bar using no key.
JOIN #foo,#bar fubar,foobar ; join channel #foo using key "fubar".
and channel #bar using key "foobar".
JOIN #foo,#bar ; join channels #foo and #bar.
:WiZ JOIN #Twilight_zone ; JOIN message from WiZ
|
I'll update my post if it did work but i doubt it.
EDIT/UPDATE: Doesnt changes ahing for me. Reborn??
EDIT2: I just found out that the last option is received by all persons on the channel. It's not a way to join a channel. Using ':' in a JOIN is there for wrong for certain
[Updated on: Thu, 18 February 2010 13:52] Report message to a moderator
|
|
|
|
Re: Learn2IRC! [message #420203 is a reply to message #420116] |
Thu, 18 February 2010 08:05 |
|
reborn
Messages: 3231 Registered: September 2004 Location: uk - london
Karma: 0
|
General (3 Stars) |
|
|
I'm going away for a weekend break, will see if I can get anywhere when I get back.
|
|
|
|
Re: Learn2IRC! [message #420214 is a reply to message #420116] |
Thu, 18 February 2010 11:28 |
|
Reborn you should try using mIRC to connect as a service it will help you to understand the way IRC works. It helped me a lot when i made a IRC bot in C++ and VB
FT-Owners - Sladewill,Snazy2007,Willdy
http://FT-Gaming.com for more info...
|
|
|
|
Goto Forum:
Current Time: Tue Dec 31 21:23:53 MST 2024
Total time taken to generate the page: 0.01445 seconds
|