Saturday, 20 January 2007

Building a bot, part tres

After much study and some soul-searching, I've decided to cut the scripting engines from the nibblebot. It pains me to do so, but there's a couple good reaons:

  • The Perl intepreter adds about 1MB to the runtime binary, and

  • there's no way to implement a truly persistent interpreter, so you'd have to fork() a lot.


When you put those two things together, it means that you'd potentially have a fork() and then a bunch of database activity for every incoming line of IRC text, and it's just too heavy and slow.

So, my approach will be a little different. I've pretty much covered all the RFC-defined stuff in a base class now, (class Bot), so now I'm going to extend that class (class NibbleBot), and mostly all it will do is consult the database for what to do next. The idea is this: set up a "scripts" table that can act as an interpreter of sorts.

We'd have an `event` column, in which you'd have things like 'onJoin', 'onPart', 'onChanMsg', 'onKick' and so forth. The next column would contain a query that the bot should execute after substituting the appropriate information in special "macros"; for instance, perhaps you'd have something like "SELECT * FROM `users` WHERE `name` = $NICK", and $NICK would be replaced by the nickname of the user who uttered the line that activated this "script".

Next column over would contain the `action`, i.e. where the result from the query (if any) should be directed (or not). So perhaps we have an enum like, 'chanMsg', 'op', 'topic', etc, and if the query returned any results, the bot would do the appropriate thing with the resulting rows. Add on a final row - `priority` - which would dictate the order in which the bot should execute multiple scripts for a particular event, and you could have the bot doing all sorts of stuff when something interesting occured.

I think this will work for most things; there may be a few special cases, but I think for the most part everything I want to do can be accomplished in this manner. So in theory, once I have all the core code for this in place, I'll still be able to re-program the bot on the fly. Good for me.

1 comment: