Skip to content

Commit

Permalink
feat: add ngc events
Browse files Browse the repository at this point in the history
  • Loading branch information
Green-Sky committed Dec 25, 2023
1 parent b9d15ec commit 2e30bfe
Show file tree
Hide file tree
Showing 28 changed files with 5,847 additions and 3 deletions.
18 changes: 18 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,24 @@ set(toxcore_SOURCES
toxcore/events/friend_status_message.c
toxcore/events/friend_typing.c
toxcore/events/self_connection_status.c
toxcore/events/group_custom_packet.c
toxcore/events/group_custom_private_packet.c
toxcore/events/group_invite.c
toxcore/events/group_join_fail.c
toxcore/events/group_message.c
toxcore/events/group_moderation.c
toxcore/events/group_password.c
toxcore/events/group_peer_exit.c
toxcore/events/group_peer_join.c
toxcore/events/group_peer_limit.c
toxcore/events/group_peer_name.c
toxcore/events/group_peer_status.c
toxcore/events/group_privacy_state.c
toxcore/events/group_private_message.c
toxcore/events/group_self_join.c
toxcore/events/group_topic.c
toxcore/events/group_topic_lock.c
toxcore/events/group_voice_state.c
toxcore/forwarding.c
toxcore/forwarding.h
toxcore/friend_connection.c
Expand Down
2 changes: 0 additions & 2 deletions other/event_tooling/generate_event_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,6 @@ int main(int argc, char** argv) {
}
},

#if 0 // not yet :)
{
"Group_Peer_Name",
{
Expand Down Expand Up @@ -768,7 +767,6 @@ int main(int argc, char** argv) {
EventTypeTrivial{"Tox_Group_Mod_Event", "mod_type"},
}
},
#endif
};

