All pastes #796454 Raw Edit

Miscellany

public text v1 · immutable
#796454 ·published 2007-11-25 02:13 UTC
rendered paste body
// DumbBot - Basic IRC Robot Built by Jonathan Cox for CS192
// Should run on Linux
// Basically, we start by having it respond to basic commands in a channel.

#include <stdio.h>
#include <stdlib.h>

#include "libIRC.h"

IRCClient client;

class myEndMOTDCallback : public IRCClientEventCallback
{
        // Prepare to process!
public:
        bool process ( IRCClient &ircClient, teIRCEventType eventType, trBaseEventInfo &info )
        {
                printf("Now connected. Joining my channel...\n");
                ircClient.join("#botpark");
                return true;
        }
};

class myAllCallback : public IRCClientCommandHandler
{
public:
        myAllCallback()
        {
                name = "ALL";
        }
        virtual bool receive ( IRCClient &client, const std::string &command, BaseIRCCommandInfo &info )
        {
                std::string line = info.raw;
                FILE *fp = fopen("dbot.log", "at");
                if (fp)
                {
                        fprintf(fp, "%s\n", info.raw.c_str());
                        fclose(fp);
                }
                return false;
        }
}

class myDumbEventHandler : public IRCClientEventHandler
{
        bool process ( IRCClient &ircClient, teIRCEventType     eventType, trBaseEventInfo &info )
        {
                switch (eventType)
                {
                        case eIRCPrivateMessageEvent:
                                privateMessage((trClientMessageEventInfo*)&info);
                                break;
                        case eIRCChannelMessageEvent:
                                channelMessage((trClientMessageEventInfo*)&info);
                                break;
                }
        }
}

myEndMOTDCallback startupCallback;
myAllCallback allCallback;
myDumbEventHandler dumbEventHandler;

void channelMessage( trClientMessageEventInfo *info )
{
        // Process a channel message.
        IRCCommandInfo commandInfo;
        int die = 0;

        if (strcmp(info->params[0], "$8ball"))
        {
                // Someone wants a fortune!
                commandInfo.target = info->channel;
                if (!info->params[1])
                {
                        commandInfo.params.push_back("Who knows.");
                }
                else
                {
                        if (strlen(info->params[1]) % 2 == 0)
                        {
                                commandInfo.params.push_back("Yes.");
                        }
                        else
                        {
                                commandInfo.params.push_back("No.");
                        }
                }
                client.sendIRCCommand(eCMD_PRIVMSG, commandInfo);
        }
        else if (strcmp(info->params[0], "$rolldie"))
        {
                // Oh, so you really want to roll the dice?
                commandInfo.target = info->channel;
                myroll = time() / 7 % 6;
                commandInfo.params.push_back("The die lands on " + myroll + ".");
                client.sendIRCCommand(eCMD_PRIVMSG, commandInfo);
        }
}

void privateMessage( trClientMessageEventInfo *info )
{
        // Process a private message to me.
        IRCCommandInfo commandInfo;

        if (strcmp(info->params[0], "quit") == 0)
        {
                // Someone told me to quit.
                if (strcmp(strtolower(info->from), "etangent") == 0)
                {
                        // The right person said it.
                        client.disconnect("Boss says I have to go.");
                }
                else
                {
                        // The wrong person said it.
                        commandInfo.target = info->from;
                        commandInfo.params.push_back("Hey! You're not the boss of me!");
                        client.sendIRCCommand(eCMD_PRIVMSG, commandInfo);
                }
        else
        {
                // Someone just randomly sent me a private message. What a jerk.
                commandInfo.target = info->from;
                commandInfo.params.push_back("I don't understand.");
                client.sendIRCCommand(eCMD_PRIVMSG, commandInfo);
        }
}

int main()
{
        // Will need to look at what variables I used in my PHP bots.

        printf("DumbBot Started...\n");

        /* Project Plans:
                - Connect to an IRC server (irc.freenode.net)
                - Join an IRC channel (#botpark)
                - Respond appropriately to 
                        !8ball
                        !dice
                - Quit IRC when asked via PRIVMSG (sent from nick eTangenT) */

        client.setDebugLevel(5);

        // Clear log file
        fclose(fopen("irc.log", "wt"));
        fclose(fopen("dbot.log", "wt"));

        // And set it
        client.setLogfile("irc.log");

        client.registerEventHandler(eIRCNoticeEvent, &startupCallback);
        client.registerEventHandler(eIRCPrivateMessageEvent, &dumbEventHandler);
        client.registerEventHandler(eIRCChannelMessageEvent, &dumbEventHandler);

        client.connect("irc.freenode.net",6667);
        client.login(std::string("dBot192"), std::string("cox"), std::string("CS 192 Project"), std::string(""));

        while (client.process())
        {
                IRCOSSleep(1);
        }

        return 0;
}