First way to publish
This commit is contained in:
parent
0be7fe9bff
commit
647b8f3072
17 changed files with 240 additions and 21 deletions
|
@ -1,11 +1,13 @@
|
|||
set(SOURCES
|
||||
module.cpp
|
||||
actor.cpp
|
||||
publish.cpp
|
||||
)
|
||||
|
||||
set(HEADERS
|
||||
module.h
|
||||
actor.h
|
||||
publish.h
|
||||
)
|
||||
|
||||
target_sources(${EXEC_NAME} PRIVATE ${SOURCES})
|
||||
|
|
|
@ -23,21 +23,6 @@ bool Module::Module::hasPermission(const std::string& permission, const std::sha
|
|||
|
||||
Module::Module::~Module() noexcept {}
|
||||
|
||||
std::vector<std::string> Module::Module::split(const std::string& string, const std::string& delimiter) {
|
||||
std::vector<std::string> result;
|
||||
|
||||
std::size_t last = 0;
|
||||
std::size_t next = string.find(delimiter, last);
|
||||
while (next != std::string::npos) {
|
||||
result.emplace_back(string.substr(last, next - last));
|
||||
last = next + 1;
|
||||
next = string.find(delimiter, last);
|
||||
}
|
||||
result.emplace_back(string.substr(last));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string Module::Module::lower(const std::string& text) {
|
||||
return std::ranges::to<std::string>(text | std::views::transform(::tolower));
|
||||
}
|
||||
|
|
|
@ -26,7 +26,6 @@ protected:
|
|||
public:
|
||||
virtual ~Module() noexcept;
|
||||
|
||||
static Shared::Strings split(const std::string& string, const std::string& delimiter = " ");
|
||||
static std::string lower(const std::string& text);
|
||||
|
||||
virtual Shared::Result message(const std::shared_ptr<::Actor>& actor, const Shared::Strings& args) = 0;
|
||||
|
|
45
module/publish.cpp
Normal file
45
module/publish.cpp
Normal file
|
@ -0,0 +1,45 @@
|
|||
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "publish.h"
|
||||
|
||||
#include <exception>
|
||||
|
||||
Module::Publish::Publish(const std::shared_ptr<Core>& core, const Shared::Permissions& permissions):
|
||||
Module(core, permissions, "Actor")
|
||||
{}
|
||||
|
||||
Module::Publish::~Publish() noexcept {}
|
||||
|
||||
Shared::Result Module::Publish::message(const std::shared_ptr<::Actor>& actor, const Shared::Strings& args) {
|
||||
if (args.front() == "to") {
|
||||
if (!hasPermission("publish", actor))
|
||||
return Shared::forbidden;
|
||||
|
||||
if (args.size() < 3)
|
||||
return Shared::error;
|
||||
|
||||
return to(args[1], args[2]);
|
||||
}
|
||||
|
||||
return Shared::unhandled;
|
||||
}
|
||||
|
||||
Shared::Result Module::Publish::to(const std::string& address, const std::string& body) {
|
||||
Shared::Strings parts = Shared::split(address, "@");
|
||||
if (parts.size() != 2) {
|
||||
warn("Malformed address in \"to\" method");
|
||||
return Shared::error;
|
||||
}
|
||||
|
||||
try {
|
||||
core->publish(parts[1], parts[0], "Completely testing stuff, early stages, ignore please", body);
|
||||
return Shared::success;
|
||||
} catch (const std::exception& e) {
|
||||
error("Exception in \"to\" method: " + std::string(e.what()));
|
||||
} catch (...) {
|
||||
error("Unhandled exception in \"to\" method");
|
||||
}
|
||||
|
||||
return Shared::error;
|
||||
}
|
25
module/publish.h
Normal file
25
module/publish.h
Normal file
|
@ -0,0 +1,25 @@
|
|||
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "shared/definitions.h"
|
||||
#include "shared/result.h"
|
||||
#include "shared/utils.h"
|
||||
|
||||
#include "module.h"
|
||||
|
||||
namespace Module {
|
||||
|
||||
class Publish : public Module {
|
||||
public:
|
||||
Publish(const std::shared_ptr<Core>& core, const Shared::Permissions& permissions);
|
||||
~Publish() noexcept;
|
||||
|
||||
virtual Shared::Result message(const std::shared_ptr<::Actor>& actor, const Shared::Strings& args) override;
|
||||
|
||||
private:
|
||||
Shared::Result to(const std::string& address, const std::string& body);
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue