diff options
Diffstat (limited to 'Swiftob')
-rw-r--r-- | Swiftob/.gitignore | 1 | ||||
-rw-r--r-- | Swiftob/Commands.cpp | 217 | ||||
-rw-r--r-- | Swiftob/Commands.h | 81 | ||||
-rw-r--r-- | Swiftob/LuaCommands.cpp | 465 | ||||
-rw-r--r-- | Swiftob/LuaCommands.h | 90 | ||||
-rw-r--r-- | Swiftob/MUCs.cpp | 137 | ||||
-rw-r--r-- | Swiftob/MUCs.h | 47 | ||||
-rw-r--r-- | Swiftob/SConscript | 24 | ||||
-rw-r--r-- | Swiftob/Storage.cpp | 49 | ||||
-rw-r--r-- | Swiftob/Storage.h | 28 | ||||
-rw-r--r-- | Swiftob/Swiftob.cpp | 139 | ||||
-rw-r--r-- | Swiftob/Swiftob.h | 52 | ||||
-rw-r--r-- | Swiftob/Users.cpp | 48 | ||||
-rw-r--r-- | Swiftob/Users.h | 47 | ||||
-rw-r--r-- | Swiftob/main.cpp | 37 | ||||
-rw-r--r-- | Swiftob/scripts/agenda.lua | 94 | ||||
-rw-r--r-- | Swiftob/scripts/badWords.lua | 15 | ||||
-rw-r--r-- | Swiftob/scripts/echo.lua | 5 | ||||
-rw-r--r-- | Swiftob/scripts/eval.lua | 14 | ||||
-rw-r--r-- | Swiftob/scripts/irssiLogs.lua | 23 | ||||
-rw-r--r-- | Swiftob/scripts/logAllMessages.lua | 6 | ||||
-rw-r--r-- | Swiftob/scripts/version.lua | 32 |
22 files changed, 0 insertions, 1651 deletions
diff --git a/Swiftob/.gitignore b/Swiftob/.gitignore deleted file mode 100644 index 443811d..0000000 --- a/Swiftob/.gitignore +++ /dev/null @@ -1 +0,0 @@ -swiftob diff --git a/Swiftob/Commands.cpp b/Swiftob/Commands.cpp deleted file mode 100644 index 9212aaf..0000000 --- a/Swiftob/Commands.cpp +++ /dev/null @@ -1,217 +0,0 @@ -/* - * Copyright (c) 2011-2016 Isode Limited. - * All rights reserved. - * See the COPYING file for more information. - */ - -#include <Swiftob/Commands.h> - -#include <iostream> - -#include <boost/algorithm/string.hpp> -#include <boost/bind.hpp> - -#include <Swiften/Base/foreach.h> -#include <Swiften/Client/Client.h> - -typedef std::pair<std::string, Commands::Command*> NamedCommand; - -Commands::Commands(Users* users, Swift::Client* client, Storage* storage, MUCs* mucs) { - users_ = users; - client_ = client; - mucs_ = mucs; - storage_ = storage; - 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)); - registerCommand("join", Owner, "Join a MUC", boost::bind(&Commands::handleJoinCommand, this, _1, _2, _3)); - 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(); -} - -void Commands::registerCommand(const std::string& name, RoleList roles, const std::string& description, boost::function<void(const std::string& /*command*/, const std::string& /*params*/, Swift::Message::ref)> callback) { - Command* command = new Command(roles, description); - commands_[name] = command; - command->onReceived.connect(callback); -} - -void Commands::registerListener(ListenerCallback listener) { - listeners_.push_back(listener); -} - -bool Commands::hasCommand(const std::string& name) { - return commands_.find(name) != commands_.end(); -} - -bool Commands::runCommand(const std::string& name, const std::string& params, Swift::Message::ref message) { - Users::User::Role userRole = users_->getRoleForSender(message); - Command* command = commands_[name]; - if (roleIn(userRole, command->getAllowedBy())) { - command->onReceived(name, params, message); - return true; - } else { - replyTo(message, "You may not run this command", true); - } - return false; -} - -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) { - case Owner : return userRole == Users::User::Owner; - case Anyone : return true; - } - std::cerr << "Unrecognised role list" << std::endl; - return false; -} - -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"); - std::cout << "Quitting at the behest of " << message->getFrom().toString() << std::endl; - exit(0); -} - -void Commands::setRehashError(const std::string& error) { - if (!rehashError_.empty()) { - rehashError_ += "; "; - } - rehashError_ += error; -} - -void Commands::handleRehashCommand(const std::string& /*command*/, const std::string& /*params*/, Swift::Message::ref message) { - rehashError_ = ""; - replyTo(message, "Rehashing now."); - std::cout << "Rehashing at the behest of " << message->getFrom().toString() << std::endl; - resetCommands(); - listeners_.clear(); - if (rehashError_.empty()) { - replyTo(message, "Rehash complete"); - } else { - replyTo(message, "I have suffered a tremendous failure: " + rehashError_); - } -} - -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); - if (!room.isValid() || !room.getResource().empty() || room.getNode().empty()) { - replyTo(message, "Can't join " + room.toString() + ", not a valid room JID."); - return; - } - if (mucs_->contains(room)) { - replyTo(message, "I'm already (trying to be?) in " + room.toString() + "."); - return; - } - replyTo(message, "Trying to join " + room.toString() + "."); - mucs_->join(room, boost::bind(&Commands::handleJoinCommandSuccess, this, room, message), boost::bind(&Commands::handleJoinCommandFailure, this, room, _1, message)); -} - -void Commands::handlePartCommand(const std::string& /*command*/, const std::string& params, Swift::Message::ref message) { - Swift::JID room(params); - if (!room.isValid() || !room.getResource().empty() || room.getNode().empty()) { - replyTo(message, "Can't leave " + room.toString() + ", not a valid room JID."); - return; - } - if (mucs_->contains(room)) { - replyTo(message, "I'm not in " + room.toString() + "."); - return; - } - replyTo(message, "Leaving " + room.toString() + "."); - mucs_->part(room); -} - -void Commands::handleJoinCommandSuccess(const Swift::JID& room, Swift::Message::ref message) { - replyTo(message, "Joined " + room.toString()); -} - -void Commands::handleJoinCommandFailure(const Swift::JID& room, const std::string& error, Swift::Message::ref message) { - replyTo(message, "Join to " + room.toString() + "failed. " + error); -} - -void Commands::handleHelpCommand(const std::string& /*command*/, const std::string& /*params*/, Swift::Message::ref message) { - Users::User::Role userRole = users_->getRoleForSender(message); - std::string result("Available commands:"); - std::string bang = message->getType() == Swift::Message::Groupchat ? "\n!" : "\n"; - foreach (NamedCommand pair, commands_) { - if (roleIn(userRole, pair.second->getAllowedBy())) { - result += bang + pair.first + " - " + pair.second->getDescription(); - } - } - replyTo(message, result, true); -} - -/** - * \param outOfMUC Reply to the sender directly, don't spam MUCs with the reply - */ -void Commands::replyTo(Swift::Message::ref source, std::string replyBody, bool outOfMUC) { - Swift::Message::ref reply(new Swift::Message()); - Swift::Message::Type type = source->getType(); - reply->setType(type); - reply->setBody(type == Swift::Message::Groupchat ? source->getFrom().getResource() + ": " + replyBody : replyBody); - Swift::JID to = source->getFrom(); - if (type == Swift::Message::Groupchat) { - if (outOfMUC) { - reply->setType(Swift::Message::Chat); - } else { - to = to.toBare(); - } - } - reply->setTo(to); - if (client_->isAvailable()) { - client_->sendMessage(reply); - } else { - std::cout << "Dropping '" + reply->getBody().get_value_or("") + "' -> " + reply->getTo().toString() + " on the floor due to missing connection." << std::endl; - } -} diff --git a/Swiftob/Commands.h b/Swiftob/Commands.h deleted file mode 100644 index 5042517..0000000 --- a/Swiftob/Commands.h +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (c) 2011 Isode Limited. - * All rights reserved. - * See the COPYING file for more information. - */ - -#pragma once - -#include <map> -#include <string> - -#include <Swiften/Base/boost_bsignals.h> -#include <Swiften/Elements/Message.h> - -#include <Swiftob/Users.h> -#include <Swiftob/MUCs.h> - -namespace Swift { - class Client; -} - -class Storage; -class Commands { - typedef boost::function<void(Swift::Message::ref)> ListenerCallback; - public: - enum RoleList {Anyone, Owner}; - public: - class Command { - public: - Command(RoleList allowedBy, const std::string& description) : allowedBy_(allowedBy), description_(description) { - } - - virtual ~Command() {}; - - boost::signal<void (const std::string&, const std::string&, Swift::Message::ref)> onReceived; - RoleList getAllowedBy() {return allowedBy_;} - std::string getDescription() {return description_;} - private: - RoleList allowedBy_; - std::string description_; - }; - - public: - Commands(Users* users, Swift::Client* client, Storage* storage, MUCs* mucs); - ~Commands(); - bool hasCommand(const std::string&); - bool runCommand(const std::string& command, const std::string& params, Swift::Message::ref message); - void runListeners(Swift::Message::ref message); - void replyTo(Swift::Message::ref source, std::string replyBody, bool outOfMUC = false); - void registerCommand(const std::string& name, RoleList roles, const std::string& description, boost::function<void(const std::string& /*command*/, const std::string& /*params*/, Swift::Message::ref)> callback); - void registerListener(ListenerCallback); - void resetCommands(); - void setRehashError(const std::string& error); - - public: - boost::signal<void ()> onReset; - boost::signal<void ()> onRestartRequested; - private: - void clearCommands(); - bool roleIn(const Users::User::Role userRole, RoleList roles); - void handleQuitCommand(const std::string& command, const std::string& params, Swift::Message::ref message); - void handleHelpCommand(const std::string& command, const std::string& params, Swift::Message::ref message); - void handleJoinCommand(const std::string& /*command*/, const std::string& params, Swift::Message::ref message); - void handleJoinCommandSuccess(const Swift::JID& room, Swift::Message::ref message); - void handleJoinCommandFailure(const Swift::JID& room, const std::string& error, Swift::Message::ref message); - void handlePartCommand(const std::string& /*command*/, const std::string& params, Swift::Message::ref message); - void handleRehashCommand(const std::string& command, const std::string& params, Swift::Message::ref message); - void handleRestartCommand(const std::string& command, const std::string& params, Swift::Message::ref message); - void handleChangeNick(const std::string& command, const std::string& params, Swift::Message::ref message); - void handleChangeOwner(const std::string& command, const std::string& params, Swift::Message::ref message); - private: - std::map<std::string, Command*> commands_; - std::vector<ListenerCallback> listeners_; - Users* users_; - Swift::Client* client_; - Storage* storage_; - MUCs* mucs_; - std::string rehashError_; -}; - - diff --git a/Swiftob/LuaCommands.cpp b/Swiftob/LuaCommands.cpp deleted file mode 100644 index 18535f3..0000000 --- a/Swiftob/LuaCommands.cpp +++ /dev/null @@ -1,465 +0,0 @@ -/* - * Copyright (c) 2011-2016 Isode Limited. - * All rights reserved. - * See the COPYING file for more information. - */ - -#include <Swiftob/LuaCommands.h> - -#include <boost/bind.hpp> -#include <vector> -#include <algorithm> -#include <iostream> - -#include <Swiften/Base/foreach.h> -#include <Swiften/Client/Client.h> -#include <Swiften/Network/TimerFactory.h> -#include <boost/filesystem/operations.hpp> -#include <Swiften/Base/Path.h> - -#include <Swiftob/Commands.h> - -#include <Swiften/Base/BoostFilesystemVersion.h> - -#define LUA_COMMANDS "__Lua_Commands" -#define STORAGE "__Storage" - -static const luaL_Reg defaultLibraries[] = { - {"", luaopen_base}, - {LUA_LOADLIBNAME, luaopen_package}, - {LUA_TABLIBNAME, luaopen_table}, - {LUA_IOLIBNAME, luaopen_io}, - {LUA_OSLIBNAME, luaopen_os}, - {LUA_STRLIBNAME, luaopen_string}, - {LUA_MATHLIBNAME, luaopen_math}, - {LUA_DBLIBNAME, luaopen_debug}, - {NULL, NULL} -}; - -static void initialize(lua_State* L) { - lua_gc(L, LUA_GCSTOP, 0); - for (const luaL_Reg* lib = defaultLibraries; lib->func; lib++) { -#if LUA_VERSION_NUM >= 502 - luaL_requiref(L, lib->name, lib->func, 1); - lua_pop(L, 1); -#else - lua_pushcfunction(L, lib->func); - lua_pushstring(L, lib->name); - lua_call(L, 1, 0); -#endif - } - lua_gc(L, LUA_GCRESTART, 0); -} - -LuaCommands::LuaCommands(Commands* commands, const std::string& path, Client* client, TimerFactory* timerFactory, MUCs* mucs) : path_(path), scriptsPath_(boost::filesystem::path(path_) / "scripts") { - commands_ = commands; - client_ = client; - timerFactory_ = timerFactory; - mucs_ = mucs; - commands_->onReset.connect(boost::bind(&LuaCommands::registerCommands, this)); - registerCommands(); -} - -void LuaCommands::registerCommands() { - std::cout << "Trying to load all scripts in " << scriptsPath_ << std::endl; - if (boost::filesystem::exists(scriptsPath_) && boost::filesystem::is_directory(scriptsPath_)) { - std::vector<boost::filesystem::path> files; - copy(boost::filesystem::directory_iterator(scriptsPath_), boost::filesystem::directory_iterator(), std::back_inserter(files)); - foreach (boost::filesystem::path file, files) { - if (boost::filesystem::is_regular_file(file) && file.extension() == ".lua") { - loadScript(file); - } - } - } -} - -static int l_register_listener(lua_State *L) { - LuaCommands* commands = NULL; - lua_getfield(L, LUA_REGISTRYINDEX, LUA_COMMANDS); - commands = static_cast<LuaCommands*>(lua_touserdata(L, -1)); - lua_pop(L, 1); - if (!lua_isfunction(L, 1)) { - return luaL_error(L, "register_listener parameter must be a callback function"); - } - lua_pushvalue(L, 1); - int callbackIndex = luaL_ref(L, LUA_REGISTRYINDEX); - lua_pop(L, 1); - commands->getCommands()->registerListener(boost::bind(&LuaCommands::handleLuaListener, commands, callbackIndex, L, _1)); - return 0; -} - -static int l_register_command(lua_State *L) { - LuaCommands* commands = NULL; - lua_getfield(L, LUA_REGISTRYINDEX, LUA_COMMANDS); - commands = static_cast<LuaCommands*>(lua_touserdata(L, -1)); - lua_pop(L, 1); - if (!lua_isfunction(L, 4)) { - return luaL_error(L, "register_command callback parameter must be a function"); - } - //luaL_ref callback(lua_to(L, 4)); - lua_pushvalue(L, 4); - int callbackIndex = luaL_ref(L, LUA_REGISTRYINDEX); - lua_pop(L, 1); - - if (!lua_isstring(L, 3)) { - return luaL_error(L, "register_command description parameter must be a string"); - } - std::string description(lua_tostring(L, 3)); - lua_pop(L, 1); - - if (!lua_isstring(L, 2)) { - return luaL_error(L, "register_command allowed roles parameter must be a string"); - } - std::string roleString(lua_tostring(L, 2)); - lua_pop(L, 1); - Commands::RoleList roleList = Commands::Owner; - if (roleString == "Owner") { - roleList = Commands::Owner; - } else if (roleString == "Anyone") { - roleList = Commands::Anyone; - } else { - return luaL_error(L, "register_command allowed roles parameter has illegal value"); - } - if (!lua_isstring(L, 1)) { - return luaL_error(L, "register_command command name parameter must be a string"); - } - std::string name(lua_tostring(L, 1)); - lua_pop(L, 1); - std::cout << "Registering lua command '" << name << "' for '" << roleString << "' with callback index " << callbackIndex << std::endl; - commands->getCommands()->registerCommand(name, roleList, description, boost::bind(&LuaCommands::handleLuaCommand, commands, callbackIndex, L, _1, _2, _3)); - - return 0; -} - -static std::string luatable_asstring(lua_State *L, const char* key) { - lua_getfield(L, -1, key); - const char* valueChars = lua_tostring(L, -1); - std::string value(valueChars != NULL ? valueChars : ""); - lua_pop(L, 1); - return value; -} - -static int luatable_asint(lua_State *L, const char* key) { - lua_getfield(L, -1, key); - int value = lua_tointeger(L, -1); - lua_pop(L, 1); - return value; -} - -static int luatable_asfunction(lua_State *L, const char* key) { - lua_getfield(L, -1, key); - int callbackIndex = luaL_ref(L, LUA_REGISTRYINDEX); - return callbackIndex; -} - -static Message::ref messageFromTable(lua_State *L) { - Message::ref message(new Message()); - message->setFrom(JID(luatable_asstring(L, "from"))); - message->setBody(luatable_asstring(L, "body")); - Message::Type type = Message::Normal; - std::string typeString(luatable_asstring(L, "type")); - if (typeString == "normal") { - type = Message::Normal; - } else if (typeString == "chat") { - type = Message::Chat; - } else if (typeString == "groupchat") { - type = Message::Groupchat; - } else if (typeString == "error") { - type = Message::Error; - } else if (typeString == "headline") { - type = Message::Headline; - } else { - return Message::ref(); - } - message->setType(type); - return message; -} - -LuaCommands* LuaCommands::commandsFromLua(lua_State *L) { - LuaCommands* commands = NULL; - lua_getfield(L, LUA_REGISTRYINDEX, LUA_COMMANDS); - commands = static_cast<LuaCommands*>(lua_touserdata(L, -1)); - lua_pop(L, 1); - return commands; -} - -Storage* LuaCommands::storageFromLua(lua_State *L) { - Storage* storage = NULL; - lua_getfield(L, LUA_REGISTRYINDEX, STORAGE); - storage = static_cast<Storage*>(lua_touserdata(L, -1)); - lua_pop(L, 1); - return storage; -} - -static int l_reply_to(lua_State *L) { - LuaCommands* commands = LuaCommands::commandsFromLua(L); - - if (!lua_isboolean(L, 3) && lua_gettop(L) > 2) { - return luaL_error(L, "reply_to parameter 3 must be boolean if present"); - } - bool outOfMUC = lua_toboolean(L, 3); - if (lua_gettop(L) == 3) { - lua_pop(L, 1); - } - - if (!lua_isstring(L, 2)) { - return luaL_error(L, "reply_to body parameter must be a string"); - } - std::string body(lua_tostring(L, 2)); - lua_pop(L, 1); - - if (!lua_istable(L, 1)) { - return luaL_error(L, "reply_to message parameter must be a table"); - } - lua_pushvalue(L, 1); - Message::ref message(messageFromTable(L)); - if (!message) { - return luaL_error(L, "message parameter invalid"); - } - commands->getCommands()->replyTo(message, body, outOfMUC); - lua_pop(L, 1); - - return 0; -} - -static int l_muc_input_to_jid(lua_State *L) { - LuaCommands* commands = LuaCommands::commandsFromLua(L); - return commands->muc_input_to_jid(L); -} - -int LuaCommands::muc_input_to_jid(lua_State *L) { - if (!lua_isstring(L, 2)) { - return luaL_error(L, "must pass a string to muc_input_to_jid p2"); - } - std::string source = lua_tostring(L, 2); - JID sourceJID(source); - lua_pop(L, 1); - if (!lua_isstring(L, 1)) { - return luaL_error(L, "must pass a string to muc_input_to_jid p1"); - } - std::string input = lua_tostring(L, 1); - lua_pop(L, 1); - JID result(input); - if (mucs_->contains(sourceJID.toBare())) { - if (result.isBare() && result.getNode().empty()) { - if (mucs_->getMUC(sourceJID.toBare())->hasOccupant(input)) { - result = JID(sourceJID.getNode(), sourceJID.getDomain(), input); - } - } - } - - lua_pushstring(L, result.isValid() ? result.toString().c_str() : ""); - return 1; -} - -void LuaCommands::handleSoftwareVersionResponse(boost::shared_ptr<SoftwareVersion> version, ErrorPayload::ref error, bool timeout, GetSoftwareVersionRequest::ref request, Timer::ref timer, lua_State* L, Callbacks callbacks) { - request->onResponse.disconnect_all_slots(); - timer->onTick.disconnect_all_slots(); - timer->stop(); - int callback = callbacks.failure; - int stackCount = 0; - if (timeout) { - callback = callbacks.timeout; - } else if (version) { - callback = callbacks.success; - } - lua_rawgeti(L, LUA_REGISTRYINDEX, callback); - if (error) { - lua_pushstring(L, error->getText().empty() ? "No error text" : error->getText().c_str()); - stackCount++; - } - else if (version) { - lua_createtable(L, 0, 3); - lua_pushstring(L, version->getName().c_str()); - lua_setfield(L, -2, "name"); - lua_pushstring(L, version->getVersion().c_str()); - lua_setfield(L, -2, "version"); - lua_pushstring(L, version->getOS().c_str()); - lua_setfield(L, -2, "os"); - stackCount++; - } - else { - lua_pushliteral(L, "Missing payload"); - stackCount++; - } - int result = lua_pcall(L, stackCount, 0, 0); - if (result != 0) { - std::string error(lua_tostring(L, -1)); - lua_pop(L, 1); - std::cout << error << std::endl; - callbacks.erase(L); - luaL_error(L, error.c_str()); - } else { - callbacks.erase(L); - } - -} - -static int l_get_software_version(lua_State *L) { - LuaCommands* commands = LuaCommands::commandsFromLua(L); - return commands->get_software_version(L); -} - -int LuaCommands::get_software_version(lua_State *L) { - if (!lua_istable(L, 1)) { - return luaL_error(L, "get_software_version requires a table parameter."); - } - lua_pushvalue(L, 1); - JID to(luatable_asstring(L, "to")); - if (!to.isValid()) { - return luaL_error(L, "invalid JID."); - } - int timeout = luatable_asint(L, "timeout"); - if (timeout == 0) { - return luaL_error(L, "invalid timeout."); - } - - int successCallback = luatable_asfunction(L, "success_callback"); - int failureCallback = luatable_asfunction(L, "failure_callback"); - int timeoutCallback = luatable_asfunction(L, "timeout_callback"); - GetSoftwareVersionRequest::ref request = GetSoftwareVersionRequest::create(to, client_->getIQRouter()); - Timer::ref timer = timerFactory_->createTimer(timeout * 1000); - Callbacks callbacks(successCallback, failureCallback, timeoutCallback); - request->onResponse.connect(boost::bind(&LuaCommands::handleSoftwareVersionResponse, this, _1, _2, false, request, timer, L, callbacks)); - boost::shared_ptr<SoftwareVersion> fakePayload; - ErrorPayload::ref fakeError; - timer->onTick.connect(boost::bind(&LuaCommands::handleSoftwareVersionResponse, this, fakePayload, fakeError, true, request, timer, L, callbacks)); - timer->start(); - request->send(); - return 1; -} - -int LuaCommands::muc_kick(lua_State *L) { - if (!lua_isstring(L, 2)) { - return luaL_error(L, "muc_kick requires a nick to kick"); - } - std::string nick = lua_tostring(L, 2); - if (!lua_isstring(L, 1)) { - return luaL_error(L, "muc_kick requires a muc to kick from"); - } - JID mucJID(lua_tostring(L, 1)); - MUC::ref muc = mucs_->getMUC(mucJID); - muc->kickOccupant(JID(mucJID.getNode(), mucJID.getDomain(), nick)); - return 0; -} - -static int l_muc_kick(lua_State *L) { - LuaCommands* commands = LuaCommands::commandsFromLua(L); - return commands->muc_kick(L); -} - -static int l_store_setting(lua_State *L) { - return LuaCommands::commandsFromLua(L)->store_setting(L); -} - -static int l_get_setting(lua_State *L) { - return LuaCommands::commandsFromLua(L)->get_setting(L); -} - -int LuaCommands::store_setting(lua_State *L) { - if (!lua_isstring(L, 2) || !lua_isstring(L, 1)) { - return luaL_error(L, "both setting and key must be strings"); - } - std::string value(lua_tostring(L, 2)); - std::string key(lua_tostring(L, 1)); - lua_pop(L, 2); - storageFromLua(L)->saveSetting(key, value); - return 0; -} - -int LuaCommands::get_setting(lua_State *L) { - if (!lua_isstring(L, 1)) { - return luaL_error(L, "key must be a string"); - } - std::string key(lua_tostring(L, 1)); - lua_pop(L, 1); - lua_pushstring(L, storageFromLua(L)->getSetting(key).c_str()); - return 1; - -} - -void LuaCommands::handleLuaListener(int callbackIndex, lua_State* L, Swift::Message::ref message) { - lua_rawgeti(L, LUA_REGISTRYINDEX, callbackIndex); - lua_pushstring(L, message->getBody().get_value_or("").c_str()); - lua_pushstring(L, message->getFrom().toBare().toString().c_str()); - lua_pushstring(L, message->getFrom().getResource().c_str()); - messageOntoStack(message, L); - int result = lua_pcall(L, 4, 0, 0); - if (result != 0) { - std::string error(lua_tostring(L, -1)); - lua_pop(L, 1); - error = "Listener failed: " + error; - std::cout << error << std::endl; - } -} - -void LuaCommands::handleLuaCommand(int callbackIndex, lua_State* L, const std::string& command, const std::string& params, Swift::Message::ref message) { - lua_rawgeti(L, LUA_REGISTRYINDEX, callbackIndex); - lua_pushstring(L, command.c_str()); - lua_pushstring(L, params.c_str()); - messageOntoStack(message, L); - int result = lua_pcall(L, 3, 0, 0); - if (result != 0) { - std::string error(lua_tostring(L, -1)); - lua_pop(L, 1); - error = "Command '" + command + "' failed: " + error; - std::cout << error << std::endl; - commands_->replyTo(message, error, false); - } -} - -void LuaCommands::messageOntoStack(Swift::Message::ref message, lua_State* L) { - lua_createtable(L, 0, 4); - std::string typeString; - switch (message->getType()) { - case Message::Chat : typeString = "chat";break; - case Message::Groupchat : typeString = "groupchat";break; - case Message::Normal : typeString = "normal";break; - case Message::Error : typeString = "error";break; - case Message::Headline : typeString = "headline";break; - } - lua_pushstring(L, typeString.c_str()); - lua_setfield(L, -2, "type"); - lua_pushstring(L, message->getFrom().toString().c_str()); - lua_setfield(L, -2, "from"); - lua_pushstring(L, message->getFrom().toBare().toString().c_str()); - lua_setfield(L, -2, "frombare"); - lua_pushstring(L, message->getTo().toString().c_str()); - lua_setfield(L, -2, "to"); - lua_pushstring(L, message->getBody().get_value_or("").c_str()); - lua_setfield(L, -2, "body"); -} - -void LuaCommands::loadScript(boost::filesystem::path filePath) { - std::cout << "Trying to load file from " << filePath << std::endl; - lua_State* lua = luaL_newstate(); - initialize(lua); - lua_pushlightuserdata(lua, this); - lua_setfield(lua, LUA_REGISTRYINDEX, LUA_COMMANDS); -#if BOOST_FILESYSTEM_VERSION == 2 // TODO: Delete this when boost 1.44 becomes a minimum requirement, and we no longer need v2 - std::string filename = filePath.filename(); -#else - std::string filename = filePath.filename().string(); -#endif - filename += ".storage"; - boost::filesystem::path storagePath(boost::filesystem::path(path_) / stringToPath(filename)); - Storage* storage = new Storage(storagePath); - lua_pushlightuserdata(lua, storage); - lua_setfield(lua, LUA_REGISTRYINDEX, STORAGE); - lua_register(lua, "swiftob_register_command", &l_register_command); - lua_register(lua, "swiftob_register_listener", &l_register_listener); - lua_register(lua, "swiftob_reply_to", &l_reply_to); - lua_register(lua, "swiftob_get_software_version", &l_get_software_version); - lua_register(lua, "swiftob_muc_input_to_jid", &l_muc_input_to_jid); - lua_register(lua, "swiftob_store_setting", &l_store_setting); - lua_register(lua, "swiftob_get_setting", &l_get_setting); - lua_register(lua, "swiftob_muc_kick", &l_muc_kick); - int fileLoaded = luaL_dofile(lua, filePath.string().c_str()); - if (fileLoaded == 0 ) { - std::cout << "Loaded" << std::endl; - } else { - const char* error = lua_tostring(lua, -1); - std::cout << "Error: " << error << std::endl; - lua_pop(lua, -1); - } -} diff --git a/Swiftob/LuaCommands.h b/Swiftob/LuaCommands.h deleted file mode 100644 index 2ade6b3..0000000 --- a/Swiftob/LuaCommands.h +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (c) 2011 Isode Limited. - * All rights reserved. - * See the COPYING file for more information. - */ - -#pragma once - -#include <string> -#include <vector> - -#include <lua.hpp> -#include <boost/filesystem/fstream.hpp> -#include <boost/noncopyable.hpp> -#include <Swiften/Network/NetworkFactories.h> -#include <Swiften/Elements/SoftwareVersion.h> -#include <Swiften/Queries/Requests/GetSoftwareVersionRequest.h> -#include <Swiften/Network/Timer.h> - -#include <Swiftob/Commands.h> -#include <Swiftob/Storage.h> - -using namespace Swift; -/** - * Yes, there's an odd naming scheme going on here for methods. - * normalCamelCase methods are methods called from C++ - * lower_case_methods are exposed to Lua through wrappers and - * l_lower_case_functions are functions directly exposed (often calling the lower_case_methods). - */ -class LuaCommands { - public: - class LuaCommand : public Commands::Command, boost::noncopyable { - public: - /** Takes ownership of lua and storage.*/ - LuaCommand(Commands::RoleList allowedBy, const std::string& description, lua_State* lua, Storage* storage) : Command(allowedBy, description), lua_(lua), storage_(storage) { - } - - virtual ~LuaCommand() { - lua_close(lua_); - delete storage_; - }; - - private: - lua_State* lua_; - Storage* storage_; - }; - - class Callbacks { - public: - Callbacks(int success, int failure, int timeout) : success(success), failure(failure), timeout(timeout) {}; - int success; - int failure; - int timeout; - void erase(lua_State *L) { - lua_pushnil(L); - lua_rawseti(L, LUA_REGISTRYINDEX, success); - lua_pushnil(L); - lua_rawseti(L, LUA_REGISTRYINDEX, failure); - lua_pushnil(L); - lua_rawseti(L, LUA_REGISTRYINDEX, timeout); - } - }; - - LuaCommands(Commands* commands, const std::string& path, Client* client, TimerFactory* timerFactory, MUCs* mucs); - /* Public but aren't really part of the API */ - void handleLuaCommand(int callbackIndex, lua_State* L, const std::string& command, const std::string& params, Message::ref message); - void handleLuaListener(int callbackIndex, lua_State* L, Message::ref message); - Commands* getCommands() {return commands_;} - int get_software_version(lua_State *L); - int muc_input_to_jid(lua_State *L); - int store_setting(lua_State *L); - int get_setting(lua_State *L); - int muc_kick(lua_State *L); - static LuaCommands* commandsFromLua(lua_State *L); - static Storage* storageFromLua(lua_State *L); - private: - void registerCommands(); - void loadScript(boost::filesystem::path filePath); - void messageOntoStack(Message::ref message, lua_State* L); - void handleSoftwareVersionResponse(boost::shared_ptr<SoftwareVersion> version, ErrorPayload::ref error, bool timeout, GetSoftwareVersionRequest::ref request, Timer::ref timer, lua_State* L, Callbacks callbacks); - private: - std::string path_; - boost::filesystem::path scriptsPath_; - Commands* commands_; - MUCs* mucs_; - Client* client_; - TimerFactory* timerFactory_; -}; - - diff --git a/Swiftob/MUCs.cpp b/Swiftob/MUCs.cpp deleted file mode 100644 index 23099d3..0000000 --- a/Swiftob/MUCs.cpp +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright (c) 2011 Isode Limited. - * All rights reserved. - * See the COPYING file for more information. - */ - -#include <Swiftob/MUCs.h> - -#include <boost/bind.hpp> -#include <boost/date_time/posix_time/posix_time.hpp> - -#include <Swiften/Base/foreach.h> -#include <Swiften/Client/Client.h> -#include <Swiften/MUC/MUC.h> -#include <Swiften/MUC/MUCManager.h> -#include <Swiften/Base/String.h> - -#include <Swiftob/Storage.h> - -using namespace Swift; - -#define MUC_LIST_SETTING "muc_list" -#define NICK "default_nick" - -typedef std::pair<JID, MUC::ref> JIDMUCPair; - -MUCs::MUCs(Client* client, Storage* storage) : defaultNick_("Kanchil+") { - client_ = client; - storage_ = storage; - std::string storedNick = storage_->getSetting(NICK); - if (!storedNick.empty()) { - defaultNick_ = storedNick; - } - client_->onConnected.connect(boost::bind(&MUCs::handleConnected, this)); -} - -void MUCs::handleConnected() { - foreach (std::string room, String::split(storage_->getSetting(MUC_LIST_SETTING), ' ')) { - join(JID(room), boost::bind(&MUCs::handleInitialJoinSuccess, this), boost::bind(&MUCs::handleInitialJoinFailure, this, _1)); - } -} - -void MUCs::handleInitialJoinSuccess() { - -} - -void MUCs::handleInitialJoinFailure(const std::string&) { - -} - -void MUCs::join(const JID& room, boost::signal<void (const std::string&)>::slot_type successCallback, boost::function<void(const std::string& /*reason*/)> failureCallback) { - if (contains(room)) { - failureCallback("Already in room"); - } - mucs_[room] = client_->getMUCManager()->createMUC(room); - mucs_[room]->onJoinComplete.connect(successCallback); - mucs_[room]->onJoinFailed.connect(boost::bind(&MUCs::handleJoinFailed, this, room, _1, failureCallback)); - mucs_[room]->joinWithContextSince(defaultNick_, boost::posix_time::microsec_clock::universal_time()); - save(); -} - -void MUCs::part(const JID& room) { - if (!contains(room)) { - return; - } - mucs_[room]->part(); -} - -bool MUCs::contains(const JID& room) { - return mucs_.find(room) != mucs_.end(); -} - -void MUCs::handleJoinFailed(const JID& muc, ErrorPayload::ref error, boost::function<void(const std::string& /*reason*/)> failureCallback) { - std::string errorMessage("Couldn't join room"); - if (error) { - switch (error->getCondition()) { - case ErrorPayload::Conflict: - // rejoinNick = nick_ + "_"; - errorMessage += ": Nickname in use"; - // errorMessage = str(format("Unable to enter this room as %1%, retrying as %2%") % nick_ % rejoinNick); - break; - case ErrorPayload::JIDMalformed: - errorMessage += ": "; - errorMessage += "No nickname specified"; - break; - case ErrorPayload::NotAuthorized: - errorMessage += ": "; - errorMessage += "A password needed"; - break; - case ErrorPayload::RegistrationRequired: - errorMessage += ": "; - errorMessage += "Only members may enter"; - break; - case ErrorPayload::Forbidden: - errorMessage += ": "; - errorMessage += "You are banned from the room"; - break; - case ErrorPayload::ServiceUnavailable: - errorMessage += ": "; - errorMessage += "The room is full"; - break; - case ErrorPayload::ItemNotFound: - errorMessage += ": "; - errorMessage += "The room does not exist"; - break; - default: - break; - } - if (!error->getText().empty()) { - errorMessage += " - " + error->getText(); - } - } - mucs_.erase(muc); - failureCallback(errorMessage); -} - -void MUCs::save() { - std::string concat; - foreach (JIDMUCPair pair, mucs_) { - concat += pair.first.toString() + " "; - } - storage_->saveSetting(MUC_LIST_SETTING, concat); -} - -MUC::ref MUCs::getMUC(const JID& room) { - return (mucs_.find(room) != mucs_.end()) ? mucs_[room] : MUC::ref(); -} - -bool MUCs::setDefaultNick(const std::string& nick) { - JID testJID("alice", "wonderland.lit", nick); - if (testJID.isValid()) { - defaultNick_ = testJID.getResource(); - storage_->saveSetting(NICK, defaultNick_); - return true; - } - return false; -} diff --git a/Swiftob/MUCs.h b/Swiftob/MUCs.h deleted file mode 100644 index 3acd207..0000000 --- a/Swiftob/MUCs.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (c) 2011 Isode Limited. - * All rights reserved. - * See the COPYING file for more information. - */ - -#pragma once - -#include <vector> -#include <string> - -#include <Swiften/JID/JID.h> -#include <Swiften/Elements/Message.h> -#include <Swiften/MUC/MUC.h> -#include <Swiften/MUC/MUCRegistry.h> - -namespace Swift { -class Client; -class MUC; -} - -class Storage; - -class MUCs { - public: - MUCs(Swift::Client* client, Storage* storage); - void join(const Swift::JID& room, boost::signal<void (const std::string&)>::slot_type successCallback, boost::function<void(const std::string& /*reason*/)> failureCallback); - void part(const Swift::JID& room); - bool contains(const Swift::JID& room); - Swift::MUC::ref getMUC(const Swift::JID& room); - const std::string& getDefaultNick() const {return defaultNick_;} - bool setDefaultNick(const std::string& nick); - private: - void handleConnected(); - void handleJoinFailed(const Swift::JID& room, Swift::ErrorPayload::ref error, boost::function<void(const std::string& /*reason*/)> failureCallback); - void handleInitialJoinSuccess(); - void handleInitialJoinFailure(const std::string&); - void save(); - private: - Swift::MUCRegistry registry_; - std::map<Swift::JID, Swift::MUC::ref> mucs_; - Swift::Client* client_; - Storage* storage_; - std::string defaultNick_; -}; - - diff --git a/Swiftob/SConscript b/Swiftob/SConscript deleted file mode 100644 index b78ade9..0000000 --- a/Swiftob/SConscript +++ /dev/null @@ -1,24 +0,0 @@ -Import("env") - -if env["SCONS_STAGE"] == "build" and not GetOption("help") and not env.get("HAVE_LUA", 0) : - print "Warning: Lua was not found. Swiftob will not be built." - if "Sluift" in env["PROJECTS"] : - env["PROJECTS"].remove("Sluift") - -elif env["SCONS_STAGE"] == "build": - myenv = env.Clone() - # Too many compile warnings here at the moment - myenv.Replace(CXXFLAGS = [flag for flag in env["CXXFLAGS"] if flag != "-Weverything"]) - myenv.UseFlags(myenv.get("LUA_FLAGS", {})) - myenv.UseFlags(myenv["SWIFTEN_FLAGS"]) - myenv.UseFlags(myenv["SWIFTEN_DEP_FLAGS"]) - sources = [ - "Swiftob.cpp", - "Users.cpp", - "Commands.cpp", - "MUCs.cpp", - "Storage.cpp", - "LuaCommands.cpp", - "main.cpp" - ] - swiftob = myenv.Program("swiftob", sources) diff --git a/Swiftob/Storage.cpp b/Swiftob/Storage.cpp deleted file mode 100644 index 86debbe..0000000 --- a/Swiftob/Storage.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2011 Isode Limited. - * All rights reserved. - * See the COPYING file for more information. - */ - -#include <Swiftob/Storage.h> - -#include <boost/filesystem/operations.hpp> - -#include <Swiften/Base/String.h> -#include <Swiften/Base/ByteArray.h> -#include <Swiften/Base/foreach.h> - -typedef std::pair<std::string, std::string> Strings; - -Storage::Storage(const std::string& path) : settingsPath_(boost::filesystem::path(path)) { - load(); -} - -Storage::Storage(const boost::filesystem::path& path) : settingsPath_(path) { - load(); -} - -void Storage::load() { - if (boost::filesystem::exists(settingsPath_)) { - Swift::ByteArray data; - Swift::readByteArrayFromFile(data, settingsPath_); - foreach (std::string line, Swift::String::split(Swift::byteArrayToString(data), '\n')) { - std::pair<std::string, std::string> pair = Swift::String::getSplittedAtFirst(line, '\t'); - settings_[pair.first] = pair.second; - } - } -} - -void Storage::saveSetting(const std::string& setting, const std::string& value) { - settings_[setting] = value; - std::string settingsString; - foreach(Strings pair, settings_) { - settingsString += pair.first + '\t' + pair.second + '\n'; - } - boost::filesystem::ofstream file(settingsPath_); - file << settingsString; - file.close(); -} - -std::string Storage::getSetting(const std::string& setting) { - return settings_[setting]; -} diff --git a/Swiftob/Storage.h b/Swiftob/Storage.h deleted file mode 100644 index ba36c84..0000000 --- a/Swiftob/Storage.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2011 Isode Limited. - * All rights reserved. - * See the COPYING file for more information. - */ - -#pragma once - -#include <map> -#include <string> - -#include <boost/filesystem/fstream.hpp> -#include <boost/filesystem/path.hpp> - -class Storage { - public: - Storage(const std::string& path); - Storage(const boost::filesystem::path& path); - void saveSetting(const std::string& setting, const std::string& value); - std::string getSetting(const std::string& setting); - private: - void load(); - std::map<std::string, std::string> settings_; - std::string path_; - boost::filesystem::path settingsPath_; -}; - - diff --git a/Swiftob/Swiftob.cpp b/Swiftob/Swiftob.cpp deleted file mode 100644 index d534479..0000000 --- a/Swiftob/Swiftob.cpp +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Copyright (c) 2011-2016 Isode Limited. - * All rights reserved. - * See the COPYING file for more information. - */ - -#include <Swiftob/Swiftob.h> - -#include <iostream> -#include <string> - -#include <boost/bind.hpp> - -#include <Swiften/Base/String.h> -#include <Swiften/JID/JID.h> -#include <Swiften/Presence/PresenceSender.h> - -#include <Swiftob/Storage.h> -#include <Swiftob/Users.h> - -po::options_description Swiftob::getOptionsDescription() { - po::options_description result("Options"); - result.add_options() - ("path", po::value<std::string>(), "Configuration folder") - ("help", "produce help message") - ("init", "Reset everything (Really, everything, be careful, you only want to use this on first run).") - ("jid", po::value<std::string>(), "JID to use") - ("password", po::value<std::string>(), "password") - ("initial-owner", po::value<std::string>(), "Initial bot owner (JID)") - ; - return result; -} - -Swiftob::Swiftob(const po::variables_map& options) : options_(options), networkFactories_(&eventLoop_), quitting_(false) { - path_ = options["path"].as<std::string>(); - client_ = new Swift::Client(Swift::JID(options["jid"].as<std::string>()), options["password"].as<std::string>(), &networkFactories_); - storage_ = new Storage(boost::filesystem::path(path_) / "settings.txt"); - mucs_ = NULL; - users_ = NULL; - commands_ = NULL; - lua_ = NULL; - init(); - client_->onConnected.connect(boost::bind(&Swiftob::handleConnected, this)); - client_->onDisconnected.connect(boost::bind(&Swiftob::handleDisconnected, this, _1)); - client_->onMessageReceived.connect(boost::bind(&Swiftob::handleMessageReceived, this, _1)); - if (options_.count("init") > 0) { - - } else if (options_.count("jid") > 0 || options_.count("password") > 0 || options_.count("initial-owner") == 0) { - std::cout << "Ignoring initial config options without --initial" << std::endl; - } - client_->setAlwaysTrustCertificates(); - client_->setSoftwareVersion("Swiftob", "pregit", ""); - client_->connect(); - eventLoop_.run(); -} - -void Swiftob::init() { - delete mucs_; - mucs_ = new MUCs(client_, storage_); - delete users_; - users_ = new Users(client_, mucs_); - delete commands_; - commands_ = new Commands(users_, client_, storage_, mucs_); - commands_->onRestartRequested.connect(boost::bind(&Swiftob::handleRestartRequested, this)); - delete lua_; - lua_ = new LuaCommands(commands_, path_, client_, networkFactories_.getTimerFactory(), mucs_); -} - -void Swiftob::handleRestartRequested() { - client_->disconnect(); - init(); -} - -void Swiftob::handleConnected() { - std::cout << "Connected" << std::endl; - if (options_.count("init") > 0) {}{ /* FIXME: Not ready for persistence yet*/ - users_->clearAll(); - users_->addUser(Users::User(Swift::JID(options_["initial-owner"].as<std::string>()), Users::User::Owner)); - } - Swift::Presence::ref presence(new Swift::Presence()); - presence->setStatus("Online and botty"); - client_->getPresenceSender()->sendPresence(presence); -} - -void Swiftob::handleDisconnected(const boost::optional<Swift::ClientError>& /*error*/) { - std::cout << "Disconnected" << std::endl; - /* FIXME: check if last connect was more than a minute ago. If so, go ahead and connect, if not then wait a minute before connecting.*/ - if (quitting_) { - eventLoop_.stop(); - } else { - client_->connect(); - } -} - -void Swiftob::handleMessageReceived(Swift::Message::ref message) { - Swift::Message::Type type = message->getType(); - if (type == Swift::Message::Error || type == Swift::Message::Headline) { - std::cout << "Ignoring typed message" << std::endl; - return; - } - std::string body = message->getBody().get_value_or(""); - std::cout << "Got message with body " << body << std::endl; - if (body.size() == 0) { - std::cout << "Not handling empty body" << std::endl; - return; - } - - /* Run through any full-message listeners */ - commands_->runListeners(message); - - /*Convert body into !command if it's not a MUC, and it misses the bang*/ - std::string bangBody(body); - if (type != Swift::Message::Groupchat && body[0] != '!') { - bangBody = "!" + body; - } - std::cout << "After banging, body is " << bangBody << std::endl; - std::pair<std::string, std::string> split = Swift::String::getSplittedAtFirst(bangBody, ' '); - std::string commandName(split.first); - commandName = Swift::String::getSplittedAtFirst(commandName, '!').second; - /*FIXME: remove leading bang in commandName*/ - if (commands_->hasCommand(commandName)) { - std::cout << "Matched command " << commandName << std::endl; - commands_->runCommand(commandName, split.second, message); - } -} - -Swiftob::~Swiftob() { - delete lua_; - delete commands_; - delete storage_; - delete users_; - delete mucs_; - delete client_; -} - -int Swiftob::exec() { - return 0; -} - diff --git a/Swiftob/Swiftob.h b/Swiftob/Swiftob.h deleted file mode 100644 index 178f88b..0000000 --- a/Swiftob/Swiftob.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (c) 2011 Isode Limited. - * All rights reserved. - * See the COPYING file for more information. - */ - -#pragma once - -#include <boost/program_options/variables_map.hpp> -#include <boost/program_options/options_description.hpp> -#include <boost/shared_ptr.hpp> - -#include <string> - -#include <Swiften/Base/Platform.h> -#include <Swiften/EventLoop/SimpleEventLoop.h> -#include <Swiften/Base/boost_bsignals.h> -#include <Swiften/Client/Client.h> -#include <Swiften/Network/BoostNetworkFactories.h> - -#include <Swiftob/Commands.h> -#include <Swiftob/LuaCommands.h> - -namespace po = boost::program_options; - -class Users; -class Storage; -class Swiftob { - public: - Swiftob(const po::variables_map& options); - static po::options_description getOptionsDescription(); - int exec(); - ~Swiftob(); - private: - void init(); - void handleRestartRequested(); - void handleConnected(); - void handleDisconnected(const boost::optional<Swift::ClientError>&); - void handleMessageReceived(Swift::Message::ref); - private: - const po::variables_map options_; - Swift::SimpleEventLoop eventLoop_; - Swift::BoostNetworkFactories networkFactories_; - Commands* commands_; - LuaCommands* lua_; - Storage* storage_; - MUCs* mucs_; - bool quitting_; - Users* users_; - std::string path_; - Swift::Client* client_; -}; diff --git a/Swiftob/Users.cpp b/Swiftob/Users.cpp deleted file mode 100644 index fdaad03..0000000 --- a/Swiftob/Users.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (c) 2011 Isode Limited. - * All rights reserved. - * See the COPYING file for more information. - */ - -#include <Swiftob/Users.h> - -#include <iostream> - -#include <Swiften/Base/foreach.h> -#include <Swiften/Client/Client.h> - -#include <Swiftob/MUCs.h> - -using namespace Swift; - -Users::Users(Client* client, MUCs* mucs) { - client_ = client; - mucs_ = mucs; -} - -/* TODO: Store in roster */ -void Users::clearAll() { - users_.clear(); -} - -void Users::addUser(const User& user) { - users_.push_back(user); -} - -Users::User::Role Users::getRoleForSender(Message::ref message) { - JID jid = message->getFrom(); - MUC::ref muc = mucs_->getMUC(message->getFrom().toBare()); - if (muc && muc->hasOccupant(message->getFrom().getResource())) { - MUCOccupant occupant = muc->getOccupant(message->getFrom().getResource()); - if (occupant.getRealJID()) { - jid = occupant.getRealJID().get(); - } - } - foreach (User user, users_) { - if (user.getJID().equals(jid.toBare(), JID::WithoutResource)) { - return user.getRole(); - } - } - return User::Unknown; -} - diff --git a/Swiftob/Users.h b/Swiftob/Users.h deleted file mode 100644 index 9a44f86..0000000 --- a/Swiftob/Users.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (c) 2011 Isode Limited. - * All rights reserved. - * See the COPYING file for more information. - */ - -#pragma once - -#include <vector> -#include <string> - -#include <Swiften/JID/JID.h> -#include <Swiften/Elements/Message.h> - -namespace Swift { -class Client; -} - -class MUCs; - -class Users { - public: - class User { - public: - /* If you add a role here, edit the role lists in Commands.cpp*/ - enum Role {Unknown, Owner}; - User(const Swift::JID& jid, Role role) : jid_(jid), role_(role) {} - Role getRole() {return role_;} - Swift::JID getJID() {return jid_;} - private: - Swift::JID jid_; - Role role_; - }; - - public: - Users(Swift::Client* client, MUCs* mucs); - void clearAll(); - void addUser(const User& user); - User::Role getRoleForSender(Swift::Message::ref message); - - private: - std::vector<User> users_; - Swift::Client* client_; - MUCs* mucs_; -}; - - diff --git a/Swiftob/main.cpp b/Swiftob/main.cpp deleted file mode 100644 index 87713a9..0000000 --- a/Swiftob/main.cpp +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2011 Isode Limited. - * All rights reserved. - * See the COPYING file for more information. - */ - -#include <boost/program_options/options_description.hpp> -#include <boost/program_options/variables_map.hpp> -#include <boost/program_options.hpp> -#include <boost/version.hpp> -#include <iostream> - -#include "Swiftob.h" - -int main(int argc, char* argv[]) { - boost::program_options::options_description desc = Swiftob::getOptionsDescription(); - boost::program_options::variables_map vm; - try { - boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm); - } catch (const boost::program_options::unknown_option& option) { -#if BOOST_VERSION >= 104200 - std::cout << "Ignoring unknown option " << option.get_option_name() << " but continuing." << std::endl; -#else - std::cout << "Error: " << option.what() << " (continuing)" << std::endl; -#endif - } - boost::program_options::notify(vm); - if (vm.count("help") > 0) { - std::cout << desc << "\n"; - return 1; - } - - Swiftob bot(vm); - int result = bot.exec(); - - return result; -} diff --git a/Swiftob/scripts/agenda.lua b/Swiftob/scripts/agenda.lua deleted file mode 100644 index 897b89c..0000000 --- a/Swiftob/scripts/agenda.lua +++ /dev/null @@ -1,94 +0,0 @@ -agendas = {} -currents = {} - -function full_agenda(from) - fullagenda = {} - fullagenda[1] = "Roll call" - fullagenda[2] = "Agenda bashing" - for i, v in ipairs(agendas[from]) do - table.insert(fullagenda, v) - end - table.insert(fullagenda, "Date of next meeting") - table.insert(fullagenda, "Any other business") - return fullagenda -end - -function agenda_full_command(command, params, message) - from = message['frombare'] - ensure_loaded(from) - agenda = agendas[from] - fullagenda = full_agenda(from) - reply = "" - for i, v in ipairs(fullagenda) do - reply = reply..i..") "..v.."\n" - end - reply = reply.."Fini" - swiftob_reply_to(message, reply) -end - -function agenda_append_command(command, params, message) - from = message['frombare'] - agenda_append(from, params) - agenda_save(from) - swiftob_reply_to(message, "Done.") -end - -function agenda_up_command(command, params, message) - from = message['frombare'] - ensure_loaded(from) - up = tonumber(params) - if up == nil then up = 1 end - currents[from] = currents[from] + up - if currents[from] <= 0 then currents[from] = 1 end - item = full_agenda(from)[currents[from]] - if item == nil then item = "Fini." end - reply = currents[from]..") "..item - swiftob_reply_to(message, reply) -end - - -function agenda_clear_command(command, params, message) - from = message['frombare'] - agendas[from] = {} - agenda_save(from) - swiftob_reply_to(message, "Done.") -end - -function agenda_save(from) - agenda = agendas[from] - swiftob_store_setting("count@@@"..from, #agenda) - for i, v in ipairs(agenda) do - swiftob_store_setting(i.."@@@"..from, v) - end -end - -function ensure_loaded(from) - if agendas[from] == nil then - agenda_load(from) - end -end - -function agenda_load(from) - agendas[from] = {} - currents[from] = 0 - num_items = tonumber(swiftob_get_setting("count@@@"..from)) - if num_items == nil then num_items = 0 end - for i = 1, num_items do - agenda_append(from, swiftob_get_setting(i.."@@@"..from)) - end -end - -function agenda_append(from, item) - ensure_loaded(from) - agenda = agendas[from] - table.insert(agenda, item) - agendas[from] = agenda -end - -swiftob_register_command("agenda", "Anyone", "print the full agenda", agenda_full_command) -swiftob_register_command("agendaappend", "Owner", "append an item to the agenda", agenda_append_command) -swiftob_register_command("agendaclear", "Owner", "clear the agenda", agenda_clear_command) -swiftob_register_command("agendaup", "Owner", "Moves the current counter by n, and returns the current agenda item", agenda_up_command) - - - diff --git a/Swiftob/scripts/badWords.lua b/Swiftob/scripts/badWords.lua deleted file mode 100644 index 2c16214..0000000 --- a/Swiftob/scripts/badWords.lua +++ /dev/null @@ -1,15 +0,0 @@ -function bad_words(body, muc, nick, message) - words = {"sbwriel"} - print("Received line from '" .. nick .. "' in '" .. muc .. "':") - print(body) - - for _, word in pairs(words) do - if string.len(string.match(body, word)) > 0 then - --swiftob_reply_to(message, "Kicking "..nick.." for bad word "..word) - swiftob_muc_kick(muc, nick) - end - end -end - -swiftob_register_listener(bad_words) - diff --git a/Swiftob/scripts/echo.lua b/Swiftob/scripts/echo.lua deleted file mode 100644 index a478a46..0000000 --- a/Swiftob/scripts/echo.lua +++ /dev/null @@ -1,5 +0,0 @@ -function echo_message(command, params, message) - swiftob_reply_to(message, params) -end - -swiftob_register_command("echo", "Anyone", "What did you say?", echo_message) diff --git a/Swiftob/scripts/eval.lua b/Swiftob/scripts/eval.lua deleted file mode 100644 index c9840dc..0000000 --- a/Swiftob/scripts/eval.lua +++ /dev/null @@ -1,14 +0,0 @@ - -function eval_command(command, params, message) - assert(loadstring(params))() - swiftob_reply_to(message, "done") -end - -function evalr_command(command, params, message) - result = assert(loadstring(params))() - swiftob_reply_to(message, "" .. result) -end - -swiftob_register_command("eval", "Owner", "Evaluate an expression", eval_command) -swiftob_register_command("evalr", "Owner", "Evaluate an expression and return the result", evalr_command) - diff --git a/Swiftob/scripts/irssiLogs.lua b/Swiftob/scripts/irssiLogs.lua deleted file mode 100644 index 3cecd9d..0000000 --- a/Swiftob/scripts/irssiLogs.lua +++ /dev/null @@ -1,23 +0,0 @@ -function irssi_log_to_file(muc, line) - filename = muc:gsub("@", "_at_") - filename = filename:gsub("%.%.", "_") - filename = filename:gsub("/", "_") - filename = filename:gsub("\\", "_") - file = io.open(filename, "a+") - if file == nil then - io.close() - else - file:write(line) - file:write("\n") - file:close() - end - -end - -function irssi_log_message(body, muc, nick, message) - time = os.date("%H:%M") - irssi_log_to_file(muc, time.." <"..nick.."> "..body) -end - -swiftob_register_listener(irssi_log_message) - diff --git a/Swiftob/scripts/logAllMessages.lua b/Swiftob/scripts/logAllMessages.lua deleted file mode 100644 index a14c0f3..0000000 --- a/Swiftob/scripts/logAllMessages.lua +++ /dev/null @@ -1,6 +0,0 @@ -function log_a_message(body, muc, nick, message) - print("Received line from '" .. nick .. "' in '" .. muc .. "':") - print(body) -end - -swiftob_register_listener(log_a_message) diff --git a/Swiftob/scripts/version.lua b/Swiftob/scripts/version.lua deleted file mode 100644 index 25e8870..0000000 --- a/Swiftob/scripts/version.lua +++ /dev/null @@ -1,32 +0,0 @@ -function friendly_version(version) - result = version['name'] - if version['version'] ~= nil and version['version'] ~= "" then - result = result.." version "..version['version'] - end - if version['os'] ~= nil and version['os'] ~= "" then - result = result .." on "..version['os'] - end - return result -end - -function version_command(command, params, message) - jid = swiftob_muc_input_to_jid(params, message['from']) - if jid == nil then - - else - swiftob_get_software_version({ - to=jid, - timeout=10, - success_callback=function(result) - swiftob_reply_to(message, params.." is running "..friendly_version(result)) - end, - failure_callback=function(error) - swiftob_reply_to(message, "Error getting version from "..params..": "..error) - end, - timeout_callback=function() - swiftob_reply_to(message, "Timeout waiting for version from "..params) - end}) - end -end - -swiftob_register_command("version", "Anyone", "Ask for someone's version", version_command) |