⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 irc.cpp

📁 .net 方面的开发说明资料。
💻 CPP
字号:
// ========================================================
// Internet Relay Chat protocol implementation
//
// Based on RFC1459 (http://www.faqs.org/rfcs/rfc1459.html)
//
// Design and implementation by:
// - Floris van den Berg
// ========================================================

#pragma warning (disable : 4786)

#include <vector>
#include <string>

#include "..\ETransport.h"
#include "mail.h"

// --------------------------------------------------------

struct ProtocolData {
	TRANSPORT_HANDLE plug;
	char data[8192];
	int data_size;
	int data_pos;
};

// --------------------------------------------------------

static void
NoticeAuthDecode(TRANSPORT_HANDLE transport, std::string line) {
	EpEvent pm_event;
	pm_event.reference_id = 0;
	pm_event.protocol = CLSID_IRC_PROTOCOL;
	pm_event.msg = IRC_NOTICEAUTH;
	pm_event.data = (unsigned char *)line.c_str();
	pm_event.size = line.length() + 1;

	EpDispatchEvent(transport, &pm_event);
}	

static void
DecodeStatusMessage(TRANSPORT_HANDLE transport, std::string line, std::string server) {
	std::string command;
	std::vector<std::string> middle;
	std::string params;

	int i = 0;

	// retrieve all the command data stuff

	while ((i < line.length()) && (isdigit(line[i])))
		command += line[i++];

	while ((i < line.length()) && (line[i] == ' '))
		++i;

	while ((i < line.length()) && (line[i] == '*'))
		++i;

	while ((i < line.length()) && (line[i] == ' '))
		++i;

	while ((i < line.length())  && (line[i] != ':')) {
		std::string tmp;

		while ((i < line.length()) && (line[i] != ' ') && (line[i] != ':'))
			tmp += line[i++];

		middle.push_back(tmp);

		while ((i < line.length()) && (line[i] == ' '))
			++i;
	}

	++i; // skip ':'

	while ((i < line.length()) && (line[i] == ' '))
		++i;

	while (i < line.length())
		params += line[i++];

	// use the command string to create an event

	EpEvent pm_event;
	pm_event.reference_id = 0;
	pm_event.protocol = CLSID_IRC_PROTOCOL;

	switch(atoi(command.c_str())) {
		case 365 : // [RPL_ENDOFLINKS]
		case 366 : // [RPL_ENDOFNAMES]
		case 368 : // [RPL_ENDOFBANLIST]
			break;

		case 332 : // [RPL_TOPIC]
		{
			IRCChannelTopic msg;
			strcpy(msg.server, server.c_str());
			strcpy(msg.channel, middle[0].c_str());
			strcpy(msg.topic, params.c_str());

			pm_event.msg = IRC_CHANNEL_TOPIC;
			pm_event.data = (unsigned char *)&msg;
			pm_event.size = sizeof(IRCChannelTopic);

			EpDispatchEvent(transport, &pm_event);
			break;
		}

		case 353 : // [RPL_NAMREPLY]
		{
			IRCNick ircnick;
			strcpy(ircnick.channel, middle[2].c_str());
			pm_event.msg = IRC_NICK_ENTRY;

			int i = 0;

			while (i < params.length()) {
				std::string nick;

				while ((i < params.length()) && (params[i] != ' '))
					nick += params[i++];
	
				while ((i < params.length()) && (params[i] == ' '))
					++i;

				strcpy(ircnick.nick, nick.c_str());

				pm_event.data = (unsigned char *)&ircnick;
				pm_event.size = sizeof(IRCNick);
				EpDispatchEvent(transport, &pm_event);
			}
			
			break;
		}

		case 252 : // [RPL_LUSEROP]
		case 253 : // [RPL_LUSERUNKNOWN]
		case 254 : // [RPL_LUSERCHANNELS]
		{
			std::string tmp = middle[1] + std::string(" ") + params;

			IRCStatusMessage msg;
			strcpy(msg.message, tmp.c_str());
			strcpy(msg.server, server.c_str());

			pm_event.msg = IRC_STATUS_MESSAGE;
			pm_event.data = (unsigned char *)&msg;
			pm_event.size = sizeof(IRCStatusMessage);

			EpDispatchEvent(transport, &pm_event);
			break;
		}

		default :
		{
			IRCStatusMessage msg;
			strcpy(msg.message, params.c_str());
			strcpy(msg.server, server.c_str());

			pm_event.msg = IRC_STATUS_MESSAGE;
			pm_event.data = (unsigned char *)&msg;
			pm_event.size = sizeof(IRCStatusMessage);

			EpDispatchEvent(transport, &pm_event);
			break;
		}
	}
}

