Compare commits

...

13 Commits

Author SHA1 Message Date
dd462b450e make contact store version visible 2025-03-10 20:54:34 +01:00
344af9dc4b port to contact4 2025-03-10 14:54:55 +01:00
b63bc1dd0f Create LICENSE 2025-02-07 12:26:58 +01:00
c5bf212ab9 update cmake version 2025-01-13 02:05:34 +01:00
41d86bfd8f time moved to utils 2025-01-07 16:07:22 +01:00
82a0fc0d36 use sr 2024-10-24 12:07:06 +02:00
34908967aa update to rmmi 2024-10-06 11:32:19 +02:00
b21acd4c99 correct plugin target type 2024-05-28 10:09:23 +02:00
ea63233452 remove entt from external, since contact now resolves it itself 2024-02-04 13:24:28 +01:00
09c09e1bb2 make version for mcd work 2024-01-31 21:32:09 +01:00
d9ee8f9dc4 update for new plugin api 2024-01-18 18:22:33 +01:00
158860e1b0 allow specifying vgroup 2024-01-14 17:24:40 +01:00
759cd414ab fix wrong id for subs 2024-01-14 17:13:31 +01:00
7 changed files with 129 additions and 99 deletions

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2025 Erik Scholz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -2,16 +2,6 @@ cmake_minimum_required(VERSION 3.14...3.24 FATAL_ERROR)
include(FetchContent) include(FetchContent)
# TODO: move entt dep into solanaceae_contact
if (NOT TARGET EnTT::EnTT)
FetchContent_Declare(EnTT
GIT_REPOSITORY https://github.com/skypjack/entt.git
GIT_TAG v3.12.2
EXCLUDE_FROM_ALL
)
FetchContent_MakeAvailable(EnTT)
endif()
if (NOT TARGET solanaceae_util) if (NOT TARGET solanaceae_util)
FetchContent_Declare(solanaceae_util FetchContent_Declare(solanaceae_util
GIT_REPOSITORY https://github.com/Green-Sky/solanaceae_util.git GIT_REPOSITORY https://github.com/Green-Sky/solanaceae_util.git

View File

@ -1,9 +1,14 @@
cmake_minimum_required(VERSION 3.14...3.24 FATAL_ERROR) cmake_minimum_required(VERSION 3.14...3.24 FATAL_ERROR)
add_library(plugin_bridge SHARED add_library(plugin_bridge MODULE
./plugin_bridge.cpp ./plugin_bridge.cpp
) )
set_target_properties(plugin_bridge PROPERTIES
C_VISIBILITY_PRESET hidden
)
target_compile_definitions(plugin_bridge PUBLIC ENTT_API_IMPORT)
target_link_libraries(plugin_bridge PUBLIC target_link_libraries(plugin_bridge PUBLIC
solanaceae_plugin solanaceae_plugin
solanaceae_bridge solanaceae_bridge

View File

@ -2,19 +2,22 @@
#include "../src/bridge.hpp" #include "../src/bridge.hpp"
#include <solanaceae/util/config_model.hpp>
#include <solanaceae/contact/contact_store_i.hpp>
#include <solanaceae/message3/message_command_dispatcher.hpp>
#include <memory> #include <memory>
#include <limits> #include <limits>
#include <iostream> #include <iostream>
#define RESOLVE_INSTANCE(x) static_cast<x*>(solana_api->resolveInstance(#x))
#define PROVIDE_INSTANCE(x, p, v) solana_api->provideInstance(#x, p, static_cast<x*>(v))
static std::unique_ptr<Bridge> g_bridge = nullptr; static std::unique_ptr<Bridge> g_bridge = nullptr;
constexpr const char* plugin_name = "Bridge";
extern "C" { extern "C" {
SOLANA_PLUGIN_EXPORT const char* solana_plugin_get_name(void) { SOLANA_PLUGIN_EXPORT const char* solana_plugin_get_name(void) {
return "Bridge"; return plugin_name;
} }
SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_get_version(void) { SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_get_version(void) {
@ -22,59 +25,42 @@ SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_get_version(void) {
} }
SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api) { SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api) {
std::cout << "PLUGIN Bridge START()\n"; std::cout << "PLUGIN " << plugin_name << " START()\n";
if (solana_api == nullptr) { if (solana_api == nullptr) {
return 1; return 1;
} }
Contact3Registry* cr = nullptr; try {
RegistryMessageModel* rmm = nullptr; auto* cs = PLUG_RESOLVE_INSTANCE(ContactStore4I);
ConfigModelI* conf = nullptr; auto* rmm = PLUG_RESOLVE_INSTANCE(RegistryMessageModelI);
MessageCommandDispatcher* mcd = nullptr; auto* conf = PLUG_RESOLVE_INSTANCE(ConfigModelI);
{ // make sure required types are loaded // optional dep
cr = RESOLVE_INSTANCE(Contact3Registry); auto* mcd = PLUG_RESOLVE_INSTANCE_OPT(MessageCommandDispatcher);
rmm = RESOLVE_INSTANCE(RegistryMessageModel);
conf = RESOLVE_INSTANCE(ConfigModelI);
mcd = RESOLVE_INSTANCE(MessageCommandDispatcher);
if (cr == nullptr) {
std::cerr << "PLUGIN Bridge missing Contact3Registry\n";
return 2;
}
if (rmm == nullptr) {
std::cerr << "PLUGIN Bridge missing RegistryMessageModel\n";
return 2;
}
if (conf == nullptr) {
std::cerr << "PLUGIN Bridge missing ConfigModelI\n";
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, mcd); g_bridge = std::make_unique<Bridge>(*cs, *rmm, *conf, mcd);
// register types // register types
PROVIDE_INSTANCE(Bridge, "Bridge", g_bridge.get()); PLUG_PROVIDE_INSTANCE(Bridge, plugin_name, g_bridge.get());
} catch (const ResolveException& e) {
std::cerr << "PLUGIN " << plugin_name << " " << e.what << "\n";
return 2;
}
return 0; return 0;
} }
SOLANA_PLUGIN_EXPORT void solana_plugin_stop(void) { SOLANA_PLUGIN_EXPORT void solana_plugin_stop(void) {
std::cout << "PLUGIN Bridge STOP()\n"; std::cout << "PLUGIN " << plugin_name << " STOP()\n";
g_bridge.reset(); g_bridge.reset();
} }
SOLANA_PLUGIN_EXPORT float solana_plugin_tick(float delta) { SOLANA_PLUGIN_EXPORT float solana_plugin_tick(float time_delta) {
g_bridge->iterate(delta); g_bridge->iterate(time_delta);
return std::numeric_limits<float>::max(); return std::numeric_limits<float>::max();
} }

View File

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.9 FATAL_ERROR) cmake_minimum_required(VERSION 3.14...3.24 FATAL_ERROR)
add_library(solanaceae_bridge STATIC add_library(solanaceae_bridge STATIC
./bridge.hpp ./bridge.hpp

View File

@ -2,6 +2,8 @@
#include <solanaceae/util/config_model.hpp> #include <solanaceae/util/config_model.hpp>
#include <solanaceae/util/utils.hpp> #include <solanaceae/util/utils.hpp>
#include <solanaceae/util/time.hpp>
#include <solanaceae/contact/contact_store_i.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 <solanaceae/message3/message_command_dispatcher.hpp>
@ -9,12 +11,12 @@
#include <iostream> #include <iostream>
Bridge::Bridge( Bridge::Bridge(
Contact3Registry& cr, ContactStore4I& cs,
RegistryMessageModel& rmm, RegistryMessageModelI& rmm,
ConfigModelI& conf, ConfigModelI& conf,
MessageCommandDispatcher* mcd MessageCommandDispatcher* mcd
) : _cr(cr), _rmm(rmm), _conf(conf), _mcd(mcd) { ) : _cs(cs), _rmm(rmm), _rmm_sr(_rmm.newSubRef(this)), _conf(conf), _mcd(mcd) {
_rmm.subscribe(this, enumType::message_construct); _rmm_sr.subscribe(RegistryMessageModel_Event::message_construct);
if (!_conf.has_bool("Bridge", "username_angle_brackets")) { if (!_conf.has_bool("Bridge", "username_angle_brackets")) {
_conf.set("Bridge", "username_angle_brackets", true); _conf.set("Bridge", "username_angle_brackets", true);
@ -37,7 +39,8 @@ Bridge::Bridge(
auto& v_group = _vgroups.at(tmp_name_to_id.at(tmp_vgroup_str)); auto& v_group = _vgroups.at(tmp_name_to_id.at(tmp_vgroup_str));
auto& new_vgc = v_group.contacts.emplace_back(); auto& new_vgc = v_group.contacts.emplace_back();
new_vgc.c = {_cr, entt::null}; // this is annoying af //new_vgc.c = {}; // TODO: does this work here?
new_vgc.c = _cs.contactHandle(entt::null);
new_vgc.id = hex2bin(contact_id); new_vgc.id = hex2bin(contact_id);
} }
@ -66,10 +69,10 @@ void Bridge::updateVGroups(void) {
if (!vgc.c.valid()) { if (!vgc.c.valid()) {
// search // search
auto view = _cr.view<Contact::Components::TagBig, Contact::Components::ID>(); auto view = _cs.registry().view<Contact::Components::TagBig, Contact::Components::ID>();
for (const auto c : view) { for (const auto c : view) {
if (view.get<Contact::Components::ID>(c).data == vgc.id) { if (view.get<Contact::Components::ID>(c).data == vgc.id) {
vgc.c = {_cr, c}; vgc.c = _cs.contactHandle(c);
std::cout << "Bridge: found contact for vgroup\n"; std::cout << "Bridge: found contact for vgroup\n";
break; break;
} }
@ -95,15 +98,30 @@ void Bridge::registerCommands(void) {
const auto contact_from = m.get<Message::Components::ContactFrom>().c; const auto contact_from = m.get<Message::Components::ContactFrom>().c;
const auto contact_to = m.get<Message::Components::ContactTo>().c; const auto contact_to = m.get<Message::Components::ContactTo>().c;
// TODO: if no vgroup name supplied ContactHandle4 group_contact;
const VirtualGroups* vg = nullptr;
Contact3Handle group_contact; if (!params.empty()) { // vgroup_name supplied
if (/*is public ?*/ _c_to_vg.count({_cr, contact_to})) { for (const auto& vg_it : _vgroups) {
if (vg_it.vg_name == params) {
vg = &vg_it;
break;
}
}
if (vg == nullptr) {
_rmm.sendText(
contact_from,
"The supplied vgroup name does not exist!"
);
return true;
}
} else {
if (/*is public ?*/ _c_to_vg.count(_cs.contactHandle(contact_to))) {
// message was sent public in group // message was sent public in group
group_contact = {_cr, contact_to}; group_contact = _cs.contactHandle(contact_to);
} else if (_cr.all_of<Contact::Components::Parent>(contact_from)) { } else if (_cs.registry().all_of<Contact::Components::Parent>(contact_from)) {
// use parent of sender // use parent of sender
group_contact = {_cr, _cr.get<Contact::Components::Parent>(contact_from).parent}; group_contact = _cs.contactHandle(_cs.registry().get<Contact::Components::Parent>(contact_from).parent);
} else if (false /* parent of contact_to ? */) { } else if (false /* parent of contact_to ? */) {
} }
@ -117,13 +135,16 @@ void Bridge::registerCommands(void) {
} }
assert(static_cast<bool>(group_contact)); assert(static_cast<bool>(group_contact));
const auto& vg = _vgroups.at(_c_to_vg.at(group_contact)); vg = &_vgroups.at(_c_to_vg.at(group_contact));
}
assert(vg != nullptr);
_rmm.sendText( _rmm.sendText(
contact_from, contact_from,
"Contacts online in other bridged group(s) in vgroup '" + vg.vg_name + "'" "Contacts online in other bridged group(s) in vgroup '" + vg->vg_name + "'"
); );
for (const auto& vgc : vg.contacts) { for (const auto& vgc : vg->contacts) {
if (vgc.c == group_contact) { if (vgc.c == group_contact) {
// skip self // skip self
continue; continue;
@ -137,10 +158,12 @@ void Bridge::registerCommands(void) {
"' id:" + bin2hex(vgc.id) "' id:" + bin2hex(vgc.id)
); );
const auto& cr = _cs.registry();
// for each sub contact // for each sub contact
for (const auto& sub_c : vgc.c.get<Contact::Components::ParentOf>().subs) { for (const auto& sub_c : vgc.c.get<Contact::Components::ParentOf>().subs) {
if ( if (
const auto* sub_cs = _cr.try_get<Contact::Components::ConnectionState>(sub_c); const auto* sub_cs = cr.try_get<Contact::Components::ConnectionState>(sub_c);
sub_cs != nullptr && sub_cs->state == Contact::Components::ConnectionState::disconnected sub_cs != nullptr && sub_cs->state == Contact::Components::ConnectionState::disconnected
) { ) {
// skip offline // skip offline
@ -150,9 +173,9 @@ void Bridge::registerCommands(void) {
_rmm.sendText( _rmm.sendText(
contact_from, contact_from,
" '" + " '" +
(_cr.all_of<Contact::Components::Name>(sub_c) ? _cr.get<Contact::Components::Name>(sub_c).name : "<unk>") + (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) : "") (cr.all_of<Contact::Components::ID>(sub_c) ? " id:" + bin2hex(cr.get<Contact::Components::ID>(sub_c).data) : "")
); );
} }
@ -174,7 +197,7 @@ void Bridge::registerCommands(void) {
std::cout << "Bridge: registered commands\n"; std::cout << "Bridge: registered commands\n";
} }
const Bridge::VirtualGroups* Bridge::findVGforContact(const Contact3Handle& c) { const Bridge::VirtualGroups* Bridge::findVGforContact(const ContactHandle4& c) {
return nullptr; return nullptr;
} }
@ -192,20 +215,22 @@ bool Bridge::onEvent(const Message::Events::MessageConstruct& e) {
} }
if (e.e.all_of<Message::Components::Timestamp>()) { if (e.e.all_of<Message::Components::Timestamp>()) {
int64_t time_diff = int64_t(Message::getTimeMS()) - int64_t(e.e.get<Message::Components::Timestamp>().ts); int64_t time_diff = int64_t(getTimeMS()) - int64_t(e.e.get<Message::Components::Timestamp>().ts);
if (time_diff > 5*1000*60) { if (time_diff > 5*1000*60) {
return false; // message too old return false; // message too old
} }
} }
const auto& cr = _cs.registry();
const auto contact_from = e.e.get<Message::Components::ContactFrom>().c; const auto contact_from = e.e.get<Message::Components::ContactFrom>().c;
if (_cr.any_of<Contact::Components::TagSelfStrong, Contact::Components::TagSelfWeak>(contact_from)) { if (cr.any_of<Contact::Components::TagSelfStrong, Contact::Components::TagSelfWeak>(contact_from)) {
return false; // skip own messages return false; // skip own messages
} }
const auto contact_to = e.e.get<Message::Components::ContactTo>().c; const auto contact_to = e.e.get<Message::Components::ContactTo>().c;
// if e.e <contact to> is in c to vg // if e.e <contact to> is in c to vg
const auto it = _c_to_vg.find(Contact3Handle{_cr, contact_to}); const auto it = _c_to_vg.find(_cs.contactHandle(contact_to));
if (it == _c_to_vg.cend()) { if (it == _c_to_vg.cend()) {
return false; // contact is not bridged return false; // contact is not bridged
} }
@ -220,8 +245,8 @@ bool Bridge::onEvent(const Message::Events::MessageConstruct& e) {
from_str += "<"; from_str += "<";
} }
if (_cr.all_of<Contact::Components::Name>(contact_from)) { if (cr.all_of<Contact::Components::Name>(contact_from)) {
const auto& name = _cr.get<Contact::Components::Name>(contact_from).name; const auto& name = cr.get<Contact::Components::Name>(contact_from).name;
if (name.empty()) { if (name.empty()) {
from_str += "UNK"; from_str += "UNK";
} else { } else {
@ -230,9 +255,9 @@ bool Bridge::onEvent(const Message::Events::MessageConstruct& e) {
} }
if (_conf.get_bool("Bridge", "username_id", vgroup.vg_name).value()) { if (_conf.get_bool("Bridge", "username_id", vgroup.vg_name).value()) {
if (_cr.all_of<Contact::Components::ID>(contact_from)) { if (cr.all_of<Contact::Components::ID>(contact_from)) {
// copy // copy
auto id = _cr.get<Contact::Components::ID>(contact_from).data; auto id = cr.get<Contact::Components::ID>(contact_from).data;
id.resize(3); // TODO:make size configurable id.resize(3); // TODO:make size configurable
// TODO: make seperator configurable // TODO: make seperator configurable

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <solanaceae/contact/contact_model3.hpp> #include <solanaceae/contact/fwd.hpp>
#include <solanaceae/message3/registry_message_model.hpp> #include <solanaceae/message3/registry_message_model.hpp>
#include <vector> #include <vector>
@ -13,14 +13,15 @@ struct ConfigModelI;
class MessageCommandDispatcher; class MessageCommandDispatcher;
class Bridge : public RegistryMessageModelEventI { class Bridge : public RegistryMessageModelEventI {
Contact3Registry& _cr; ContactStore4I& _cs;
RegistryMessageModel& _rmm; RegistryMessageModelI& _rmm;
RegistryMessageModelI::SubscriptionReference _rmm_sr;
ConfigModelI& _conf; ConfigModelI& _conf;
MessageCommandDispatcher* _mcd; MessageCommandDispatcher* _mcd;
struct VirtualGroups { struct VirtualGroups {
struct VContact { struct VContact {
Contact3Handle c; // might be null ContactHandle4 c; // might be null
std::vector<uint8_t> id; // if contact appears, we check std::vector<uint8_t> id; // if contact appears, we check
}; };
std::vector<VContact> contacts; std::vector<VContact> contacts;
@ -29,14 +30,16 @@ class Bridge : public RegistryMessageModelEventI {
// TODO: cache settings here? // TODO: cache settings here?
}; };
std::vector<VirtualGroups> _vgroups; std::vector<VirtualGroups> _vgroups;
std::map<Contact3Handle, size_t> _c_to_vg; std::map<ContactHandle4, size_t> _c_to_vg;
float _iterate_timer {0.f}; float _iterate_timer {0.f};
public: public:
static constexpr const char* version {"1"};
Bridge( Bridge(
Contact3Registry& cr, ContactStore4I& cs,
RegistryMessageModel& rmm, RegistryMessageModelI& rmm,
ConfigModelI& conf, ConfigModelI& conf,
MessageCommandDispatcher* mcd = nullptr MessageCommandDispatcher* mcd = nullptr
); );
@ -47,7 +50,7 @@ class Bridge : public RegistryMessageModelEventI {
private: private:
void updateVGroups(void); void updateVGroups(void);
void registerCommands(void); void registerCommands(void);
const VirtualGroups* findVGforContact(const Contact3Handle& c); const VirtualGroups* findVGforContact(const ContactHandle4& c);
protected: // mm protected: // mm
bool onEvent(const Message::Events::MessageConstruct& e) override; bool onEvent(const Message::Events::MessageConstruct& e) override;