This repository has been archived on 2023-01-13. You can view files and clone it, but cannot push or open issues or pull requests.
NGC_HS1/ngc_hs1.cpp

550 lines
14 KiB
C++
Raw Normal View History

2022-09-26 20:55:58 +02:00
#include "./ngc_hs1.h"
2022-09-28 20:40:49 +02:00
#include <cstdint>
2022-09-30 02:04:25 +02:00
#include <cassert>
2022-09-28 20:40:49 +02:00
#include <new>
2022-09-29 02:39:54 +02:00
#include <map>
#include <list>
2022-10-01 01:38:42 +02:00
#include <set>
2022-09-30 02:04:25 +02:00
#include <optional>
#include <algorithm>
2022-09-28 20:40:49 +02:00
struct _GroupID {
2022-09-29 02:39:54 +02:00
std::array<uint8_t, TOX_GROUP_CHAT_ID_SIZE> data;
2022-09-30 02:04:25 +02:00
_GroupID(void) = default;
2022-09-30 23:38:19 +02:00
_GroupID(const _GroupID& other) : data(other.data) {}
2022-09-30 02:04:25 +02:00
_GroupID(_GroupID&&) = delete;
//_GroupID(_GroupID&& other) : data(std::move(other.data)) {}
2022-09-29 02:39:54 +02:00
bool operator<(const _GroupID& rhs) const {
for (size_t i = 0; i < data.size(); i++) {
2022-09-30 23:38:19 +02:00
if (data[i] < rhs.data[i]) {
return true;
} else if (data[i] > rhs.data[i]) {
2022-09-30 02:04:25 +02:00
return false;
}
}
2022-09-30 23:38:19 +02:00
return false; // equal
2022-09-30 02:04:25 +02:00
}
bool operator==(const _GroupID& rhs) const {
for (size_t i = 0; i < data.size(); i++) {
if (data[i] != rhs.data[i]) {
2022-09-29 02:39:54 +02:00
return false;
}
}
return true;
}
};
struct _PeerID {
std::array<uint8_t, TOX_GROUP_PEER_PUBLIC_KEY_SIZE> data;
2022-09-30 02:04:25 +02:00
_PeerID(void) = default;
_PeerID(const _PeerID& other) : data(other.data) {}
_PeerID(_PeerID&&) = delete;
2022-09-29 02:39:54 +02:00
bool operator<(const _PeerID& rhs) const {
for (size_t i = 0; i < data.size(); i++) {
2022-09-30 23:38:19 +02:00
if (data[i] < rhs.data[i]) {
return true;
} else if (data[i] > rhs.data[i]) {
2022-09-30 02:04:25 +02:00
return false;
}
}
2022-09-30 23:38:19 +02:00
return false; // equal
2022-09-30 02:04:25 +02:00
}
bool operator==(const _PeerID& rhs) const {
for (size_t i = 0; i < data.size(); i++) {
if (data[i] != rhs.data[i]) {
2022-09-29 02:39:54 +02:00
return false;
}
}
return true;
}
};
2022-09-30 02:04:25 +02:00
enum _PacketType : uint8_t {
INVALID = 0u,
2022-09-26 20:55:58 +02:00
2022-09-30 02:04:25 +02:00
// request last (few) message_ids for a peer
// - peer_key bytes (peer key we want to know ids for)
// - 1 byte (uint8_t count ids, atleast 1)
HS_REQUEST_LAST_IDS,
// respond to a request with 0 or more message ids, sorted by newest first
// - peer_key bytes (the msg_ids are from)
// - 1 byte (uint8_t count ids, can be 0)
// - array [
// - msg_id bytes (the message id
// - ]
HS_RESPONSE_LAST_IDS,
//TODO: make it possible to go further back
};
2022-09-26 20:55:58 +02:00
2022-09-30 02:04:25 +02:00
const char* _pkgid2str(_PacketType type) {
#define _HS1_CASE(x) case (x): return #x;
switch (type) {
_HS1_CASE(INVALID)
_HS1_CASE(HS_REQUEST_LAST_IDS)
_HS1_CASE(HS_RESPONSE_LAST_IDS)
default: return "<unk>";
}
#undef _HS1_CASE
}
struct NGC_HS1 {
2022-09-26 20:55:58 +02:00
NGC_HS1_options options;
2022-09-30 02:04:25 +02:00
// callbacks
//tox_group_message_cb* client_tox_msg_callback;
2022-09-28 20:40:49 +02:00
// key - key - key - value store
// group pubkey - peer pubkey - msg_id - message(type + text)
2022-09-29 02:39:54 +02:00
struct Message {
uint32_t msg_id{};
2022-09-30 02:04:25 +02:00
Tox_Message_Type type{};
std::string text{};
2022-09-29 02:39:54 +02:00
};
2022-09-30 02:04:25 +02:00
struct Peer {
std::optional<uint32_t> id;
2022-09-29 02:39:54 +02:00
std::map<uint32_t, Message> dict;
std::list<uint32_t> order; // ordered list of message ids
2022-10-01 01:38:42 +02:00
// msg_ids we have only heard of, with peer_number of who we heard it from
std::map<uint32_t, std::set<uint32_t>> heard_of;
2022-09-30 02:04:25 +02:00
// dont start immediatly
float time_since_last_request_sent {0.f};
2022-09-29 02:39:54 +02:00
void append(uint32_t msg_id, Tox_Message_Type type, const std::string& text) {
order.push_back(msg_id);
// overwrites
auto& new_msg = dict[msg_id];
new_msg.msg_id = msg_id;
new_msg.type = type;
new_msg.text = text;
2022-09-30 23:38:19 +02:00
2022-10-01 01:38:42 +02:00
if (heard_of.count(msg_id)) {
// we got history before we got the message
heard_of.erase(msg_id);
}
2022-09-30 23:38:19 +02:00
fprintf(stderr, "######## last msgs ########\n");
auto rit = order.crbegin();
for (size_t i = 0; i < 10 && rit != order.crend(); i++, rit++) {
fprintf(stderr, " %08X - %s\n", *rit, dict.at(*rit).text.c_str());
}
2022-09-29 02:39:54 +02:00
}
2022-10-01 01:38:42 +02:00
// returns if new (from that peer)
bool hear(uint32_t msg_id, uint32_t peer_number) {
if (dict.count(msg_id)) {
// we know
return false;
}
if (heard_of.count(msg_id) && heard_of.at(msg_id).count(peer_number)) {
// we heard it from that peer before
return false;
}
heard_of[msg_id].emplace(peer_number);
return true;
}
2022-09-29 02:39:54 +02:00
};
2022-09-30 02:04:25 +02:00
struct Group {
std::map<_PeerID, Peer> peers;
};
std::map<_GroupID, Group> history;
2022-09-26 20:55:58 +02:00
};
NGC_HS1* NGC_HS1_new(const struct NGC_HS1_options* options) {
2022-09-28 20:40:49 +02:00
NGC_HS1* context = new NGC_HS1;
2022-09-26 20:55:58 +02:00
context->options = *options;
return context;
}
void NGC_HS1_kill(NGC_HS1* ngc_hs1_ctx) {
2022-09-28 20:40:49 +02:00
delete ngc_hs1_ctx;
2022-09-26 20:55:58 +02:00
}
2022-09-30 02:04:25 +02:00
static void _iterate_group(Tox *tox, NGC_HS1* ngc_hs1_ctx, uint32_t group_number, float time_delta) {
//fprintf(stderr, "g:%u\n", g_i);
_GroupID g_id{};
{ // TODO: error
tox_group_get_chat_id(tox, group_number, g_id.data.data(), nullptr);
}
if (ngc_hs1_ctx->history.count(g_id) == 0) {
fprintf(stderr, "adding new group: %u %X%X%X%X\n",
group_number,
g_id.data.data()[0],
g_id.data.data()[1],
g_id.data.data()[2],
g_id.data.data()[3]
);
ngc_hs1_ctx->history[g_id];
} else {
auto& group = ngc_hs1_ctx->history[g_id];
// for each peer
for (auto& [key, peer] : group.peers) {
//fprintf(stderr, " p: %X%X%X%X\n", key.data.data()[0], key.data.data()[1], key.data.data()[2], key.data.data()[3]);
peer.time_since_last_request_sent += time_delta;
2022-09-30 23:38:19 +02:00
if (peer.time_since_last_request_sent > 15.f) {
2022-09-30 02:04:25 +02:00
peer.time_since_last_request_sent = 0.f;
fprintf(stderr, "requesting ids for %X%X%X%X\n", key.data.data()[0], key.data.data()[1], key.data.data()[2], key.data.data()[3]);
// TODO: other way around?
// ask everyone if they have newer stuff for this peer
// - 1 byte packet id
// - peer_key bytes (peer key we want to know ids for)
// - 1 byte (uint8_t count ids, atleast 1)
std::array<uint8_t, 1+TOX_GROUP_PEER_PUBLIC_KEY_SIZE+1> pkg;
pkg[0] = HS_REQUEST_LAST_IDS;
std::copy(key.data.begin(), key.data.end(), pkg.begin()+1);
pkg[1+TOX_GROUP_PEER_PUBLIC_KEY_SIZE] = 5; // request last (up to) 5 msg_ids
tox_group_send_custom_packet(tox, group_number, true, pkg.data(), pkg.size(), nullptr);
}
}
}
assert(ngc_hs1_ctx->history.size() != 0);
assert(ngc_hs1_ctx->history.count(g_id));
}
2022-09-26 20:55:58 +02:00
void NGC_HS1_iterate(Tox *tox, NGC_HS1* ngc_hs1_ctx/*, void *user_data*/) {
2022-09-30 02:04:25 +02:00
assert(ngc_hs1_ctx);
//fprintf(stderr, "groups: %u\n", ngc_hs1_ctx->history.size());
uint32_t group_count = tox_group_get_number_groups(tox);
// this can loop endless if toxcore misbehaves
for (uint32_t g_i = 0, g_c_done = 0; g_c_done < group_count; g_i++) {
Tox_Err_Group_Is_Connected g_err;
if (tox_group_is_connected(tox, g_i, &g_err)) {
// valid and connected here
// TODO: delta time, or other timers
2022-09-30 23:38:19 +02:00
_iterate_group(tox, ngc_hs1_ctx, g_i, 0.02f);
2022-09-30 02:04:25 +02:00
g_c_done++;
} else if (g_err != TOX_ERR_GROUP_IS_CONNECTED_GROUP_NOT_FOUND) {
g_c_done++;
} // else do nothing
// safety
if (g_i > group_count + 1000) {
fprintf(stderr, "WAY PAST GOUPS in iterate\n");
break;
}
}
}
void NGC_HS1_peer_online(Tox* tox, NGC_HS1* ngc_hs1_ctx, uint32_t group_number, uint32_t peer_number, bool online) {
// get group id
_GroupID g_id{};
{ // TODO: error
tox_group_get_chat_id(tox, group_number, g_id.data.data(), nullptr);
}
auto& group = ngc_hs1_ctx->history[g_id];
if (online) {
// get peer id
_PeerID p_id{};
{ // TODO: error
tox_group_peer_get_public_key(tox, group_number, peer_number, p_id.data.data(), nullptr);
}
auto& peer = group.peers[p_id];
peer.id = peer_number;
} else { // offline
// search
for (auto& [key, peer] : group.peers) {
if (peer.id.has_value() && peer.id.value() == peer_number) {
peer.id = {}; // reset
break;
}
}
}
2022-09-26 20:55:58 +02:00
}
bool NGC_HS1_shim_group_send_message(
const Tox *tox,
NGC_HS1* ngc_hs1_ctx,
uint32_t group_number,
Tox_Message_Type type, const uint8_t *message, size_t length,
uint32_t *message_id,
Tox_Err_Group_Send_Message *error
) {
uint32_t* msg_id_ptr = message_id;
uint32_t msg_id_placeholder = 0;
2022-09-29 02:39:54 +02:00
if (msg_id_ptr == nullptr) {
2022-09-26 20:55:58 +02:00
msg_id_ptr = &msg_id_placeholder;
}
bool ret = tox_group_send_message(tox, group_number, type, message, length, msg_id_ptr, error);
2022-09-29 02:39:54 +02:00
NGC_HS1_record_own_message(tox, ngc_hs1_ctx, group_number, type, message, length, *msg_id_ptr);
2022-09-26 20:55:58 +02:00
return ret;
}
// record own msg
void NGC_HS1_record_own_message(
const Tox *tox,
NGC_HS1* ngc_hs1_ctx,
uint32_t group_number,
Tox_Message_Type type, const uint8_t *message, size_t length, uint32_t message_id
) {
2022-09-30 02:04:25 +02:00
fprintf(stderr, "record_own_message %08X\n", message_id);
2022-09-29 02:39:54 +02:00
// get group id
_GroupID g_id{};
{ // TODO: error
tox_group_get_chat_id(tox, group_number, g_id.data.data(), nullptr);
}
// get peer id
_PeerID p_id{};
{ // TODO: error
tox_group_self_get_public_key(tox, group_number, p_id.data.data(), nullptr);
}
2022-09-30 02:04:25 +02:00
ngc_hs1_ctx->history[g_id].peers[p_id].append(message_id, type, std::string{message, message+length});
assert(ngc_hs1_ctx->history.size() != 0);
assert(ngc_hs1_ctx->history.count(g_id));
}
// record others msg
void NGC_HS1_record_message(
const Tox *tox,
NGC_HS1* ngc_hs1_ctx,
uint32_t group_number,
uint32_t peer_number,
Tox_Message_Type type, const uint8_t *message, size_t length, uint32_t message_id
) {
if (!ngc_hs1_ctx->options.record_others) {
return;
}
fprintf(stderr, "record_message %08X\n", message_id);
// get group id
_GroupID g_id{};
{ // TODO: error
tox_group_get_chat_id(tox, group_number, g_id.data.data(), nullptr);
}
// get peer id
_PeerID p_id{};
{ // TODO: error
tox_group_peer_get_public_key(tox, group_number, peer_number, p_id.data.data(), nullptr);
}
ngc_hs1_ctx->history[g_id].peers[p_id].append(message_id, type, std::string{message, message+length});
}
static void _handle_HS_REQUEST_LAST_IDS(
Tox* tox,
NGC_HS1* ngc_hs1_ctx,
uint32_t group_number,
uint32_t peer_number,
const uint8_t *data,
size_t length
);
2022-09-30 23:38:19 +02:00
static void _handle_HS_RESPONSE_LAST_IDS(
Tox* tox,
NGC_HS1* ngc_hs1_ctx,
uint32_t group_number,
uint32_t peer_number,
const uint8_t *data,
size_t length
);
2022-09-30 02:04:25 +02:00
#define _HS1_HAVE(x, error) if ((length - curser) < (x)) { error; }
void NGC_HS1_handle_group_custom_packet(
Tox* tox,
NGC_HS1* ngc_hs1_ctx,
uint32_t group_number,
uint32_t peer_number,
const uint8_t *data,
size_t length
//void *user_data
) {
size_t curser = 0;
_HS1_HAVE(1, return)
_PacketType pkg_type = static_cast<_PacketType>(*(data + curser));
curser++;
fprintf(stderr, "custom_packet [%s] %u\n", _pkgid2str(pkg_type), length);
switch (pkg_type) {
case INVALID:
break;
case HS_REQUEST_LAST_IDS:
_handle_HS_REQUEST_LAST_IDS(tox, ngc_hs1_ctx, group_number, peer_number, data+curser, length-curser);
break;
case HS_RESPONSE_LAST_IDS:
2022-09-30 23:38:19 +02:00
_handle_HS_RESPONSE_LAST_IDS(tox, ngc_hs1_ctx, group_number, peer_number, data+curser, length-curser);
2022-09-30 02:04:25 +02:00
break;
}
}
static void _handle_HS_REQUEST_LAST_IDS(
Tox* tox,
NGC_HS1* ngc_hs1_ctx,
uint32_t group_number,
uint32_t peer_number,
const uint8_t *data,
size_t length
) {
size_t curser = 0;
_PeerID p_key;
_HS1_HAVE(p_key.data.size(), fprintf(stderr, "packet too small, missing pkey\n"); return)
2022-10-01 01:38:42 +02:00
2022-09-30 02:04:25 +02:00
std::copy(data+curser, data+curser+p_key.data.size(), p_key.data.begin());
curser += p_key.data.size();
_HS1_HAVE(1, fprintf(stderr, "packet too small, missing count\n"); return)
uint8_t last_msg_id_count = data[curser++];
fprintf(stderr, "got request for last %u ids\n", last_msg_id_count);
2022-09-30 23:38:19 +02:00
// get group id
_GroupID g_id{};
{ // TODO: error
tox_group_get_chat_id(tox, group_number, g_id.data.data(), nullptr);
}
auto& group = ngc_hs1_ctx->history[g_id];
std::vector<uint32_t> message_ids{};
if (!group.peers.empty() && group.peers.count(p_key)) {
const auto& peer = group.peers.at(p_key);
auto rit = peer.order.crbegin();
for (size_t c = 0; c < last_msg_id_count && rit != peer.order.crend(); c++, rit++) {
message_ids.push_back(*rit);
}
}
// - 1 byte packet id
// respond to a request with 0 or more message ids, sorted by newest first
// - peer_key bytes (the msg_ids are from)
// - 1 byte (uint8_t count ids, can be 0)
// - array [
// - msg_id bytes (the message id
// - ]
//std::array<uint8_t, 1+TOX_GROUP_PEER_PUBLIC_KEY_SIZE+1+> pkg;
std::vector<uint8_t> pkg;
pkg.resize(1+TOX_GROUP_PEER_PUBLIC_KEY_SIZE+1+sizeof(uint32_t)*message_ids.size());
2022-10-01 01:38:42 +02:00
size_t packing_curser = 0;
2022-09-30 23:38:19 +02:00
2022-10-01 01:38:42 +02:00
pkg[packing_curser++] = HS_RESPONSE_LAST_IDS;
2022-09-30 23:38:19 +02:00
2022-10-01 01:38:42 +02:00
std::copy(p_key.data.begin(), p_key.data.end(), pkg.begin()+packing_curser);
packing_curser += p_key.data.size();
pkg[packing_curser++] = message_ids.size();
2022-09-30 23:38:19 +02:00
for (size_t i = 0; i < message_ids.size(); i++) {
2022-10-01 01:38:42 +02:00
const uint8_t* tmp_ptr = reinterpret_cast<uint8_t*>(message_ids.data()+i);
2022-09-30 23:38:19 +02:00
// HACK: little endian
2022-10-01 01:38:42 +02:00
//std::copy(tmp_ptr, tmp_ptr+sizeof(uint32_t), pkg.begin()+1+TOX_GROUP_PEER_PUBLIC_KEY_SIZE+1+i*sizeof(uint32_t));
std::copy(tmp_ptr, tmp_ptr+sizeof(uint32_t), pkg.begin()+packing_curser);
packing_curser += sizeof(uint32_t);
2022-09-30 23:38:19 +02:00
}
tox_group_send_custom_private_packet(tox, group_number, peer_number, true, pkg.data(), pkg.size(), nullptr);
}
static void _handle_HS_RESPONSE_LAST_IDS(
Tox* tox,
NGC_HS1* ngc_hs1_ctx,
uint32_t group_number,
uint32_t peer_number,
const uint8_t *data,
size_t length
) {
size_t curser = 0;
_PeerID p_key;
_HS1_HAVE(p_key.data.size(), fprintf(stderr, "packet too small, missing pkey\n"); return)
2022-10-01 01:38:42 +02:00
2022-09-30 23:38:19 +02:00
std::copy(data+curser, data+curser+p_key.data.size(), p_key.data.begin());
curser += p_key.data.size();
// TODO: did we ask?
_HS1_HAVE(1, fprintf(stderr, "packet too small, missing count\n"); return)
uint8_t last_msg_id_count = data[curser++];
2022-10-01 01:38:42 +02:00
fprintf(stderr, "got response with last %u ids:\n", last_msg_id_count);
if (last_msg_id_count == 0) {
return;
}
// get group id
_GroupID g_id{};
{ // TODO: error
tox_group_get_chat_id(tox, group_number, g_id.data.data(), nullptr);
}
// get peer
auto& peer = ngc_hs1_ctx->history[g_id].peers[p_key];
for (size_t i = 0; i < last_msg_id_count && curser+sizeof(uint32_t) <= length; i++) {
uint32_t msg_id;
// HACK: little endian
std::copy(data+curser, data+curser+sizeof(uint32_t), reinterpret_cast<uint8_t*>(&msg_id));
curser += sizeof(uint32_t);
fprintf(stderr, " %08X", msg_id);
if (peer.hear(msg_id, peer_number)) {
fprintf(stderr, " - NEW");
}
fprintf(stderr, "\n");
}
2022-09-26 20:55:58 +02:00
}
2022-09-30 02:04:25 +02:00
#undef _HS1_HAVE