jay/module/actor.cpp

58 lines
1.5 KiB
C++

// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#include "actor.h"
Module::Actor::Actor(const std::shared_ptr<Core>& core, const Shared::Permissions& permissions):
Module(core, permissions)
{}
Module::Actor::~Actor() noexcept {}
Shared::Result Module::Actor::message(const std::shared_ptr<::Actor>& actor, const Shared::Strings& args) {
std::string result;
if (args.front() == "list") {
if (!hasPermission("read", actor))
return Shared::forbidden;
result = list();
} else if (args.front() == "set") {
if (!hasPermission("write", actor))
return Shared::forbidden;
if (args.size() < 3)
return Shared::error;
result = set(args[1], args[2]);
}
if (!result.empty()) {
core->send(actor->jid, result);
return Shared::success;
}
return Shared::unhandled;
}
std::string Module::Actor::list() {
std::string result;
for (const std::pair<const std::string, std::string>& pair : core->router.getActors()) {
if (!result.empty())
result.append(1, '\n');
result += pair.first + ": " + pair.second;
}
if (result.empty())
result += "There are no actors currently";
return result;
}
std::string Module::Actor::set(const std::string& jid, const std::string& group) {
core->setGroup(jid, group);
return jid + " is now " + core->router.getGroup(jid);
}