Skip to content

Commit

Permalink
Add PRIVMSG events and basic command handling
Browse files Browse the repository at this point in the history
  • Loading branch information
AppleDash committed Oct 7, 2015
1 parent 8cffbbc commit 4c827e0
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
2 changes: 2 additions & 0 deletions include/botframe.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "irc.h"
#include "utils.h"
#include "module.h"
#include "ircparse.h"
#include "configuration.h"
Expand Down Expand Up @@ -28,6 +29,7 @@ class Bot {
std::string ident;
std::string realname;
std::vector<std::string> channels;
std::string cmd_prefix;
Configuration config;
IrcConnection *conn;

Expand Down
3 changes: 3 additions & 0 deletions include/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ class Module {
Module() { }
virtual ~Module() { }
virtual void OnIRCRaw(Line line) { }
virtual void OnPrivateMessage(Hostmask source, std::string message) { }
virtual void OnChannelMessage(Hostmask source, std::string channel, std::string message) { }
virtual void OnIRCCommand(Hostmask source, std::string target, std::string cmd, std::vector<std::string> args) { }
Bot *bot;
};
3 changes: 3 additions & 0 deletions modules/include/m_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ class ModuleTest : public Module {
public:
ModuleTest();
virtual void OnIRCRaw(Line line);
virtual void OnPrivateMessage(Hostmask source, std::string message);
virtual void OnChannelMessage(Hostmask source, std::string channel, std::string message);
virtual void OnIRCCommand(Hostmask source, std::string target, std::string cmd, std::vector<std::string> args);
};
17 changes: 17 additions & 0 deletions modules/src/m_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,21 @@ ModuleTest::ModuleTest() {

void ModuleTest::OnIRCRaw(Line line) {
cout << "Received line: " << line.linestr << "\n";
}

void ModuleTest::OnPrivateMessage(Hostmask source, std::string message) {
cout << "Private message to us from " << source.nick << ": " << message << "\n";
}

void ModuleTest::OnChannelMessage(Hostmask source, std::string channel, std::string message) {
cout << "Channel message to " << channel << " from " << source.nick << ": " << message << "\n";
}

void ModuleTest::OnIRCCommand(Hostmask source, std::string target, std::string cmd, std::vector<std::string> args) {
cout << source.nick << " performed command " << cmd << " with target " << target << " and args ";
for (std::string &arg : args) {
cout << arg << " ";
}

cout << "\n";
}
36 changes: 35 additions & 1 deletion src/botframe.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "botframe.h"

Bot::Bot() : config("bot.config") {
Bot::Bot() : config("bot.config"), cmd_prefix("!") {
std::string host;
int port;

Expand All @@ -17,6 +17,8 @@ Bot::Bot() : config("bot.config") {
throw std::runtime_error("Failed to load IRC hostname and port from config!");
}

this->config.GetString("command_prefix", this->cmd_prefix);

this->LoadModules();
}

Expand Down Expand Up @@ -51,6 +53,38 @@ void Bot::Go() {
cout << "Nickname " << this->nick << " is already in use, appending an underscore and trying again.\n";
this->nick += "_";
this->Nick(this->nick);
} else if (ln.command == "PRIVMSG") {
std::string target = ln.params[0];
std::string message = ln.params[1];

if (std::string("#&").find(target[0]) != std::string::npos) { /* Message is to a channel, &channels are server-local on some ircds. */
for (Module *mod : this->modules) {
mod->OnChannelMessage(*(ln.hostmask), target, message);
}
} else { /* Message directly to us */
for (Module *mod : this->modules) {
mod->OnPrivateMessage(*(ln.hostmask), message);
}
}

if (message.find(this->cmd_prefix) == 0) { /* Starts with command prefix */
if (message.length() > this->cmd_prefix.length()) {
std::string raw_cmd = message.substr(this->cmd_prefix.length());
std::vector<std::string> split_cmd = split_string(raw_cmd, " ");
std::vector<std::string> cmd_args(split_cmd);
std::string cmd = split_cmd[0];

if (split_cmd.size() > 1) {
cmd_args.erase(cmd_args.begin(), cmd_args.begin() + 1);
} else {
cmd_args.erase(cmd_args.begin(), cmd_args.end());
}

for (Module *mod : this->modules) {
mod->OnIRCCommand(*(ln.hostmask), target, cmd, cmd_args);
}
}
}
}

/* Run module hooks for raw line */
Expand Down

0 comments on commit 4c827e0

Please sign in to comment.