mirror of https://github.com/JAJames/jessilib.git
22 changed files with 409 additions and 42 deletions
@ -0,0 +1,60 @@ |
|||
/**
|
|||
* Copyright (C) 2020 Jessica James. |
|||
* |
|||
* Permission to use, copy, modify, and/or distribute this software for any |
|||
* purpose with or without fee is hereby granted, provided that the above |
|||
* copyright notice and this permission notice appear in all copies. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
|||
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
|||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
|||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|||
* |
|||
* Written by Jessica James <jessica.aj@outlook.com> |
|||
*/ |
|||
|
|||
#include "io/command.hpp" |
|||
#include "io/command_manager.hpp" |
|||
#include "shutdown.hpp" |
|||
|
|||
namespace jessibot { |
|||
namespace io { |
|||
|
|||
using namespace jessilib::io; |
|||
|
|||
command quit_command{ [](command_context& context) { |
|||
using namespace jessilib::io; |
|||
text quit_text{ "Closing jessibot", text::property::bold, color{ 0xFF0000 } }; // TODO: localize
|
|||
context.publicReply(formatted_message{ "{}", quit_text }); |
|||
notify_shutdown(); |
|||
}, "quit" }; |
|||
|
|||
// ISSUE: help command has no way to know what commands exist for the given context
|
|||
command help_command{ [](command_context& context) { |
|||
auto details = context.details(); |
|||
auto table_name = details["table"].get<std::string>(); |
|||
|
|||
if (table_name.empty()) { |
|||
text error_text{ "ERROR", text::property::bold, color{ 0xFF0000 } }; // TODO: localize
|
|||
context.publicReply(formatted_message{ "{} command context is missing permission table name", error_text }); |
|||
return; |
|||
} |
|||
|
|||
// table examples: "console", "irc", "irc+", "irc%", "irc@"; should table instead be 'tables'?
|
|||
|
|||
text table_text{ table_name, text::property::bold, color{ 0x0000FF } }; |
|||
context.publicReply(formatted_message{ "Commands for table '{}':", table_text }); |
|||
|
|||
// TODO: read some permission table and filter commands based upon permission and context information
|
|||
const command_manager& manager = command_manager::instance(); |
|||
manager.foreach([&context](basic_command* in_command) { |
|||
context.publicReply(formatted_message{ "{}", in_command->label() }); |
|||
return true; |
|||
}); |
|||
}, "help" }; |
|||
|
|||
} // namespace io
|
|||
} // namespace jessibot
|
@ -0,0 +1,48 @@ |
|||
/**
|
|||
* Copyright (C) 2020 Jessica James. |
|||
* |
|||
* Permission to use, copy, modify, and/or distribute this software for any |
|||
* purpose with or without fee is hereby granted, provided that the above |
|||
* copyright notice and this permission notice appear in all copies. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
|||
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
|||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
|||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|||
* |
|||
* Written by Jessica James <jessica.aj@outlook.com> |
|||
*/ |
|||
|
|||
#include "console.hpp" |
|||
|
|||
#include <iostream> |
|||
#include "io/command_manager.hpp" |
|||
#include "io/ansi/ansi_text.hpp" |
|||
#include "shutdown.hpp" |
|||
#include "console/console_command_context.hpp" |
|||
|
|||
namespace jessibot { |
|||
namespace io { |
|||
|
|||
void console_input_loop() { |
|||
using namespace jessilib::io; |
|||
|
|||
std::string input; |
|||
auto shutdown_future = get_shutdown_future(); |
|||
while (shutdown_future.wait_for(std::chrono::milliseconds(10)) != std::future_status::ready) { |
|||
std::getline(std::cin, input); // TODO: use a non-bloicking call and poll running periodically
|
|||
jessibot::io::console_command_context context{ input }; |
|||
if (!command_manager::instance().execute_command(context)) { |
|||
text error_text{ "ERROR", text::property::bold, color{ 0xFF0000 }}; |
|||
text keyword_text{ context.keyword(), text::property::bold, color{ 0x0000FF }}; |
|||
auto result = process_message<ansi::text_wrapper>(formatted_message{"{} Command \"{}\" not found", error_text, keyword_text}); |
|||
std::cout << result << std::endl; |
|||
} |
|||
} |
|||
} |
|||
|
|||
} // namespace io
|
|||
} // namespace jessibot
|
@ -0,0 +1,30 @@ |
|||
/**
|
|||
* Copyright (C) 2020 Jessica James. |
|||
* |
|||
* Permission to use, copy, modify, and/or distribute this software for any |
|||
* purpose with or without fee is hereby granted, provided that the above |
|||
* copyright notice and this permission notice appear in all copies. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
|||
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
|||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
|||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|||
* |
|||
* Written by Jessica James <jessica.aj@outlook.com> |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include <atomic> |
|||
|
|||
namespace jessibot { |
|||
namespace io { |
|||
|
|||
void console_input_loop(); |
|||
|
|||
} // namespace io
|
|||
} // namespace jessibot
|
|||
|
@ -0,0 +1,53 @@ |
|||
/**
|
|||
* Copyright (C) 2020 Jessica James. |
|||
* |
|||
* Permission to use, copy, modify, and/or distribute this software for any |
|||
* purpose with or without fee is hereby granted, provided that the above |
|||
* copyright notice and this permission notice appear in all copies. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
|||
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
|||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
|||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|||
* |
|||
* Written by Jessica James <jessica.aj@outlook.com> |
|||
*/ |
|||
|
|||
#include "console_command_context.hpp" |
|||
|
|||
#include <iostream> |
|||
#include "io/ansi/ansi_text.hpp" |
|||
|
|||
namespace jessibot { |
|||
namespace io { |
|||
|
|||
/** Reply */ |
|||
bool console_command_context::privateReply(const jessilib::io::formatted_message& in_message) { |
|||
auto result = jessilib::io::process_message<jessilib::io::ansi::text_wrapper>(in_message); |
|||
std::cout << result << std::endl; |
|||
return true; |
|||
} |
|||
|
|||
bool console_command_context::publicReply(const jessilib::io::formatted_message& in_message) { |
|||
// Consoles only have 1 output mechanism
|
|||
return privateReply(in_message); |
|||
} |
|||
|
|||
/** Additional contextual details */ |
|||
jessilib::object console_command_context::details() const { |
|||
static jessilib::object s_details { |
|||
jessilib::object::map_t{ { "table", "console" } } |
|||
}; |
|||
|
|||
return s_details; |
|||
} |
|||
|
|||
std::string console_command_context::getText(std::string_view tag) const { |
|||
return { tag.begin(), tag.end() }; // TODO: implement properly
|
|||
} |
|||
|
|||
} // namespace io
|
|||
} // namespace jessilib
|
@ -0,0 +1,41 @@ |
|||
/**
|
|||
* Copyright (C) 2020 Jessica James. |
|||
* |
|||
* Permission to use, copy, modify, and/or distribute this software for any |
|||
* purpose with or without fee is hereby granted, provided that the above |
|||
* copyright notice and this permission notice appear in all copies. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
|||
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
|||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
|||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|||
* |
|||
* Written by Jessica James <jessica.aj@outlook.com> |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include "io/command_context.hpp" |
|||
|
|||
namespace jessibot { |
|||
namespace io { |
|||
|
|||
class console_command_context : public jessilib::io::command_context { |
|||
public: |
|||
using jessilib::io::command_context::command_context; |
|||
|
|||
/** Reply */ |
|||
bool privateReply(const jessilib::io::formatted_message& in_message) override; |
|||
bool publicReply(const jessilib::io::formatted_message& in_message) override; |
|||
|
|||
/** Additional contextual details */ |
|||
jessilib::object details() const override; |
|||
std::string getText(std::string_view tag) const override; |
|||
}; // class console_command_context
|
|||
|
|||
} // namespace io
|
|||
} // namespace jessibot
|
|||
|
@ -0,0 +1,38 @@ |
|||
/**
|
|||
* Copyright (C) 2020 Jessica James. |
|||
* |
|||
* Permission to use, copy, modify, and/or distribute this software for any |
|||
* purpose with or without fee is hereby granted, provided that the above |
|||
* copyright notice and this permission notice appear in all copies. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
|||
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
|||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
|||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|||
* |
|||
* Written by Jessica James <jessica.aj@outlook.com> |
|||
*/ |
|||
|
|||
#include "shutdown.hpp" |
|||
|
|||
namespace jessibot { |
|||
|
|||
std::promise<void>& shutdown_promise() { |
|||
static std::promise<void> s_shutdown_promise; |
|||
return s_shutdown_promise; |
|||
} |
|||
|
|||
std::shared_future<void> get_shutdown_future() { |
|||
static std::shared_future<void> s_shutdown_future{ shutdown_promise().get_future().share() }; |
|||
return s_shutdown_future; |
|||
} |
|||
|
|||
void notify_shutdown() { |
|||
shutdown_promise().set_value(); |
|||
} |
|||
|
|||
} // namespace jessibot
|
|||
|
@ -0,0 +1,29 @@ |
|||
/**
|
|||
* Copyright (C) 2020 Jessica James. |
|||
* |
|||
* Permission to use, copy, modify, and/or distribute this software for any |
|||
* purpose with or without fee is hereby granted, provided that the above |
|||
* copyright notice and this permission notice appear in all copies. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
|||
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
|||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
|||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|||
* |
|||
* Written by Jessica James <jessica.aj@outlook.com> |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include <future> |
|||
|
|||
namespace jessibot { |
|||
|
|||
std::shared_future<void> get_shutdown_future(); |
|||
void notify_shutdown(); |
|||
|
|||
} // namespace jessibot
|
|||
|
@ -0,0 +1,52 @@ |
|||
/**
|
|||
* Copyright (C) 2020 Jessica James. |
|||
* |
|||
* Permission to use, copy, modify, and/or distribute this software for any |
|||
* purpose with or without fee is hereby granted, provided that the above |
|||
* copyright notice and this permission notice appear in all copies. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
|||
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
|||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
|||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|||
* |
|||
* Written by Jessica James <jessica.aj@outlook.com> |
|||
*/ |
|||
|
|||
#include "io/command_manager.hpp" |
|||
|
|||
namespace jessilib { |
|||
namespace io { |
|||
|
|||
void command_manager::register_command(basic_command& in_command) { |
|||
std::lock_guard<std::shared_mutex> guard{ m_commands_mutex }; |
|||
m_commands.push_back(&in_command); |
|||
} |
|||
|
|||
void command_manager::unregister_command(basic_command& in_command) { |
|||
std::lock_guard<std::shared_mutex> guard{ m_commands_mutex }; |
|||
std::remove(m_commands.begin(), m_commands.end(), &in_command); |
|||
} |
|||
|
|||
bool command_manager::execute_command(command_context& in_context) { |
|||
std::shared_lock<std::shared_mutex> lock{ m_commands_mutex }; |
|||
for (auto& command : m_commands) { |
|||
if (in_context.keyword() == command->label()) { |
|||
command->execute(in_context); |
|||
return true; |
|||
} |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
command_manager& command_manager::instance() { |
|||
static command_manager s_command_manager; |
|||
return s_command_manager; |
|||
} |
|||
|
|||
} // namespace io
|
|||
} // namespace jessilib
|
Loading…
Reference in new issue