Running bash, perl, c++ from IRC

I added 3 new commands to my irc bot, which allows you to input a line of a programming language for it to execute. The output of the program is then relayed back to irc (only up to first line and truncated for whole message to be less than 512 bytes in length). The scripts are not persistent so using cd for bash has no effect.

As always, the source to my irc bot (together with copy of axcel) can be found at http://abacus.subluminal.net/~jakashthree/sauce/

AXCEL C++ Framework project: http://sourceforge.net/projects/libaxcel/
AXCEL C++ Framework Docs: http://libaxcel.sourceforge.net/

//Run line of bash
void shell() {
	if (owner != im.nick) { sock << "PRIVMSG " << im.param(0) << " :Access denied.\r\n"; return; }
	if (buf.empty()) return;
	String s(564);
	s << "PRIVMSG " << im.param(0) << " :";
	s += buf.qx().rep("\n", " | ").chomp(" | ").cat("\r\n");
	s = s.cpy("\r\n", 512);
	sock << s;
}

//Run line of perl
void perl() {
	if (owner != im.nick) { sock << "PRIVMSG " << im.param(0) << " :Access denied.\r\n"; return; }
	if (buf.empty()) { sock << "PRIVMSG " << im.param(0) << " :(null)\r\n"; return; }
	String s(1024);
	s = buf;
	s = s.rep("'", "'\\''").prep("perl -e '").cat("'").qx().cat("\r\n");
	s = s.prep(" :").prep(im.param(0)).prep("PRIVMSG ");
	s = s.cpy("\r\n", 512);
	sock << s;
}

//Run line of C++
void cpp() {
	rm("fooirctemp");
	if (owner != im.nick) { sock << "PRIVMSG " << im.param(0) << " :Access denied.\r\n"; return; }
	if (buf.empty()) { sock << "PRIVMSG " << im.param(0) << " :(null)\r\n"; return; }
	String s(1024);
	File f;
	f.open("fooirctemp.cpp", "wb");
	f <<
		"#include \"axcel.h\"\n"
		"using namespace axcel;\n" << buf;
	f.close();
	system("g++ -o fooirctemp fooirctemp.cpp axcel.cpp -l dl -l pthread");
	unless (fexist("fooirctemp")) {
		sock << "PRIVMSG " << im.param(0) << " :Compiler error!\r\n";
		return;
	}
	s = qx("./fooirctemp");
	s += "\r\n";
	s.cpy("\r\n", 512);
	sock << "PRIVMSG " << im.param(0) << " :" << s;
}

3 thoughts on “Running bash, perl, c++ from IRC

  1. You tell us :”The scripts are not persistent so using cd for bash has no effect.”
    Is working or not ? I think if this working will work on client side.
    Anyway nice souce code.
    Thank share us.

    • For void shell(), the command is executed in a new shell with system() on the machine that the IRC bot is running on, the output is then returned to IRC. So commands will work, but only within that session. Custom user-set variables including the current directory is forgotten by the time the command ends. For example:

      foo #shell pwd
      foo #shell cd ..
      foo #shell pwd
      

      The above will print the same current directory twice. Everytime you use #shell foo essentially opens a new bash shell session, runs that command, and exits.

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: