All pastes #15575 Raw Edit

Mine

public text v1 · immutable
#15575 ·published 2005-06-22 18:53 UTC
rendered paste body
#include <string>
#include <iostream>

#ifdef _WIN32
#include <winsock.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#endif

#include <unistd.h>
#include <cerrno>

class Exception : std::exception
{   
    public:
    std::string message;
    
    Exception(std::string msg) throw() : std::exception()
    {
        message = msg;
    }
    
    virtual ~Exception() throw()
    {
    }    
};    

class IRCBot
{
    private:
    int sd;
    
	public:
 	static const unsigned int EVENT_LOGIN = 1;
	    
	void connect(std::string host, unsigned int port = 6667)
	{
		struct hostent *ip;
		struct sockaddr_in addr;
		
#ifdef _WIN32
		WSADATA wsaData;
		if(WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
		{
		    throw Exception("Error in WSAStartup()");
		}
#endif
	    
	    // résolution dns
		if(!(ip = gethostbyname(host.c_str())))
		{
		    throw Exception("unknow host '"+host+"'");
		}    

		addr.sin_family = AF_INET;
		memcpy((char *) &addr.sin_addr.s_addr, ip->h_addr_list[0], ip->h_length);
		addr.sin_port = htons(port);

		if((sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
		{
			throw Exception("Cannot open socket");;
		}

		if(::connect(sd, (struct sockaddr *) &addr, sizeof(addr)) < 0);
		{
			throw Exception("Error in connect()");
		}
	}
	
	void login(std::string nick, std::string name, std::string user = "")
	{
 		if(user == "")
 		{
 		    user = nick;
 		}
 		
	    raw("USER "+user+" 0 *:"+name);
	    raw("NICK "+nick);
	    
	    waitFor(EVENT_LOGIN);
	}
	
	void waitFor(unsigned int eventType)
	{
	}
	
	void loop()
	{
	}    
	
	void raw(std::string data)
	{
	    write(sd, (data+"\r\n").c_str(), data.length()+2);
	} 
	
	void join(std::string channel)
	{
	}    
};