IRC Bot using AXCEL C++ Library 2 Beta

This customizable irc bot is made from my AXCEL c++ library, this library is still in beta and I hope this test can prove it to be made into a final release.

This library is cross-platform (posix or windows) and includes a socket, string, file, and thread class. It also includes facilities for common file operations, dynamic memory management, and dynamic library loading.

This irc bot includes 2 arrays of struct that contain a keyword and a function pointer. The apps array contains functions to be called based on the first word you say in the sentence directed to the bot, these can serve as irc bot commands. The events array contains functions to call whenever a certain word is detected in a privmsg message.

I hope you can get an idea of how it works even though the code is pretty high level due to the usage of the library.

Source of main function of irc bot:

#include "axcel/inc/Socket.h"
#include "axcel/inc/String.h"
#include "axcel/inc/console.h"
#include "axcel/inc/util.h"
#include <time.h>
using namespace axcel;

#include "apps.cpp"
#include "events.cpp"

int main(int argc, char** argv) {
	unless (argc == 5)
		die("Usage: %s nick owner server channel", argv[0]);
	int i;
	String buf, cmd;
	Socket s;
	struct plugin* p;
	unless (s.connect(argv[3], "6667")) die("Failed to connect\n");
	cout << "Connected" << nl;
	s <<
		"USER " << argv[1] << " 0 * :" << argv[2] << "\r\n" <<
		"NICK " << argv[1] << "\r\n";
	buf.realloc(512);
	buf.fit = false;
	while (s.gets(buf, 512)) {
		if (buf.begns("PING ")) { buf[1] = 'O'; s << buf; continue; }
		if (buf[0] != ':') continue;
		buf <= ':';
		if (buf.tok(1, ' ') == "001") {
			cout << "Logged in" << nl;
			s <<
				"MODE " << argv[1] << " +B\r\n" <<
				"JOIN " << argv[4] << "\r\n";
		} else if (buf.tok(1, ' ') == "332" || buf.tok(1, ' ') == "331")
			cout << "Joined channel: " << argv[4] << nl;
		else if (buf.tok(1, ' ') == "PRIVMSG") {
			buf = buf.rm(buf.chr('!'), buf.chr(':') - buf.chr('!')).chomp();
			cout << buf << nl;
			for (i = 0; events[i].instr != NULL; i++)
				if ((buf.rmlch(':').str(events[i].instr) != -1) && (!buf.rmlch(':').begns(argv[1])))
					events[i].func(s, buf.tok(0, ':'), argv[4], buf.shl(buf.chr(':') + 1));
			unless (buf.rmlch(':').begns(argv[1])) continue;
			if (buf.ends("quit") && buf.begns(argv[2])) { s.puts("QUIT\r\n"); s.close(); break; }
			if (buf.rmlch(' ') == "help") {
				s << "PRIVMSG " << argv[4] << " :Commands: ";
				for (i = 0; apps[i].name != NULL; i++) s << apps[i].name << " ";
				s << "\r\n";
				continue;
			}
			cmd = buf.rmlch(':').tok(1, " :,");
			for (i = 0; apps[i].name != NULL; i++) {
				if (cmd == apps[i].name) {
					apps[i].proc(s, buf.tok(0, ':'), argv[4], buf.shl(buf.itok(3, " :,")));
					break;
				}
			}
		}
	}
	cout << "Disconnected" << nl;
	return 0;
}

Download the rest of the source code and compiled elf executable:

abacus.subluminal.net/~jakashthree/ircbot_axcel2.tar.gz

This irc bot example mainly uses the << operator to write output. It also supports >>, puts, gets, printf, scanf, getc, putc, read, and write methods for flexible socket I/O. Also a few stream manipulators have been defined in In.h, Out.h and IOb.h which Socket.h derives from.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: