All pastes #20598 Raw Edit

wegstar

public text v1 · immutable
#20598 ·published 2005-08-21 02:09 UTC
rendered paste body
// Relay.cpp : Defines the entry point for the DLL application.
//

#include "bzfsAPI.h"
#include "winsock2.h" //it hurts.. but...
#include <iostream>

BZ_GET_PLUGIN_VERSION

// ------------- CONFIG
	int privmsg = 1; //specify whether it will privmsg you 
	std::string your_nick = "your_nick"; // put your nick here if it's to privmsg you	
	std::string nick = "Relay"; // put the "bot"'s nick
	std::string channel = "#flood"; // the channel that it is going to join

int fd = socket(AF_INET, SOCK_STREAM, 0);	

class RelayHandler : public bz_EventHandler
{
public:
	RelayHandler();
	virtual ~RelayHandler();

	virtual void process ( bz_EventData *eventData );

	virtual bool autoDelete ( void ) { return false;} // this will be used for more then one event

	

};
RelayHandler relay;

BZF_PLUGIN_CALL int bz_Load ( const char* commandLine )
{
	bz_debugMessage(1,"Relay plugin loaded");
	return 0;
}

BZF_PLUGIN_CALL int bz_Unload ( void )
{
	bz_debugMessage(1,"Relay plugin unloaded");
	return 0;
}
RelayHandler::RelayHandler()
{//set up winsock and irc connection in the constructor
       
		WSADATA wsaData; 		
 		WSAStartup(MAKEWORD(1, 1), &wsaData);	

	struct hostent *host;
	struct sockaddr_in dest_addr;
	dest_addr.sin_family = AF_INET;
	// ------------- CONFIG
	dest_addr.sin_addr.s_addr = inet_addr("irc.freenode.net");//address of irc server
	dest_addr.sin_port = htons(6667);//port of irc server
	// ------------- END CONFIG
	memset(&(dest_addr.sin_zero), '\0', 8);

	
	connect(fd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr));
	

	std::string str1 = "NICK " + nick + "\n";
	std::string str2 = "USER foo something.com something.com " + nick + "\n";
	std::string str3 = "JOIN" + channel + "\n";

	send(fd, str1.c_str(), strlen(str1.c_str()),0);
	send(fd, str2.c_str(), strlen(str2.c_str()),0);
	send(fd, str3.c_str(), strlen(str3.c_str()),0); 

	
}

RelayHandler::~RelayHandler()
{
}
void RelayHandler::process ( bz_EventData *eventData )
{
	switch (eventData->eventType)
	{
		case bz_eChatMessageEvent:
		{
			bz_ChatEventData *chat = (bz_ChatEventData*)eventData;

			bz_PlayerRecord* speaker = bz_getPlayerByIndex ( chat->from );
			
			std::string message = chat->message.c_str(); //um... yeah
			std::string player = speaker->callsign.c_str();

			std::string subtotal = player + ": " + message;
			if (privmsg){
				std::string total = "PRIVMSG " + your_nick + " :" + subtotal + "\n";
				send(fd, total.c_str(), strlen(total.c_str()),0);
			}
			else{
				std::string total = "PRIVMSG " + channel + " :" + subtotal + "\n";
				send(fd, total.c_str(), strlen(total.c_str()),0);
			}

			
		}
		default:
			break;
	

	}
}