start adding commands to bridge

This commit is contained in:
Green Sky 2024-01-14 16:41:41 +01:00
parent f0b55a8479
commit ddb0066733
No known key found for this signature in database
3 changed files with 114 additions and 6 deletions

View File

@ -28,14 +28,16 @@ SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api)
return 1; return 1;
} }
Contact3Registry* cr; Contact3Registry* cr = nullptr;
RegistryMessageModel* rmm = nullptr; RegistryMessageModel* rmm = nullptr;
ConfigModelI* conf = nullptr; ConfigModelI* conf = nullptr;
MessageCommandDispatcher* mcd = nullptr;
{ // make sure required types are loaded { // make sure required types are loaded
cr = RESOLVE_INSTANCE(Contact3Registry); cr = RESOLVE_INSTANCE(Contact3Registry);
rmm = RESOLVE_INSTANCE(RegistryMessageModel); rmm = RESOLVE_INSTANCE(RegistryMessageModel);
conf = RESOLVE_INSTANCE(ConfigModelI); conf = RESOLVE_INSTANCE(ConfigModelI);
mcd = RESOLVE_INSTANCE(MessageCommandDispatcher);
if (cr == nullptr) { if (cr == nullptr) {
std::cerr << "PLUGIN Bridge missing Contact3Registry\n"; std::cerr << "PLUGIN Bridge missing Contact3Registry\n";
@ -51,11 +53,13 @@ SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api)
std::cerr << "PLUGIN Bridge missing ConfigModelI\n"; std::cerr << "PLUGIN Bridge missing ConfigModelI\n";
return 2; return 2;
} }
// missing mcd is no error
} }
// static store, could be anywhere tho // static store, could be anywhere tho
// construct with fetched dependencies // construct with fetched dependencies
g_bridge = std::make_unique<Bridge>(*cr, *rmm, *conf); g_bridge = std::make_unique<Bridge>(*cr, *rmm, *conf, mcd);
// register types // register types
PROVIDE_INSTANCE(Bridge, "Bridge", g_bridge.get()); PROVIDE_INSTANCE(Bridge, "Bridge", g_bridge.get());

View File

