PDA

View Full Version : IRC bot problems: will not connect.



R0b0t1
06-11-2008, 03:54 AM
Problem is it hangs and doesn't make a connection.




#include <time.h>
#include <stdio.h>
#include <netdb.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main (int argc, const char * argv[])
{
char buffer[1024];
int sockfd, c, bytes, i = 0;
struct sockaddr_in addr;
struct hostent *host = gethostbyname("irc.freenode.net");

sockfd = socket(PF_INET, SOCK_STREAM, 0);

addr.sin_family = AF_INET;
addr.sin_port = htons(6667);
addr.sin_addr = *((struct in_addr *)host->h_addr);
memset(addr.sin_zero, '\0', sizeof addr.sin_zero);


do{
c = connect(sockfd, (struct sockaddr*)&addr, sizeof addr);
i++;
sleep(5);
}while(c == -1 && i <= 30);

printf("Connected!\n");

char *nick = "NICK Captain_Sarcasm\r\n";
char *user = "USER Captain_Sarcasm * 8:IRC Daemon\r\n";

if(send(sockfd, nick, strlen(nick), 0) == -1) printf("Could not send nick");
if(send(sockfd, user, strlen(user), 0) == -1) printf("Could not send user");

printf("Registered on network: irc.freenode.net\n");

do{
bytes = recv(sockfd, buffer, 1024, 0);
}while(!strstr(buffer, "367"));
memset((void*)buffer, '\0', 1024);

char *join = "JOIN #srl\r\n";
if(send(sockfd, join, strlen(join), 0) == -1) printf("Could not send join");
if(send(sockfd, "PRIVMSG #srl :Connection is registered and working properly\r\n", 47, 0) == -1) printf("Could not send privmsg");

do{
memset((void*)buffer, '\0', 1024);
bytes = recv(sockfd, buffer, 1024, 0);

if(strstr(buffer, "!part"))
{
send(sockfd, "PART #srl\r\n", 11, 0);
}
}while(1);

return 0;

}

boberman
06-11-2008, 02:02 PM
Most protocols have a "handshake" so to speak. I imagine that IRC is no different. Chances are, after the connection it is waiting for you to send some specific piece of data before continuing.

Im not familiar with the IRC protocol so I don't know exactally what you are missing, but that is my guess what is going on.

R0b0t1
06-11-2008, 07:21 PM
No, I know how to send the handshake, but it just isn't connecting.

ShowerThoughts
06-11-2008, 07:27 PM
Robot add me to msn? I need some help on C++ would be really cool.

R0b0t1
06-11-2008, 08:17 PM
NVM, solved.