static void
DecodeCommand(TRANSPORT_HANDLE transport, std::string line, std::string server) {
	std::string command;
	std::vector<std::string> middle;
	std::string params;

	int i = 0;

	// retrieve all the command data stuff

	while ((i < line.length()) && (line[i] != ' '))
		command += line[i++];

	while ((i < line.length()) && (line[i] == ' '))
		++i;

	while ((i < line.length())  && (line[i] != ':')) {
		std::string tmp;

		while ((i < line.length()) && (line[i] != ' ') && (line[i] != ':'))
			tmp += line[i++];

		middle.push_back(tmp);

		while ((i < line.length()) && (line[i] == ' '))
			++i;
	}

	++i; // skip ':'

	while ((i < line.length()) && (line[i] == ' '))
		++i;

	while (i < line.length())
		params += line[i++];

	// use the command string to create an event

	EpEvent pm_event;
	pm_event.reference_id = 0;
	pm_event.protocol = CLSID_IRC_PROTOCOL;

	if (strncmp(command.c_str(), "PRIVMSG", 7) == 0) {
		IRCReplyPrivateMsg msg;

		int find = server.find("!");
		strcpy(msg.nick, server.substr(0, find).c_str());
		strcpy(msg.mask, server.substr(find + 1, server.length() - find - 1).c_str());
		strcpy(msg.channel, middle[0].c_str());
		strcpy(msg.message, params.c_str());

		pm_event.msg = IRC_PRIVMSG;
		pm_event.data = (unsigned char *)&msg;
		pm_event.size = sizeof(IRCReplyPrivateMsg);

		EpDispatchEvent(transport, &pm_event);
		return;
	}

	if (strncmp(command.c_str(), "JOIN", 4) == 0) {
		IRCNick nick;

		int find = server.find("!");
		strcpy(nick.nick, server.substr(0, find).c_str());
		strcpy(nick.mask, server.substr(find + 1, server.length() - find - 1).c_str());
		strcpy(nick.channel, params.c_str());

		pm_event.msg = IRC_JOIN;
		pm_event.data = (unsigned char *)&nick;
		pm_event.size = sizeof(IRCNick);

		EpDispatchEvent(transport, &pm_event);
		return;
	};

	if (strncmp(command.c_str(), "PART", 4) == 0) {
		IRCNick nick;

		int find = server.find("!");
		strcpy(nick.nick, server.substr(0, find).c_str());
		strcpy(nick.mask, server.substr(find + 1, server.length() - find - 1).c_str());
		strcpy(nick.channel, params.c_str());

		pm_event.msg = IRC_PART;
		pm_event.data = (unsigned char *)&nick;
		pm_event.size = sizeof(IRCNick);

		EpDispatchEvent(transport, &pm_event);
		return;
	};

	if (strncmp(command.c_str(), "QUIT", 4) == 0) {
		IRCNick nick;

		int find = server.find("!");
		strcpy(nick.nick, server.substr(0, find).c_str());
		strcpy(nick.mask, server.substr(find + 1, server.length() - find - 1).c_str());
		strcpy(nick.channel, params.c_str());

		pm_event.msg = IRC_QUIT;
		pm_event.data = (unsigned char *)&nick;
		pm_event.size = sizeof(IRCNick);

		EpDispatchEvent(transport, &pm_event);
		return;
	};
}

static void
Decode(TRANSPORT_HANDLE transport, std::string line) {
	std::string nickserver;

	// prefix part

	int i = 0;

	if (line[i] == ':') {
		++i;

		while ((i < line.length()) && (line[i] != ' '))
			nickserver += line[i++];

		while ((i < line.length()) && (line[i] == ' '))
			++i;
	}

	// command part (3 digits or a string)

	if (isdigit(line[i]))
		DecodeStatusMessage(transport, line.substr(i, line.length() - i), nickserver);
	else
		DecodeCommand(transport, line.substr(i, line.length() - i), nickserver);
}

static void
PingDecode(TRANSPORT_HANDLE transport, std::string line) {
	EpEvent pm_event;
	pm_event.reference_id = 0;
	pm_event.protocol = CLSID_IRC_PROTOCOL;
	pm_event.msg = IRC_PING;
	pm_event.data = (unsigned char *)line.c_str();
	pm_event.size = line.length() + 1;

	EpDispatchEvent(transport, &pm_event);
}

static void
IrcDecode(TRANSPORT_HANDLE transport, std::string line) {
	if (strncmp(line.c_str(), "NOTICE AUTH", 11) == 0) {
		int i = 11;

		while ((i < line.length()) && (line[i] != ':'))
			++i;

		while ((i < line.length()) && (line[i] == ':'))
			++i;

		NoticeAuthDecode(transport, line.substr(i, line.length() - i));
	} else if (strncmp(line.c_str(), "PING", 4) == 0) {
		int i = 4;

		while ((i < line.length()) && (line[i] != ':'))
			++i;

		while ((i < line.length()) && (line[i] == ':'))
			++i;

		PingDecode(transport, line.substr(i, line.length() - i));
	} else {
		Decode(transport, line);
	}

	int i[] = { 0, 1, 2 };

}

// --------------------------------------------------------

static void *DLL_CALLCONV
ProtocolCreate(TRANSPORT_HANDLE plug) {
	ProtocolData *prot_data = new ProtocolData;
	memset(prot_data, 0, sizeof(ProtocolData));

	prot_data->plug = plug;
	return prot_data;
}

static bool DLL_CALLCONV
ProtocolReceive(void *self, unsigned char *data, int size) {
	ProtocolData *prot_data = (ProtocolData *)self;

	// copy the data into the buffer

	memcpy(prot_data->data + prot_data->data_size, data, size);	
	prot_data->data_size += size;

	// search for 0x0D's. if found we found a valid SMTP reply

	int i = 0;
	int pos = 0;

	// strip any pending 0x0A and 0x0D chars

	while (pos < prot_data->data_size) {
		if ((prot_data->data[pos] != 0x0A) && (prot_data->data[pos] != 0x0D))
			break;

		++pos;
	}

	// search for the next 0x0A or 0x0D

	while (pos + i < prot_data->data_size) {
		if (prot_data->data[pos + i] == 0x0D) {
			// copy the found string into a temporary buffer for easier processing

			char tmp_buffer[8192];
			memcpy(tmp_buffer, prot_data->data + pos, i - pos);
			tmp_buffer[i - pos] = 0;

			// decode the found line

			if (strlen(tmp_buffer) > 0) {
				// send the raw irc string to the user

				EpEvent pm_event;
				pm_event.reference_id = 0;
				pm_event.protocol = CLSID_IRC_PROTOCOL;
				pm_event.msg = IRC_RAW;
				pm_event.data = (unsigned char *)tmp_buffer;
				pm_event.size = strlen(tmp_buffer) + 1;

				EpDispatchEvent(prot_data->plug, &pm_event);

				// decode the string

				IrcDecode(prot_data->plug, std::string(tmp_buffer));
			}

			// skip end of line characters

			while ((pos + i < prot_data->data_size) && ((prot_data->data[pos + i] == 0x0D) || (prot_data->data[pos + i] == 0x0A)))
				++i;

			memcpy(prot_data->data, prot_data->data + i, prot_data->data_size - i);

			prot_data->data_size -= i;

			i = 0;
		} else {
			++i;
		}
	}

	return true;
}

static bool DLL_CALLCONV
ProtocolSend(void *self, EpAction *action){
	ProtocolData *prot_data = (ProtocolData *)self;

	std::string complete;

	switch(action->msg) {
		case IRC_CMD_NICK :
		{
			complete = "NICK " + std::string((char *)action->data) + std::string("\r\n");
			EpCompleteAction(prot_data->plug, (unsigned char *)complete.c_str(), complete.length());
			return true;
		}

		case IRC_CMD_PASS :
		{
			complete = "PASS " + std::string((char *)action->data) + std::string("\r\n");
			EpCompleteAction(prot_data->plug, (unsigned char *)complete.c_str(), complete.length());
			return true;
		}

		case IRC_CMD_USER :
		{
			IRCUser *msg = (IRCUser *)action->data;

			complete = "USER " + std::string(msg->username) + std::string(" ") +
				std::string(msg->hostname) + std::string(" ") + std::string(msg->servername)
				+ std::string(" : ") + std::string(msg->realname) + std::string("\r\n");

			EpCompleteAction(prot_data->plug, (unsigned char *)complete.c_str(), complete.length());
			return true;
		}

		case IRC_CMD_PONG :
		{
			complete = "PONG " + std::string((char *)action->data) + std::string("\r\n");
			EpCompleteAction(prot_data->plug, (unsigned char *)complete.c_str(), complete.length());
			return true;
		}

		case IRC_CMD_JOIN :
		{
			complete = "JOIN " + std::string((char *)action->data) + std::string("\r\n");
			EpCompleteAction(prot_data->plug, (unsigned char *)complete.c_str(), complete.length());
			return true;
		}

		case IRC_CMD_PRIVMSG :
		{
			IRCPrivateMsg *msg = (IRCPrivateMsg *)action->data;
			complete = "PRIVMSG " + std::string((char *)msg->target) + std::string(" :") + std::string((char *)msg->message) + std::string("\r\n");
			EpCompleteAction(prot_data->plug, (unsigned char *)complete.c_str(), complete.length());
			return true;
		}
	};

	return false;
}

static void DLL_CALLCONV
ProtocolDestroy(void *self) {
	delete (ProtocolData *)self;
}

// --------------------------------------------------------

SMTP_API CLSID DLL_CALLCONV
EpIRCProtocolInit(Protocol *protocol) {
	protocol->create = ProtocolCreate;
	protocol->destroy = ProtocolDestroy;
	protocol->receive = ProtocolReceive;
	protocol->send = ProtocolSend;
	return CLSID_IRC_PROTOCOL;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -