rendered paste body#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <iostream>
using namespace std;
void criar(struct ircbot *bot);
void setar(struct ircbot *bot);
void compare(struct ircbot *bot);
void sendmsg(struct ircbot *bot,char msg[1024],...);
struct ircbot
{
char server[25], nick[10];
SOCKET sock;
unsigned short int port;
bool conectado;
};
void setar(struct ircbot *bot)
{
strcpy(bot->nick,"NyX");
strcpy(bot->server,"irc.freenode.org");
bot->port = 6667;
}
void criar(struct ircbot *bot)
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
struct sockaddr_in vitima;
struct hostent *he;
wVersionRequested = MAKEWORD( 2, 0 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
exit(0);
}
if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 0 )
{
WSACleanup();
exit(0);
}
he = gethostbyname(bot->server);
if (!he)
{
perror("gethostbyname");
getchar();
}
bot->sock = socket(AF_INET,SOCK_STREAM,0);
if(bot->sock < 0)
{
perror("socket");
getchar();
}
vitima.sin_family = AF_INET;
vitima.sin_port = htons(bot->port);
memcpy(&vitima.sin_addr.s_addr,he->h_addr,4);
memset(&(vitima.sin_zero), '\0',8);
if(connect(bot->sock,(struct sockaddr*)&vitima,sizeof(struct sockaddr)) < 0)
{
perror("connect");
getchar();
}
bot->conectado = true;
}
void compare(struct ircbot *bot)
{
char msg2[2048];
if(recv(bot->sock,msg2,sizeof(msg2),0) < 0)
{
perror("recv");
getchar();
}
if(!strstr(msg2,"No Ident response"))
{
sendmsg(bot,"USER %s \"\" \"127.0.0.1\" :%s\r\n",bot->nick,bot->nick);
sendmsg(bot,"NICK %s\r\n",bot->nick);
}
if(!strstr(msg2,"end of /motd"))
{
sendmsg(bot,"JOIN #NyX\r\n");
}
if(!strstr(msg2,"PING"))
{
sendmsg(bot,"PONG :\r\n");
}
memset(&msg2,'\0',sizeof(msg2));
}
void sendmsg(struct ircbot *bot,char *msg,...)
{
if(send(bot->sock,msg,sizeof(msg),0) < 0)
{
perror("send");
printf("erro");
getchar();
}
}
int main()
{
struct ircbot bot;
setar(&bot);
criar(&bot);
while(bot.conectado != false)
{
compare(&bot);
}
}