if (argc < 2) {
Expand Down
18 changes: 18 additions & 0 deletions toxcore/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \
../toxcore/events/friend_status_message.c \
../toxcore/events/friend_typing.c \
../toxcore/events/self_connection_status.c \
../toxcore/events/group_custom_packet.c \
../toxcore/events/group_custom_private_packet.c \
../toxcore/events/group_invite.c \
../toxcore/events/group_join_fail.c \
../toxcore/events/group_message.c \
../toxcore/events/group_moderation.c \
../toxcore/events/group_password.c \
../toxcore/events/group_peer_exit.c \
../toxcore/events/group_peer_join.c \
../toxcore/events/group_peer_limit.c \
../toxcore/events/group_peer_name.c \
../toxcore/events/group_peer_status.c \
../toxcore/events/group_privacy_state.c \
../toxcore/events/group_private_message.c \
../toxcore/events/group_self_join.c \
../toxcore/events/group_topic.c \
../toxcore/events/group_topic_lock.c \
../toxcore/events/group_voice_state.c \
../toxcore/DHT.h \
../toxcore/DHT.c \
../toxcore/mem.h \
Expand Down
18 changes: 18 additions & 0 deletions toxcore/events/events_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ tox_friend_status_cb tox_events_handle_friend_status;
tox_friend_status_message_cb tox_events_handle_friend_status_message;
tox_friend_typing_cb tox_events_handle_friend_typing;
tox_self_connection_status_cb tox_events_handle_self_connection_status;
tox_group_peer_name_cb tox_events_handle_group_peer_name;
tox_group_peer_status_cb tox_events_handle_group_peer_status;
tox_group_topic_cb tox_events_handle_group_topic;
tox_group_privacy_state_cb tox_events_handle_group_privacy_state;
tox_group_voice_state_cb tox_events_handle_group_voice_state;
tox_group_topic_lock_cb tox_events_handle_group_topic_lock;
tox_group_peer_limit_cb tox_events_handle_group_peer_limit;
tox_group_password_cb tox_events_handle_group_password;
tox_group_message_cb tox_events_handle_group_message;
tox_group_private_message_cb tox_events_handle_group_private_message;
tox_group_custom_packet_cb tox_events_handle_group_custom_packet;
tox_group_custom_private_packet_cb tox_events_handle_group_custom_private_packet;
tox_group_invite_cb tox_events_handle_group_invite;
tox_group_peer_join_cb tox_events_handle_group_peer_join;
tox_group_peer_exit_cb tox_events_handle_group_peer_exit;
tox_group_self_join_cb tox_events_handle_group_self_join;
tox_group_join_fail_cb tox_events_handle_group_join_fail;
tox_group_moderation_cb tox_events_handle_group_moderation;

non_null(2) nullable(1)
bool tox_events_pack(const Tox_Events *events, Bin_Pack *bp);
Expand Down
270 changes: 270 additions & 0 deletions toxcore/events/group_custom_packet.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
*/

#include "events_alloc.h"

#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"


/*****************************************************
*
* :: struct and accessors
*
*****************************************************/


struct Tox_Event_Group_Custom_Packet {
uint32_t group_number;
uint32_t peer_id;
uint8_t *data;
uint32_t data_length;
};

non_null()
static void tox_event_group_custom_packet_set_group_number(Tox_Event_Group_Custom_Packet *group_custom_packet,
uint32_t group_number)
{
assert(group_custom_packet != nullptr);
group_custom_packet->group_number = group_number;
}
uint32_t tox_event_group_custom_packet_get_group_number(const Tox_Event_Group_Custom_Packet *group_custom_packet)
{
assert(group_custom_packet != nullptr);
return group_custom_packet->group_number;
}

non_null()
static void tox_event_group_custom_packet_set_peer_id(Tox_Event_Group_Custom_Packet *group_custom_packet,
uint32_t peer_id)
{
assert(group_custom_packet != nullptr);
group_custom_packet->peer_id = peer_id;
}
uint32_t tox_event_group_custom_packet_get_peer_id(const Tox_Event_Group_Custom_Packet *group_custom_packet)
{
assert(group_custom_packet != nullptr);
return group_custom_packet->peer_id;
}

non_null()
static bool tox_event_group_custom_packet_set_data(Tox_Event_Group_Custom_Packet *group_custom_packet,
const uint8_t *data, uint32_t data_length)
{
assert(group_custom_packet != nullptr);

if (group_custom_packet->data != nullptr) {
free(group_custom_packet->data);
group_custom_packet->data = nullptr;
group_custom_packet->data_length = 0;
}

group_custom_packet->data = (uint8_t *)malloc(data_length);

if (group_custom_packet->data == nullptr) {
return false;
}

memcpy(group_custom_packet->data, data, data_length);
group_custom_packet->data_length = data_length;
return true;
}
uint32_t tox_event_group_custom_packet_get_data_length(const Tox_Event_Group_Custom_Packet *group_custom_packet)
{
assert(group_custom_packet != nullptr);
return group_custom_packet->data_length;
}
const uint8_t *tox_event_group_custom_packet_get_data(const Tox_Event_Group_Custom_Packet *group_custom_packet)
{
assert(group_custom_packet != nullptr);
return group_custom_packet->data;
}

non_null()
static void tox_event_group_custom_packet_construct(Tox_Event_Group_Custom_Packet *group_custom_packet)
{
*group_custom_packet = (Tox_Event_Group_Custom_Packet) {
0
};
}
non_null()
static void tox_event_group_custom_packet_destruct(Tox_Event_Group_Custom_Packet *group_custom_packet, const Memory *mem)
{
free(group_custom_packet->data);
}

bool tox_event_group_custom_packet_pack(
const Tox_Event_Group_Custom_Packet *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, TOX_EVENT_GROUP_CUSTOM_PACKET)
&& bin_pack_array(bp, 3)
&& bin_pack_u32(bp, event->group_number)
&& bin_pack_u32(bp, event->peer_id)
&& bin_pack_bin(bp, event->data, event->data_length);
}

non_null()
static bool tox_event_group_custom_packet_unpack_into(
Tox_Event_Group_Custom_Packet *event, Bin_Unpack *bu)
{
assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 3, nullptr)) {
return false;
}

return bin_unpack_u32(bu, &event->group_number)
&& bin_unpack_u32(bu, &event->peer_id)
&& bin_unpack_bin(bu, &event->data, &event->data_length);
}


/*****************************************************
*
* :: new/free/add/get/size/unpack
*
*****************************************************/

