Renegade Public Forums
C&C: Renegade --> Dying since 2003™, resurrected in 2024!
Home » Renegade Discussions » Mod Forum » Learn2IRC!
Learn2IRC! [message #420116] Wed, 17 February 2010 05:55 Go to next message
reborn is currently offline  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 #420120 is a reply to message #420116] Wed, 17 February 2010 08:15 Go to previous messageGo to next message
Sladewill is currently offline  Sladewill
Messages: 291
Registered: January 2009
Location: United Kingdom
Karma: 0
Recruit

Is this for renegade because looping is not a good way to do it, it will freeze the console. Use Timer events to check and pong

FT-Owners - Sladewill,Snazy2007,Willdy
http://FT-Gaming.com for more info...
Re: Learn2IRC! [message #420121 is a reply to message #420116] Wed, 17 February 2010 08:21 Go to previous messageGo to next message
reborn is currently offline  reborn
Messages: 3231
Registered: September 2004
Location: uk - london
Karma: 0
General (3 Stars)
This has it's own thread. Wink


[Updated on: Wed, 17 February 2010 08:22]

Report message to a moderator

Re: Learn2IRC! [message #420135 is a reply to message #420116] Wed, 17 February 2010 10:56 Go to previous messageGo to next message
Sir Kane
Messages: 1701
Registered: March 2003
Location: Angerville
Karma: 0
General (1 Star)
Must be because you're using std::.

Proud N9500 and proud N6270 user. Creator of the IEE libraries (original bhs.dll) and the RB series software.
http://n00bstories.com/image.fetch.php?id=1189992501http://www.n00bstories.com/image.fetch.php?id=1257492907
Re: Learn2IRC! [message #420136 is a reply to message #420135] Wed, 17 February 2010 11:09 Go to previous messageGo to next message
Gen_Blacky is currently offline  Gen_Blacky
Messages: 3250
Registered: September 2006
Karma: 1
General (3 Stars)
Sir Kane wrote on Wed, 17 February 2010 11:56

Must be because you're using std::.


lol


http://s18.postimage.org/jc6qbn4k9/bricks3.png
Re: Learn2IRC! [message #420158 is a reply to message #420135] Wed, 17 February 2010 14:06 Go to previous messageGo to next message
reborn is currently offline  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 Go to previous messageGo to next message
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 #420162 is a reply to message #420160] Wed, 17 February 2010 15:24 Go to previous messageGo to next message
reborn is currently offline  reborn
Messages: 3231
Registered: September 2004
Location: uk - london
Karma: 0
General (3 Stars)
raven wrote on Wed, 17 February 2010 17:13

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


Thanks raven In Love

Where did you pick that knowledge up?



Re: Learn2IRC! [message #420163 is a reply to message #420116] Wed, 17 February 2010 15:28 Go to previous messageGo to next message
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 Wink

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 Go to previous messageGo to next message
Omar007 is currently offline  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 Sad (rarely it does Huh )
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 Razz

And sorry for sort of hijacking your topic but i have the same problem xD


http://tiberiumredux.omarpakker.nl/Old Unused Parts/Plaatjes/PromoteBanner_Hades_small.jpg

[Updated on: Thu, 18 February 2010 01:38]

Report message to a moderator

Re: Learn2IRC! [message #420184 is a reply to message #420116] Thu, 18 February 2010 00:00 Go to previous messageGo to next message
Sir Kane
Messages: 1701
Registered: March 2003
Location: Angerville
Karma: 0
General (1 Star)
int len;
if ((len = sprintf(sendData, "NICK %s\r\nUSER %s 0 0 :OTROSSB\r\n", nick, nick)) != -1)
Sock.SendData(sendData, len);


Proud N9500 and proud N6270 user. Creator of the IEE libraries (original bhs.dll) and the RB series software.
http://n00bstories.com/image.fetch.php?id=1189992501http://www.n00bstories.com/image.fetch.php?id=1257492907
Re: Learn2IRC! [message #420187 is a reply to message #420184] Thu, 18 February 2010 02:12 Go to previous messageGo to next message
reborn is currently offline  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 Sad (rarely it does Huh )
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 Razz

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... Smile

http://game-maps.net/staff/reborn/rebot.JPG



Re: Learn2IRC! [message #420188 is a reply to message #420116] Thu, 18 February 2010 04:49 Go to previous messageGo to next message
reborn is currently offline  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;
}



http://www.game-maps.net/staff/reborn/rebot2.JPG



[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 Go to previous messageGo to next message
Omar007 is currently offline  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 Sarcasm ). 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 Huh

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);
}


http://tiberiumredux.omarpakker.nl/Old Unused Parts/Plaatjes/PromoteBanner_Hades_small.jpg

[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 Go to previous messageGo to next message
reborn is currently offline  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. Big Ups



[Updated on: Thu, 18 February 2010 07:01]

Report message to a moderator

Re: Learn2IRC! [message #420197 is a reply to message #420195] Thu, 18 February 2010 07:10 Go to previous messageGo to next message
Omar007 is currently offline  Omar007
Messages: 1711
Registered: December 2007
Location: Amsterdam
Karma: 0
General (1 Star)
reborn wrote on Thu, 18 February 2010 14:59

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. Big Ups

np Thumbs Up
And yea you did indeed. I think i just read over it, my fault xD


http://tiberiumredux.omarpakker.nl/Old Unused Parts/Plaatjes/PromoteBanner_Hades_small.jpg
Re: Learn2IRC! [message #420198 is a reply to message #420116] Thu, 18 February 2010 07:12 Go to previous messageGo to next message
Sladewill is currently offline  Sladewill
Messages: 291
Registered: January 2009
Location: United Kingdom
Karma: 0
Recruit

you do know the IRC accepts ":"
so join :#channelname
may work


FT-Owners - Sladewill,Snazy2007,Willdy
http://FT-Gaming.com for more info...
Re: Learn2IRC! [message #420200 is a reply to message #420116] Thu, 18 February 2010 07:48 Go to previous messageGo to next message
Omar007 is currently offline  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 Thumbs Up


http://tiberiumredux.omarpakker.nl/Old Unused Parts/Plaatjes/PromoteBanner_Hades_small.jpg

[Updated on: Thu, 18 February 2010 13:52]

Report message to a moderator

Re: Learn2IRC! [message #420201 is a reply to message #420116] Thu, 18 February 2010 07:49 Go to previous messageGo to next message
reborn is currently offline  reborn
Messages: 3231
Registered: September 2004
Location: uk - london
Karma: 0
General (3 Stars)
http://www.irchelp.org/irchelp/rfc/rfc.html

Great source of help for anyone following...



Re: Learn2IRC! [message #420203 is a reply to message #420116] Thu, 18 February 2010 08:05 Go to previous messageGo to next message
reborn is currently offline  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 #420211 is a reply to message #420116] Thu, 18 February 2010 11:03 Go to previous messageGo to next message
danpaul88 is currently offline  danpaul88
Messages: 5795
Registered: June 2004
Location: England
Karma: 0
General (5 Stars)
You should always wait until you have received the 001 message code before attempting to join any channels. Something I learnt the hard way...

http://steamsignature.com/card/1/76561197975867233.png
Re: Learn2IRC! [message #420214 is a reply to message #420116] Thu, 18 February 2010 11:28 Go to previous messageGo to next message
Sladewill is currently offline  Sladewill
Messages: 291
Registered: January 2009
Location: United Kingdom
Karma: 0
Recruit

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...
Re: Learn2IRC! [message #420235 is a reply to message #420116] Thu, 18 February 2010 14:53 Go to previous message
Omar007 is currently offline  Omar007
Messages: 1711
Registered: December 2007
Location: Amsterdam
Karma: 0
General (1 Star)
OMG AWESOMENESS!!!!!!!!!!!!!!!!!!!!! Very Happy Surprised Big Grin

IT WORKS MOEHAHAHA Razz

Pretty hard to get it working. Took me 4 days Sarcasm
It now responds to !about command Razz

I really learned alot with this Big Grin

Oh and DanPaul88: i used 004 message; nick modes message Wink

This should help more people with the message numbers: http://www.mirc.net/raws/

And 1 more tip: make sure you check the 'strlen' and not the 'sizeof' when sending data Wink
It will get you (IP) banned on the server :V (luckily only 1 day; n00bstories IRC)
Closing Link: [IP] Z:Lined (Connect flooding, one day gzline)


http://tiberiumredux.omarpakker.nl/Old Unused Parts/Plaatjes/PromoteBanner_Hades_small.jpg

[Updated on: Thu, 18 February 2010 16:11]

Report message to a moderator

Previous Topic: .lsd and .ldd .dbb... What should I use?
Next Topic: How do i remove...
Goto Forum:
  


Current Time: Thu May 23 06:52:20 MST 2024

Total time taken to generate the page: 6.56319 seconds