All pastes #816473 Raw Edit

Mine

public text v1 · immutable
·published 2007-12-14 20:34 UTC
rendered paste body

#include "stdafx.h"
#pragma comment(lib, "ws2_32.lib")

void Botinfo(std::string *botinfo)
{
	char temp[30];

	std::cout<< "Nick: ";
	std::cin.getline(temp, 30, '\n');
	botinfo[0] = temp;
	std::cout<< "Name: ";
	std::cin.getline(temp, 30, '\n');
	botinfo[1] = temp;
	std::cout<< "Server: ";
	std::cin.getline(temp, 30, '\n');
	botinfo[2] = temp;
	std::cout<< "Channel: ";
	std::cin.getline(temp, 30, '\n');  
	botinfo[3] = temp;
}

unsigned int connecttoserver(std::string server, int port)
{
	SOCKET sock;
	char serverip[20];
	sockaddr_in addr;
	WSADATA wsaData;
	
	if(WSAStartup(MAKEWORD(2,0), &wsaData) != 0)
		std::cout<<"WSA could not be initialized.";

	hostent *dns = gethostbyname(server.c_str());

	sprintf(serverip, "%u.%u.%u.%u",
		(unsigned char) dns->h_addr_list[0][0],	(unsigned char) dns->h_addr_list[0][1],
		(unsigned char) dns->h_addr_list[0][2], (unsigned char) dns->h_addr_list[0][3]);

	addr.sin_family = AF_INET;
	addr.sin_port = htons(port);
	addr.sin_addr.s_addr = inet_addr(serverip);

	sock = socket(AF_INET, SOCK_STREAM, 0);
	if(sock == 0)
		std::cout<<"Socket could not be initialized";

	if(connect(sock, (sockaddr *) &addr, sizeof(addr)) == -1)
		std::cout<<"Connection could not be made";
	
	char temp[10000];
	int x = recv(sock, temp, 10000, 0);
	temp[x] = 0;
	std::cout<< temp;

	return sock;
}

void announcetoserver(SOCKET sock, std::string *botinfo)
{
	std::string nick = "NICK ";
	std::string user = "USER ";

	nick += botinfo[0];
	nick += "\r\n";

	user += botinfo[0];
	user += ' ';
	user += "localhost ";
	user += botinfo[2];
	user += ' ';
	user += botinfo[1];
	user += "\r\n";

	send(sock, nick.c_str(), nick.length(), 0);
	send(sock, user.c_str(), user.length(), 0);

	char temp[10000];
	int x = recv(sock, temp, 10000, 0);
	temp[x] = 0;
	std::cout<< temp;
}

void inareagoing(SOCKET sock, std::string *botinfo)
{
	std::string join = "JOIN ";

	join += botinfo[3];
	join += "\r\n";

	send(sock, join.c_str(), join.length(), 0);

	char temp[10000];
	int x = recv(sock, temp, 10000, 0);
	temp[x] = 0;
	std::cout<< temp;
}

void messagereceive(void *sock)
{
	char goatsecks[1000];
	int x = 0;

	while(1)
	{
		x = recv((SOCKET)sock, goatsecks, 1000, 0);
		
		if(x > 0)
		{
			goatsecks[x] = 0;
			std::cout<< goatsecks << std::endl;
		}
		Sleep(10);
	}
}

void outofnames(SOCKET sock)
{
	char temp[1000];
	std::string goatsecks2;

	while(1)
	{
		std::cin.getline(temp, 100, '\n');
		goatsecks2 = temp;
		goatsecks2 += "\r\n";

		send(sock, goatsecks2.c_str(), goatsecks2.length(), 0);
	}
}


int main()
{
	SOCKET sock;
	std::string botinfo[4];

	Botinfo(botinfo);
	sock = connecttoserver(botinfo[2], 6667);
	announcetoserver(sock, botinfo);
	inareagoing(sock, botinfo);
	_beginthread(messagereceive, 0, (void *)sock);
	outofnames(sock);
	std::cin.get();
	WSACleanup();
	return 0;
}