Publishing now has a space for an article and a title. Also reduced amount of copying
This commit is contained in:
parent
98bfab4ba5
commit
8187d045cd
8 changed files with 76 additions and 35 deletions
|
@ -9,26 +9,27 @@ Module::Actor::Actor(const std::shared_ptr<Core>& core, const Config::Module& co
|
|||
|
||||
Module::Actor::~Actor() noexcept {}
|
||||
|
||||
Shared::Result Module::Actor::message(const std::shared_ptr<::Actor>& actor, const Shared::Strings& args) {
|
||||
std::string result;
|
||||
Shared::Result Module::Actor::message(const std::shared_ptr<::Actor>& actor, std::string_view view) {
|
||||
std::string_view command = pop(view);
|
||||
|
||||
if (args.front() == "list") {
|
||||
if (command == "list") {
|
||||
if (!hasPermission("read", actor))
|
||||
return Shared::forbidden;
|
||||
|
||||
result = list();
|
||||
} else if (args.front() == "set") {
|
||||
core->send(actor->jid, list());
|
||||
return Shared::success;
|
||||
}
|
||||
|
||||
if (command == "set") {
|
||||
if (!hasPermission("write", actor))
|
||||
return Shared::forbidden;
|
||||
|
||||
if (args.size() < 3)
|
||||
std::string_view jid = pop(view);
|
||||
std::string_view group = pop(view);
|
||||
if (jid.empty() || group.empty())
|
||||
return Shared::error;
|
||||
|
||||
result = set(args[1], args[2]);
|
||||
}
|
||||
|
||||
if (!result.empty()) {
|
||||
core->send(actor->jid, result);
|
||||
core->send(actor->jid, set(jid, group));
|
||||
return Shared::success;
|
||||
}
|
||||
|
||||
|
@ -50,8 +51,11 @@ std::string Module::Actor::list() {
|
|||
return result;
|
||||
}
|
||||
|
||||
std::string Module::Actor::set(const std::string& jid, const std::string& group) {
|
||||
core->setGroup(lower(jid), group);
|
||||
std::string Module::Actor::set(const std::string_view& jid, const std::string_view& group) {
|
||||
std::string j(lower(std::string(jid)));
|
||||
std::string g(group);
|
||||
|
||||
core->setGroup(j, g);
|
||||
|
||||
return jid + " is now " + core->router.getGroup(jid);
|
||||
return j + " is now " + core->router.getGroup(j);
|
||||
}
|
||||
|
|
|
@ -14,11 +14,11 @@ public:
|
|||
Actor(const std::shared_ptr<Core>& core, const Config::Module& conf);
|
||||
~Actor() noexcept;
|
||||
|
||||
virtual Shared::Result message(const std::shared_ptr<::Actor>& actor, const Shared::Strings& args) override;
|
||||
virtual Shared::Result message(const std::shared_ptr<::Actor>& actor, std::string_view view) override;
|
||||
|
||||
private:
|
||||
std::string list();
|
||||
std::string set(const std::string& jid, const std::string& group);
|
||||
std::string set(const std::string_view& jid, const std::string_view& group);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -28,3 +28,22 @@ Module::Module::~Module() noexcept {}
|
|||
std::string Module::Module::lower(const std::string& text) {
|
||||
return std::ranges::to<std::string>(text | std::views::transform(::tolower));
|
||||
}
|
||||
|
||||
std::string_view Module::Module::pop(std::string_view& input, char delimiter) {
|
||||
std::size_t pos = input.find(delimiter);
|
||||
std::string_view token;
|
||||
|
||||
if (pos == std::string_view::npos) {
|
||||
token = input;
|
||||
input = {};
|
||||
} else {
|
||||
token = input.substr(0, pos);
|
||||
pos = input.find_first_not_of(' ', pos + 1);
|
||||
if (pos == std::string_view::npos)
|
||||
input = {};
|
||||
else
|
||||
input.remove_prefix(pos);
|
||||
}
|
||||
|
||||
return token;
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include <vector>
|
||||
#include <ranges>
|
||||
#include <cctype>
|
||||
#include <string_view>
|
||||
|
||||
#include "shared/definitions.h"
|
||||
#include "shared/result.h"
|
||||
|
@ -28,8 +29,9 @@ public:
|
|||
virtual ~Module() noexcept;
|
||||
|
||||
static std::string lower(const std::string& text);
|
||||
static std::string_view pop(std::string_view& input, char delimiter = ' ');
|
||||
|
||||
virtual Shared::Result message(const std::shared_ptr<::Actor>& actor, const Shared::Strings& args) = 0;
|
||||
virtual Shared::Result message(const std::shared_ptr<::Actor>& actor, std::string_view view) = 0;
|
||||
|
||||
public:
|
||||
const std::string name;
|
||||
|
|
|
@ -11,29 +11,33 @@ Module::Publish::Publish(const std::shared_ptr<Core>& core, const Config::Module
|
|||
|
||||
Module::Publish::~Publish() noexcept {}
|
||||
|
||||
Shared::Result Module::Publish::message(const std::shared_ptr<::Actor>& actor, const Shared::Strings& args) {
|
||||
if (args.front() == "to") {
|
||||
Shared::Result Module::Publish::message(const std::shared_ptr<::Actor>& actor, std::string_view view) {
|
||||
std::string_view command = pop(view);
|
||||
|
||||
if (command == "to") {
|
||||
if (!hasPermission("publish", actor))
|
||||
return Shared::forbidden;
|
||||
|
||||
if (args.size() < 3)
|
||||
|
||||
std::string_view head = pop(view, '\n');
|
||||
std::string_view address = pop(head);
|
||||
if (address.empty())
|
||||
return Shared::error;
|
||||
|
||||
return to(args[1], args[2]);
|
||||
return to(address, head, view);
|
||||
}
|
||||
|
||||
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) {
|
||||
Shared::Result Module::Publish::to(std::string_view address, std::string_view title, std::string_view body) {
|
||||
std::string_view node = pop(address, '@');
|
||||
if (node.empty() || address.empty()) {
|
||||
warn("Malformed address in \"to\" method");
|
||||
return Shared::error;
|
||||
}
|
||||
|
||||
try {
|
||||
core->publish(parts[1], parts[0], "Completely testing stuff, early stages, ignore please", body);
|
||||
core->publish(std::string(address), std::string(node), std::string(title), std::string(body));
|
||||
return Shared::success;
|
||||
} catch (const std::exception& e) {
|
||||
error("Exception in \"to\" method: " + std::string(e.what()));
|
||||
|
|
|
@ -16,10 +16,10 @@ public:
|
|||
Publish(const std::shared_ptr<Core>& core, const Config::Module& conf);
|
||||
~Publish() noexcept;
|
||||
|
||||
virtual Shared::Result message(const std::shared_ptr<::Actor>& actor, const Shared::Strings& args) override;
|
||||
virtual Shared::Result message(const std::shared_ptr<::Actor>& actor, std::string_view view) override;
|
||||
|
||||
private:
|
||||
Shared::Result to(const std::string& address, const std::string& body);
|
||||
Shared::Result to(std::string_view address, std::string_view title, std::string_view body);
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue