예제 #1
0
파일: IRC.cpp 프로젝트: Chase-san/AnnaBot
void IRC::handle(const string& oline) {
    string line = trim(oline);
    printf("%s\n", line.c_str());

    //We are pinged, respond!
    if(line.compare(0, 4, "PING") == 0) {
        onServerPing(line.substr(5));
        return;
    }

    char* cstr = new char[line.size() + 1];
    strcpy(cstr, line.c_str());

    string sender, nick, login, host;

    bool nickOnly = false;
    size_t tokn = 0;
    char *tok = strtok(cstr, " ");
    while(tok != NULL) {
        //For each token, check
        string tmp = tok;
        if(tokn == 0) {
            sender = tmp;
            if(sender[0] == ':')
                sender = sender.substr(1);

            size_t exp = sender.find("!");
            size_t at = sender.find("@");

            //TODO: Add checks for 'exp' and 'at' being string::npos
            //  to avoid calling these when there are (no ill effects
            //  have been seen from not doing so yet, however for sanity sake)
            if(exp == string::npos || at == string::npos) {
                nick = trim(sender);
                login = "";
                host = "";
                nickOnly = true;
            } else {
                nick = trim(sender.substr(0, exp));
                login = trim(sender.substr(exp + 1, at));
                host = trim(sender.substr(at + 1));
            }
        }

        if(tokn == 1) {
            tok = strtok(NULL, " ");
            string target(tok); //AKA channel

            uint32_t code = 0;
            if(tmp.length() == 3 && (code = (uint32_t) atoi(tmp.c_str())) != 0 && code < 1000) {
                string response = line.substr(sender.length()+5+target.length()+1);
                handle_numeric(code,target,response);
                onNumeric(sender,code,target,response);
            } else if(tmp.compare("PRIVMSG") == 0) {
                handle_msg(target, nick, login, host, line.substr(line.find(" :") + 2));
            } else if(tmp.compare("NOTICE") == 0) {
                onNotice(target, nick, login, host, line.substr(line.find(" :") + 2));
            } else if(tmp.compare("JOIN") == 0) {
                //TODO: Add this user to the channel.
                onJoin(target, nick, login, host);
            } else if(tmp.compare("PART") == 0) {
                //TODO: Remove this user from the channel.
                onPart(target, nick, login, host);
            } else if(tmp.compare("TOPIC") == 0) {
                onTopic(target, line.substr(line.find(" :") + 2), nick, time(NULL), true);
            } else if(tmp.compare("KICK") == 0) {
                tok = strtok(NULL, " ");
                string recipient(tok);
                onKick(target, nick, login, host, recipient, line.substr(line.find(" :") + 2));
            } else if(tmp.compare("QUIT") == 0) {
                onQuit(nick, login, host, line.substr(line.find(" :") + 2));
            } else if(tmp.compare("MODE") == 0) {
                tok = strtok(NULL, " ");
                string mode = tok;
                if(mode[0] == ':')
                    mode = mode.substr(1);
                handle_mode(nick, login, host, target, mode);
            }
            //TODO: Add mode handler here
            else {
                onUnknown(line);
            }

            break;
        }

        tok = strtok(NULL, " ");
        ++tokn;
    }

    delete[] cstr;
}
예제 #2
0
파일: cppmodule.cpp 프로젝트: S1M0NE/IrcBot
bool CPPModule::onEvent(IrcConnection* connection, const std::string& senderID, const std::string& eventName, const std::string& json, const std::string& optString) {
	JSONNode n;
	if(!json.empty()) {
		n = libjson::parse(json);
	}

	if(senderID=="IRCCONNECTION") {
		//Create a IrcModuleConnection instance
		IrcModuleConnection ircModuleConnection(mID, mModuleManager, connection);


		//Call onConnect if its the called event
		if(eventName=="onConnect") {
			return onConnect(ircModuleConnection);
		}


		//Convert the IRC Message
		IrcMessage message;
		message.ircLine = n["ircLine"].as_string();
		message.prefix = n["prefix"].as_string();
		message.hasDetailedPrefix = n["hasDetailedPrefix"].as_bool();

		if(message.hasDetailedPrefix) {
			message.msgPrefix.nick_or_server = n["msgPrefix"]["nick_or_server"].as_string();
			message.msgPrefix.user = n["msgPrefix"]["user"].as_string();
			message.msgPrefix.host = n["msgPrefix"]["host"].as_string();
		}

		message.command = n["command"].as_string();
		message.isNumeric = n["isNumeric"].as_bool();
		message.target = n["target"].as_string();
		message.params = n["params"].as_string();


		//Call all other event handlers
		if(eventName=="onMessage") {
			return onMessage(ircModuleConnection, message);
		}
		else if(eventName=="onJoin") {
			return onJoin(ircModuleConnection, message);
		}
		else if(eventName=="onPart") {
			return onPart(ircModuleConnection, message);
		}
		else if(eventName=="onKick") {
			return onKick(ircModuleConnection, message, optString);
		}
		else if(eventName=="onQuit") {
			return onQuit(ircModuleConnection, message);
		}
		else if(eventName=="onKill") {
			return onKill(ircModuleConnection, message, optString);
		}
		else if(eventName=="onPrivateMessage") {
			return onPrivateMessage(ircModuleConnection, message);
		}
		else if(eventName=="onChannelMessage") {
			return onChannelMessage(ircModuleConnection, message);
		}
		else if(eventName=="onNotice") {
			return onNotice(ircModuleConnection, message);
		}
		else if(eventName=="onInvite") {
			return onInvite(ircModuleConnection, message);
		}
		else if(eventName=="onTopicChanged") {
			return onTopicChanged(ircModuleConnection, message);
		}
		else if(eventName=="onNickChanged") {
			return onNickChanged(ircModuleConnection, message);
		}
	}

	return true;
}