summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiftob/Commands.cpp')
-rw-r--r--Swiftob/Commands.cpp54
1 files changed, 53 insertions, 1 deletions
diff --git a/Swiftob/Commands.cpp b/Swiftob/Commands.cpp
index cf24196..38e5f57 100644
--- a/Swiftob/Commands.cpp
+++ b/Swiftob/Commands.cpp
@@ -10,4 +10,5 @@
#include <iostream>
#include <boost/bind.hpp>
+#include <boost/algorithm/string.hpp>
#include <Swiften/Client/Client.h>
@@ -23,9 +24,18 @@ Commands::Commands(Users* users, Swift::Client* client, Storage* storage, MUCs*
}
-void Commands::resetCommands() {
+Commands::~Commands() {
+ clearCommands();
+}
+
+void Commands::clearCommands() {
foreach (NamedCommand command, commands_) {
delete command.second;
}
commands_.clear();
+
+}
+
+void Commands::resetCommands() {
+ clearCommands();
registerCommand("quit", Owner, "Quit the bot", boost::bind(&Commands::handleQuitCommand, this, _1, _2, _3));
registerCommand("help", Anyone, "Get help", boost::bind(&Commands::handleHelpCommand, this, _1, _2, _3));
@@ -33,4 +43,7 @@ void Commands::resetCommands() {
registerCommand("part", Owner, "Leave a MUC", boost::bind(&Commands::handlePartCommand, this, _1, _2, _3));
registerCommand("rehash", Owner, "Reload scripts", boost::bind(&Commands::handleRehashCommand, this, _1, _2, _3));
+ registerCommand("restart", Owner, "Restart bot", boost::bind(&Commands::handleRestartCommand, this, _1, _2, _3));
+ registerCommand("nick", Owner, "Change nick (requires restart)", boost::bind(&Commands::handleChangeNick, this, _1, _2, _3));
+ //registerCommand("owner", Owner, "Change owner settinsg", boost::bind(&Commands::handleChangeOwner, this, _1, _2, _3));
onReset();
}
@@ -42,4 +55,8 @@ void Commands::registerCommand(const std::string& name, RoleList roles, const st
}
+void Commands::registerListener(ListenerCallback listener) {
+ listeners_.push_back(listener);
+}
+
bool Commands::hasCommand(const std::string& name) {
return commands_.find(name) != commands_.end();
@@ -58,4 +75,10 @@ bool Commands::runCommand(const std::string& name, const std::string& params, Sw
}
+void Commands::runListeners(Swift::Message::ref message) {
+ foreach (ListenerCallback listener, listeners_) {
+ listener(message);
+ }
+}
+
bool Commands::roleIn(const Users::User::Role userRole, RoleList roleList) {
switch (roleList) {
@@ -67,4 +90,25 @@ bool Commands::roleIn(const Users::User::Role userRole, RoleList roleList) {
}
+void Commands::handleChangeNick(const std::string& /*command*/, const std::string& params, Swift::Message::ref message) {
+ std::string nick(params);
+ boost::algorithm::trim(nick);
+ if (nick.empty()) {
+ replyTo(message, "Current nick is '" + mucs_->getDefaultNick() + "'. Run the command with a new nick to change it.");
+ }
+ else {
+ if (mucs_->setDefaultNick(params)) {
+ replyTo(message, "Default nick now set to '" + nick + "' - restart the bot for this to take effect.");
+ }
+ else {
+ replyTo(message, "Can't set invalid nick '" + nick + "'.");
+ }
+ }
+}
+
+void Commands::handleChangeOwner(const std::string& /*command*/, const std::string& /*params*/, Swift::Message::ref /*message*/) {
+ /* Oh, right. I don't have user persistence coded yet.
+ * Probably not worth doing this until I have.*/
+}
+
void Commands::handleQuitCommand(const std::string& /*command*/, const std::string& /*params*/, Swift::Message::ref message) {
replyTo(message, "Shutting down");
@@ -85,4 +129,5 @@ void Commands::handleRehashCommand(const std::string& /*command*/, const std::st
std::cout << "Rehashing at the behest of " << message->getFrom().toString() << std::endl;
resetCommands();
+ listeners_.clear();
if (rehashError_.empty()) {
replyTo(message, "Rehash complete");
@@ -92,4 +137,11 @@ void Commands::handleRehashCommand(const std::string& /*command*/, const std::st
}
+void Commands::handleRestartCommand(const std::string& /*command*/, const std::string& /*params*/, Swift::Message::ref message) {
+ rehashError_ = "";
+ replyTo(message, "Restarting now.");
+ std::cout << "Restarting at the behest of " << message->getFrom().toString() << std::endl;
+ onRestartRequested();
+}
+
void Commands::handleJoinCommand(const std::string& /*command*/, const std::string& params, Swift::Message::ref message) {
Swift::JID room(params);