@ -4,14 +4,16 @@
#include <solanaceae/util/utils.hpp> #include <solanaceae/util/utils.hpp>
#include <solanaceae/contact/components.hpp> #include <solanaceae/contact/components.hpp>
#include <solanaceae/message3/components.hpp> #include <solanaceae/message3/components.hpp>
#include <solanaceae/message3/message_command_dispatcher.hpp>
#include <iostream> #include <iostream>
Bridge::Bridge( Bridge::Bridge(
Contact3Registry& cr, Contact3Registry& cr,
RegistryMessageModel& rmm, RegistryMessageModel& rmm,
ConfigModelI& conf ConfigModelI& conf,
) : _cr(cr), _rmm(rmm), _conf(conf) { MessageCommandDispatcher* mcd
) : _cr(cr), _rmm(rmm), _conf(conf), _mcd(mcd) {
_rmm.subscribe(this, enumType::message_construct); _rmm.subscribe(this, enumType::message_construct);
if (!_conf.has_bool("Bridge", "username_angle_brackets")) { if (!_conf.has_bool("Bridge", "username_angle_brackets")) {
@ -40,6 +42,8 @@ Bridge::Bridge(
} }
updateVGroups(); updateVGroups();
registerCommands();
} }
Bridge::~Bridge(void) { Bridge::~Bridge(void) {
@ -79,6 +83,101 @@ void Bridge::updateVGroups(void) {
} }
} }
void Bridge::registerCommands(void) {
if (_mcd == nullptr) {
return;
}
_mcd->registerCommand(
"Bridge", "bridge",
"users",
[this](std::string_view params, Message3Handle m) -> bool {
const auto contact_from = m.get<Message::Components::ContactFrom>().c;
const auto contact_to = m.get<Message::Components::ContactTo>().c;
// TODO: if no vgroup name supplied
Contact3Handle group_contact;
if (/*is public ?*/ _c_to_vg.count({_cr, contact_to})) {
// message was sent public in group
group_contact = {_cr, contact_to};
} else if (_cr.all_of<Contact::Components::Parent>(contact_from)) {
// use parent of sender
group_contact = {_cr, _cr.get<Contact::Components::Parent>(contact_from).parent};
} else if (false /* parent of contact_to ? */) {
}
if (!_c_to_vg.count(group_contact)) {
// nope
_rmm.sendText(
contact_from,
"It appears you are not bridged or forgot to supply the vgroup name!"
);
return true;
}
assert(static_cast<bool>(group_contact));
const auto& vg = _vgroups.at(_c_to_vg.at(group_contact));
_rmm.sendText(
contact_from,
"Contacts online in other bridged group(s) in vgroup '" + vg.vg_name + "'"
);
for (const auto& vgc : vg.contacts) {
if (vgc.c == group_contact) {
// skip self
continue;
}
if (vgc.c.all_of<Contact::Components::ParentOf>()) {
_rmm.sendText(
contact_from,
" online in '" +
(vgc.c.all_of<Contact::Components::Name>() ? vgc.c.get<Contact::Components::Name>().name : "<unk>") +
"' id:" + bin2hex(vgc.id)
);
// for each sub contact
for (const auto& sub_c : vgc.c.get<Contact::Components::ParentOf>().subs) {
if (
const auto* sub_cs = _cr.try_get<Contact::Components::ConnectionState>(sub_c);
sub_cs != nullptr && sub_cs->state == Contact::Components::ConnectionState::disconnected
) {
// skip offline
continue;
}
_rmm.sendText(
contact_from,
" '" +
(_cr.all_of<Contact::Components::Name>(sub_c) ? _cr.get<Contact::Components::Name>(sub_c).name : "<unk>") +
"'" +
(_cr.all_of<Contact::Components::ID>(sub_c) ? " id:" + bin2hex(vgc.id) : "")
);
}
} else { // contact without parent
_rmm.sendText(
contact_from,
" online contact '" +
(vgc.c.all_of<Contact::Components::Name>() ? vgc.c.get<Contact::Components::Name>().name : "<unk>") +
"' id:" + bin2hex(vgc.id)
);
}
}
return true;
},
"List users connected in the other bridged group(s).",
MessageCommandDispatcher::Perms::EVERYONE // TODO: should proably be MODERATOR
);
std::cout << "Bridge: registered commands\n";
}
const Bridge::VirtualGroups* Bridge::findVGforContact(const Contact3Handle& c) {
return nullptr;
}
bool Bridge::onEvent(const Message::Events::MessageConstruct& e) { bool Bridge::onEvent(const Message::Events::MessageConstruct& e) {
if (!e.e.valid()) { if (!e.e.valid()) {
return false; // how return false; // how

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include <solanaceae/message3/registry_message_model.hpp>
#include <solanaceae/contact/contact_model3.hpp> #include <solanaceae/contact/contact_model3.hpp>
#include <solanaceae/message3/registry_message_model.hpp>
#include <vector> #include <vector>
#include <map> #include <map>
@ -10,11 +10,13 @@
// fwd // fwd
struct ConfigModelI; struct ConfigModelI;
class MessageCommandDispatcher;
class Bridge : public RegistryMessageModelEventI { class Bridge : public RegistryMessageModelEventI {
Contact3Registry& _cr; Contact3Registry& _cr;
RegistryMessageModel& _rmm; RegistryMessageModel& _rmm;
ConfigModelI& _conf; ConfigModelI& _conf;
MessageCommandDispatcher* _mcd;
struct VirtualGroups { struct VirtualGroups {
struct VContact { struct VContact {
@ -35,7 +37,8 @@ class Bridge : public RegistryMessageModelEventI {
Bridge( Bridge(
Contact3Registry& cr, Contact3Registry& cr,
RegistryMessageModel& rmm, RegistryMessageModel& rmm,
ConfigModelI& conf ConfigModelI& conf,
MessageCommandDispatcher* mcd = nullptr
); );
~Bridge(void); ~Bridge(void);
@ -43,6 +46,8 @@ class Bridge : public RegistryMessageModelEventI {
private: private:
void updateVGroups(void); void updateVGroups(void);
void registerCommands(void);
const VirtualGroups* findVGforContact(const Contact3Handle& c);
protected: // mm protected: // mm
bool onEvent(const Message::Events::MessageConstruct& e) override; bool onEvent(const Message::Events::MessageConstruct& e) override;