All pastes #419858 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-04-01 18:07 UTC
rendered paste body
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock.h>
#include <iostream>

using namespace std;

void criar(struct ircbot *bot),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 criar(struct ircbot *bot)
{
     struct sockaddr_in vitima;
     struct hostent *he;
     he = gethostbyname(bot->server);
     if (he < 0)
     {
            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);
     vitima.sin_addr = *((struct in_addr *)he->h_addr);
	 memset(&(vitima.sin_zero), '\0',8);
	 if(connect(bot->sock,(struct sockaddr*)&vitima,sizeof(struct sockaddr)) < 0)
	 {
            perror("connect");
            getchar();
     }
     bot->conectado = true;              
}

void setar(struct ircbot *bot)
{
     strcpy(bot->nick,"NyX");
     strcpy(bot->server,"irc.freenode.org");
     bot->port = 6667;
}

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

void sendmsg(struct ircbot *bot,char msg[1024],...)
{
     if(send(bot->sock,msg,sizeof(msg),0) < 0)
     {
            perror("send");
            getchar();
     }
}

int main()
{
     struct ircbot bot;
     setar(&bot);
     criar(&bot);
     while(bot.conectado != false)
     {
          compare(&bot);
     }
}