const Tox_Event_Group_Custom_Packet *tox_event_get_group_custom_packet(const Tox_Event *event)
{
return event->type == TOX_EVENT_GROUP_CUSTOM_PACKET ? event->data.group_custom_packet : nullptr;
}

Tox_Event_Group_Custom_Packet *tox_event_group_custom_packet_new(const Memory *mem)
{
Tox_Event_Group_Custom_Packet *const group_custom_packet =
(Tox_Event_Group_Custom_Packet *)mem_alloc(mem, sizeof(Tox_Event_Group_Custom_Packet));

if (group_custom_packet == nullptr) {
return nullptr;
}

tox_event_group_custom_packet_construct(group_custom_packet);
return group_custom_packet;
}

void tox_event_group_custom_packet_free(Tox_Event_Group_Custom_Packet *group_custom_packet, const Memory *mem)
{
if (group_custom_packet != nullptr) {
tox_event_group_custom_packet_destruct(group_custom_packet, mem);
}
mem_delete(mem, group_custom_packet);
}

non_null()
static Tox_Event_Group_Custom_Packet *tox_events_add_group_custom_packet(Tox_Events *events, const Memory *mem)
{
Tox_Event_Group_Custom_Packet *const group_custom_packet = tox_event_group_custom_packet_new(mem);

if (group_custom_packet == nullptr) {
return nullptr;
}

Tox_Event event;
event.type = TOX_EVENT_GROUP_CUSTOM_PACKET;
event.data.group_custom_packet = group_custom_packet;

tox_events_add(events, &event);
return group_custom_packet;
}

const Tox_Event_Group_Custom_Packet *tox_events_get_group_custom_packet(const Tox_Events *events, uint32_t index)
{
uint32_t group_custom_packet_index = 0;
const uint32_t size = tox_events_get_size(events);

for (uint32_t i = 0; i < size; ++i) {
if (group_custom_packet_index > index) {
return nullptr;
}

if (events->events[i].type == TOX_EVENT_GROUP_CUSTOM_PACKET) {
const Tox_Event_Group_Custom_Packet *group_custom_packet = events->events[i].data.group_custom_packet;
if (group_custom_packet_index == index) {
return group_custom_packet;
}
++group_custom_packet_index;
}
}

return nullptr;
}

uint32_t tox_events_get_group_custom_packet_size(const Tox_Events *events)
{
uint32_t group_custom_packet_size = 0;
const uint32_t size = tox_events_get_size(events);

for (uint32_t i = 0; i < size; ++i) {
if (events->events[i].type == TOX_EVENT_GROUP_CUSTOM_PACKET) {
++group_custom_packet_size;
}
}

return group_custom_packet_size;
}

bool tox_event_group_custom_packet_unpack(
Tox_Event_Group_Custom_Packet **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
*event = tox_event_group_custom_packet_new(mem);

if (*event == nullptr) {
return false;
}

return tox_event_group_custom_packet_unpack_into(*event, bu);
}

non_null()
static Tox_Event_Group_Custom_Packet *tox_event_group_custom_packet_alloc(void *user_data)
{
Tox_Events_State *state = tox_events_alloc(user_data);
assert(state != nullptr);

if (state->events == nullptr) {
return nullptr;
}

Tox_Event_Group_Custom_Packet *group_custom_packet = tox_events_add_group_custom_packet(state->events, state->mem);

if (group_custom_packet == nullptr) {
state->error = TOX_ERR_EVENTS_ITERATE_MALLOC;
return nullptr;
}

return group_custom_packet;
}


/*****************************************************
*
* :: event handler
*
*****************************************************/


void tox_events_handle_group_custom_packet(Tox *tox, uint32_t group_number, uint32_t peer_id, const uint8_t *data, size_t length,
void *user_data)
{
Tox_Event_Group_Custom_Packet *group_custom_packet = tox_event_group_custom_packet_alloc(user_data);

if (group_custom_packet == nullptr) {
return;
}

tox_event_group_custom_packet_set_group_number(group_custom_packet, group_number);
tox_event_group_custom_packet_set_peer_id(group_custom_packet, peer_id);
tox_event_group_custom_packet_set_data(group_custom_packet, data, length);
}
Loading

0 comments on commit 2e30bfe

Please sign in to comment.