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

131 lines
2.6 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>
#include <new>
2022-09-29 02:39:54 +02:00
#include <map>
#include <list>
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;
bool operator<(const _GroupID& rhs) const {
for (size_t i = 0; i < data.size(); i++) {
if (data[i] > rhs.data[i]) {
return false;
}
}
return true;
}
};
struct _PeerID {
std::array<uint8_t, TOX_GROUP_PEER_PUBLIC_KEY_SIZE> data;
bool operator<(const _PeerID& rhs) const {
for (size_t i = 0; i < data.size(); i++) {
if (data[i] > rhs.data[i]) {
return false;
}
}
return true;
}
};
2022-09-26 20:55:58 +02:00
struct NGC_HS1 {
void* temp;
NGC_HS1_options options;
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{};
Tox_Message_Type type;
std::string text;
};
struct Messages {
std::map<uint32_t, Message> dict;
std::list<uint32_t> order; // ordered list of message ids
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;
}
};
std::map<_GroupID, std::map<_PeerID, Messages>> 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
}
void NGC_HS1_iterate(Tox *tox, NGC_HS1* ngc_hs1_ctx/*, void *user_data*/) {
}
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
) {
printf("record_own_message %u\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);
}
ngc_hs1_ctx->history[g_id];
2022-09-26 20:55:58 +02:00
}