diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6c168a1f71..ab9ba1bdb3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -35,6 +35,17 @@ jobs: - name: Build, test, and upload coverage run: other/docker/coverage/run + generate-events: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + submodules: recursive + - name: Run generate_event_c + run: | + other/event_tooling/run + git diff --exit-code + cimplefmt: runs-on: ubuntu-latest steps: diff --git a/CMakeLists.txt b/CMakeLists.txt index f8688a3013..aaf6b37507 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -248,6 +248,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 @@ -313,6 +331,8 @@ set(toxcore_SOURCES toxcore/tox.c toxcore/tox_dispatch.c toxcore/tox_dispatch.h + toxcore/tox_event.c + toxcore/tox_event.h toxcore/tox_events.c toxcore/tox_events.h toxcore/tox.h diff --git a/auto_tests/Makefile.inc b/auto_tests/Makefile.inc index d970248495..1553445d30 100644 --- a/auto_tests/Makefile.inc +++ b/auto_tests/Makefile.inc @@ -192,14 +192,14 @@ TCP_test_SOURCES = ../auto_tests/TCP_test.c TCP_test_CFLAGS = $(AUTOTEST_CFLAGS) TCP_test_LDADD = $(AUTOTEST_LDADD) -tox_events_test_SOURCES = ../auto_tests/tox_events_test.c -tox_events_test_CFLAGS = $(AUTOTEST_CFLAGS) -tox_events_test_LDADD = $(AUTOTEST_LDADD) - tox_dispatch_test_SOURCES = ../auto_tests/tox_dispatch_test.c tox_dispatch_test_CFLAGS = $(AUTOTEST_CFLAGS) tox_dispatch_test_LDADD = $(AUTOTEST_LDADD) +tox_events_test_SOURCES = ../auto_tests/tox_events_test.c +tox_events_test_CFLAGS = $(AUTOTEST_CFLAGS) +tox_events_test_LDADD = $(AUTOTEST_LDADD) + tox_many_tcp_test_SOURCES = ../auto_tests/tox_many_tcp_test.c tox_many_tcp_test_CFLAGS = $(AUTOTEST_CFLAGS) tox_many_tcp_test_LDADD = $(AUTOTEST_LDADD) diff --git a/auto_tests/tox_dispatch_test.c b/auto_tests/tox_dispatch_test.c index b9e63a76b8..01294589e2 100644 --- a/auto_tests/tox_dispatch_test.c +++ b/auto_tests/tox_dispatch_test.c @@ -32,31 +32,36 @@ static void handle_events_friend_message(Tox *tox, const Tox_Event_Friend_Messag static void dump_events(const char *path, const Tox_Events *events) { - if (want_dump_events) { - FILE *fh = fopen(path, "w"); - ck_assert(fh != nullptr); - const uint32_t len = tox_events_bytes_size(events); - uint8_t *buf = (uint8_t *)malloc(len); - ck_assert(buf != nullptr); - tox_events_get_bytes(events, buf); - fwrite(buf, 1, len, fh); - free(buf); - fclose(fh); - } + FILE *fh = fopen(path, "w"); + ck_assert(fh != nullptr); + const uint32_t len = tox_events_bytes_size(events); + uint8_t *buf = (uint8_t *)malloc(len); + ck_assert(buf != nullptr); + ck_assert(tox_events_get_bytes(events, buf)); + fwrite(buf, 1, len, fh); + free(buf); + fclose(fh); } static void print_events(const Tox_System *sys, Tox_Events *events) { const uint32_t size = tox_events_bytes_size(events); - uint8_t *bytes = (uint8_t *)malloc(size); - ck_assert(bytes != nullptr); + uint8_t *bytes1 = (uint8_t *)malloc(size); + uint8_t *bytes2 = (uint8_t *)malloc(size); + ck_assert(bytes1 != nullptr); + ck_assert(bytes2 != nullptr); + + ck_assert(tox_events_get_bytes(events, bytes1)); + ck_assert(tox_events_get_bytes(events, bytes2)); - tox_events_get_bytes(events, bytes); + // Make sure get_bytes is deterministic. + ck_assert(memcmp(bytes1, bytes2, size) == 0); - Tox_Events *events_copy = tox_events_load(sys, bytes, size); + Tox_Events *events_copy = tox_events_load(sys, bytes1, size); ck_assert(events_copy != nullptr); - free(bytes); + free(bytes1); + free(bytes2); ck_assert(tox_events_equal(sys, events, events_copy)); @@ -74,7 +79,9 @@ static bool await_message(Tox **toxes, const Tox_Dispatch *dispatch) // Check if tox 2 got the message from tox 1. Tox_Events *events = tox_events_iterate(toxes[1], false, nullptr); - dump_events("/tmp/test.mp", events); + if (want_dump_events) { + dump_events("/tmp/test.mp", events); + } bool success = false; tox_dispatch_invoke(dispatch, events, toxes[1], &success); diff --git a/auto_tests/tox_events_test.c b/auto_tests/tox_events_test.c index 04549b2179..1b7cb729aa 100644 --- a/auto_tests/tox_events_test.c +++ b/auto_tests/tox_events_test.c @@ -22,12 +22,23 @@ static bool await_message(Tox **toxes) Tox_Events *events = tox_events_iterate(toxes[1], false, nullptr); if (events != nullptr) { - ck_assert(tox_events_get_friend_message_size(events) == 1); - const Tox_Event_Friend_Message *msg_event = tox_events_get_friend_message(events, 0); + uint32_t events_size = tox_events_get_size(events); + ck_assert(events_size == 1); + + const Tox_Event_Friend_Message *msg_event = nullptr; + for (uint32_t j = 0; j < events_size; ++j) { + const Tox_Event *ev = tox_events_get(events, j); + if (tox_event_get_type(ev) == TOX_EVENT_FRIEND_MESSAGE) { + msg_event = tox_event_get_friend_message(ev); + } + } + + ck_assert(msg_event != nullptr); ck_assert(tox_event_friend_message_get_message_length(msg_event) == sizeof("hello")); const uint8_t *msg = tox_event_friend_message_get_message(msg_event); ck_assert_msg(memcmp(msg, "hello", sizeof("hello")) == 0, "message was not expected 'hello' but '%s'", (const char *)msg); + tox_events_free(events); return true; } diff --git a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 index 7a6701d5bd..0cf2867500 100644 --- a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 +++ b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 @@ -1 +1 @@ -ccaf7a29c16d97068952d872b8663e38cc3d958aff01baeafee04e0ac6f3ac98 /usr/local/bin/tox-bootstrapd +0eb835fe755f1162748029660f46717952f33775e7a845c389ecb4b4e3fbb998 /usr/local/bin/tox-bootstrapd diff --git a/other/event_tooling/Dockerfile b/other/event_tooling/Dockerfile new file mode 100644 index 0000000000..b94e6f0e7f --- /dev/null +++ b/other/event_tooling/Dockerfile @@ -0,0 +1,12 @@ +FROM ubuntu:22.04 + +RUN apt-get update && apt-get install --no-install-recommends -y \ + build-essential \ + ca-certificates \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + +WORKDIR /src +COPY generate_event_c.cpp /src/ +RUN ["g++", "generate_event_c.cpp", "-o", "generate_event_c"] +CMD ["./generate_event_c"] diff --git a/other/event_tooling/generate_event_c.cpp b/other/event_tooling/generate_event_c.cpp index d3ac0475db..b736b6f2a4 100644 --- a/other/event_tooling/generate_event_c.cpp +++ b/other/event_tooling/generate_event_c.cpp @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-or-later -// Copyright © 2023 The TokTok team. +// Copyright © 2023-2024 The TokTok team. // this file can be used to generate event.c files // requires c++17 @@ -119,22 +119,54 @@ void generate_event_impl(const std::string& event_name, const std::vector -#include -#include +#include )"; + if (need_stdlib_h) { + f << R"( +#include )"; + } + if (need_string_h) { + f << R"( +#include )"; + } + f << R"( #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" -#include "../tox_events.h" -#include "../tox_unpack.h" +#include "../tox_events.h")"; + if (need_tox_unpack_h) { + f << R"( +#include "../tox_unpack.h")"; + } + f << R"( /***************************************************** @@ -205,10 +237,11 @@ void generate_event_impl(const std::string& event_name, const std::vector" << t.name_data << " = nullptr;\n"; f << " " << event_name_l << "->" << t.name_length << " = 0;\n"; f << " }\n\n"; - f << " " << event_name_l << "->" << t.name_data << " = (uint8_t *)malloc(" << t.name_length << ");\n\n"; - f << " if (" << event_name_l << "->" << t.name_data << " == nullptr) {\n"; + f << " uint8_t *" << t.name_data << "_copy = (uint8_t *)malloc(" << t.name_length << ");\n\n"; + f << " if (" << t.name_data << "_copy == nullptr) {\n"; f << " return false;\n }\n\n"; - f << " memcpy(" << event_name_l << "->" << t.name_data << ", " << t.name_data << ", " << t.name_length << ");\n"; + f << " memcpy(" << t.name_data << "_copy, " << t.name_data << ", " << t.name_length << ");\n"; + f << " " << event_name_l << "->" << t.name_data << " = " << t.name_data << "_copy;\n"; f << " " << event_name_l << "->" << t.name_length << " = " << t.name_length << ";\n"; f << " return true;\n"; } @@ -371,28 +404,6 @@ void generate_event_impl(const std::string& event_name, const std::vector index) {\n"; - f << " return nullptr;\n }\n\n"; - f << " if (events->events[i].type == TOX_EVENT_" << str_toupper(event_name) << ") {\n"; - f << " const Tox_Event_" << event_name << " *" << event_name_l << " = events->events[i].data." << event_name_l << ";\n"; - f << " if (" << event_name_l << "_index == index) {\n"; - f << " return " << event_name_l << ";\n }\n"; - f << " ++" << event_name_l << "_index;\n }\n }\n\n return nullptr;\n}\n\n"; - - // get size - f << "uint32_t tox_events_get_" << event_name_l << "_size(const Tox_Events *events)\n{\n"; - f << " uint32_t " << event_name_l << "_size = 0;\n"; - f << " const uint32_t size = tox_events_get_size(events);\n\n"; - f << " for (uint32_t i = 0; i < size; ++i) {\n"; - f << " if (events->events[i].type == TOX_EVENT_" << str_toupper(event_name) << ") {\n"; - f << " ++" << event_name_l << "_size;\n }\n }\n\n"; - f << " return " << event_name_l << "_size;\n}\n\n"; - // unpack f << "bool tox_event_" << event_name_l << "_unpack(\n"; f << " Tox_Event_" << event_name << " **event, Bin_Unpack *bu, const Memory *mem)\n{\n"; @@ -524,7 +535,7 @@ int main(int argc, char** argv) { EventTypeTrivial{"uint32_t", "file_number"}, EventTypeTrivial{"uint32_t", "kind"}, EventTypeTrivial{"uint64_t", "file_size"}, - EventTypeByteRange{"filename", "filename_length", "length"}, // the latter two are ideally the same + EventTypeByteRange{"filename", "filename_length", "filename_length"}, } }, { @@ -626,7 +637,6 @@ int main(int argc, char** argv) { } }, -#if 0 // not yet :) { "Group_Peer_Name", { @@ -768,7 +778,6 @@ int main(int argc, char** argv) { EventTypeTrivial{"Tox_Group_Mod_Event", "mod_type"}, } }, -#endif }; if (argc < 2) { diff --git a/other/event_tooling/run b/other/event_tooling/run new file mode 100755 index 0000000000..95e2364649 --- /dev/null +++ b/other/event_tooling/run @@ -0,0 +1,8 @@ +#!/bin/sh + +set -eux +BUILD=events +docker build -t "toxchat/c-toxcore:$BUILD" "other/event_tooling" +docker run --rm -v "$PWD/toxcore/events:/src/out" -t "toxchat/c-toxcore:$BUILD" +sed -i -e 's/, uint16_t length,/, size_t length,/' toxcore/events/file_chunk_request.c +sed -i -e 's/^ 0/ TOX_CONNECTION_NONE/' toxcore/events/self_connection_status.c diff --git a/toxcore/BUILD.bazel b/toxcore/BUILD.bazel index 1232bf6253..becd3e8b37 100644 --- a/toxcore/BUILD.bazel +++ b/toxcore/BUILD.bazel @@ -918,11 +918,17 @@ cc_library( cc_library( name = "tox_events", - srcs = ["tox_events.c"] + glob([ + srcs = [ + "tox_event.c", + "tox_events.c", + ] + glob([ "events/*.c", - "events/*.h", ]), - hdrs = ["tox_events.h"], + hdrs = [ + "events/events_alloc.h", + "tox_event.h", + "tox_events.h", + ], visibility = ["//c-toxcore:__subpackages__"], deps = [ ":attributes", @@ -955,6 +961,7 @@ cc_fuzz_test( srcs = ["tox_events_fuzz_test.cc"], corpus = ["//tools/toktok-fuzzer/corpus:tox_events_fuzz_test"], deps = [ + ":tox_dispatch", ":tox_events", "//c-toxcore/testing/fuzzing:fuzz_support", ], diff --git a/toxcore/Makefile.inc b/toxcore/Makefile.inc index eecd52b792..5783c91bef 100644 --- a/toxcore/Makefile.inc +++ b/toxcore/Makefile.inc @@ -21,6 +21,8 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \ ../toxcore/events/conference_peer_list_changed.c \ ../toxcore/events/conference_peer_name.c \ ../toxcore/events/conference_title.c \ + ../toxcore/events/events_alloc.c \ + ../toxcore/events/events_alloc.h \ ../toxcore/events/file_chunk_request.c \ ../toxcore/events/file_recv.c \ ../toxcore/events/file_recv_chunk.c \ @@ -35,9 +37,25 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \ ../toxcore/events/friend_status.c \ ../toxcore/events/friend_status_message.c \ ../toxcore/events/friend_typing.c \ - ../toxcore/events/events_alloc.c \ - ../toxcore/events/events_alloc.h \ ../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 \ @@ -72,6 +90,8 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \ ../toxcore/tox.c \ ../toxcore/tox_dispatch.h \ ../toxcore/tox_dispatch.c \ + ../toxcore/tox_event.h \ + ../toxcore/tox_event.c \ ../toxcore/tox_events.h \ ../toxcore/tox_events.c \ ../toxcore/tox_unpack.h \ diff --git a/toxcore/events/conference_connected.c b/toxcore/events/conference_connected.c index fe51503f02..39169abbc1 100644 --- a/toxcore/events/conference_connected.c +++ b/toxcore/events/conference_connected.c @@ -1,15 +1,15 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" @@ -26,34 +26,32 @@ struct Tox_Event_Conference_Connected { }; non_null() -static void tox_event_conference_connected_construct(Tox_Event_Conference_Connected *conference_connected) +static void tox_event_conference_connected_set_conference_number(Tox_Event_Conference_Connected *conference_connected, + uint32_t conference_number) { - *conference_connected = (Tox_Event_Conference_Connected) { - 0 - }; + assert(conference_connected != nullptr); + conference_connected->conference_number = conference_number; } -non_null() -static void tox_event_conference_connected_destruct(Tox_Event_Conference_Connected *conference_connected) +uint32_t tox_event_conference_connected_get_conference_number(const Tox_Event_Conference_Connected *conference_connected) { - return; + assert(conference_connected != nullptr); + return conference_connected->conference_number; } non_null() -static void tox_event_conference_connected_set_conference_number( - Tox_Event_Conference_Connected *conference_connected, uint32_t conference_number) +static void tox_event_conference_connected_construct(Tox_Event_Conference_Connected *conference_connected) { - assert(conference_connected != nullptr); - conference_connected->conference_number = conference_number; + *conference_connected = (Tox_Event_Conference_Connected) { + 0 + }; } -uint32_t tox_event_conference_connected_get_conference_number( - const Tox_Event_Conference_Connected *conference_connected) +non_null() +static void tox_event_conference_connected_destruct(Tox_Event_Conference_Connected *conference_connected, const Memory *mem) { - assert(conference_connected != nullptr); - return conference_connected->conference_number; + return; } -non_null() -static bool tox_event_conference_connected_pack( +bool tox_event_conference_connected_pack( const Tox_Event_Conference_Connected *event, Bin_Pack *bp) { assert(event != nullptr); @@ -63,7 +61,7 @@ static bool tox_event_conference_connected_pack( } non_null() -static bool tox_event_conference_connected_unpack( +static bool tox_event_conference_connected_unpack_into( Tox_Event_Conference_Connected *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -73,93 +71,85 @@ static bool tox_event_conference_connected_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Conference_Connected *tox_events_add_conference_connected(Tox_Events *events) +const Tox_Event_Conference_Connected *tox_event_get_conference_connected(const Tox_Event *event) { - if (events->conference_connected_size == UINT32_MAX) { - return nullptr; - } - - if (events->conference_connected_size == events->conference_connected_capacity) { - const uint32_t new_conference_connected_capacity = events->conference_connected_capacity * 2 + 1; - Tox_Event_Conference_Connected *new_conference_connected = (Tox_Event_Conference_Connected *)realloc( - events->conference_connected, new_conference_connected_capacity * sizeof(Tox_Event_Conference_Connected)); + return event->type == TOX_EVENT_CONFERENCE_CONNECTED ? event->data.conference_connected : nullptr; +} - if (new_conference_connected == nullptr) { - return nullptr; - } +Tox_Event_Conference_Connected *tox_event_conference_connected_new(const Memory *mem) +{ + Tox_Event_Conference_Connected *const conference_connected = + (Tox_Event_Conference_Connected *)mem_alloc(mem, sizeof(Tox_Event_Conference_Connected)); - events->conference_connected = new_conference_connected; - events->conference_connected_capacity = new_conference_connected_capacity; + if (conference_connected == nullptr) { + return nullptr; } - Tox_Event_Conference_Connected *const conference_connected = - &events->conference_connected[events->conference_connected_size]; tox_event_conference_connected_construct(conference_connected); - ++events->conference_connected_size; return conference_connected; } -void tox_events_clear_conference_connected(Tox_Events *events) +void tox_event_conference_connected_free(Tox_Event_Conference_Connected *conference_connected, const Memory *mem) { - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->conference_connected_size; ++i) { - tox_event_conference_connected_destruct(&events->conference_connected[i]); + if (conference_connected != nullptr) { + tox_event_conference_connected_destruct(conference_connected, mem); } - - free(events->conference_connected); - events->conference_connected = nullptr; - events->conference_connected_size = 0; - events->conference_connected_capacity = 0; + mem_delete(mem, conference_connected); } -uint32_t tox_events_get_conference_connected_size(const Tox_Events *events) +non_null() +static Tox_Event_Conference_Connected *tox_events_add_conference_connected(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Conference_Connected *const conference_connected = tox_event_conference_connected_new(mem); + + if (conference_connected == nullptr) { + return nullptr; } - return events->conference_connected_size; -} + Tox_Event event; + event.type = TOX_EVENT_CONFERENCE_CONNECTED; + event.data.conference_connected = conference_connected; -const Tox_Event_Conference_Connected *tox_events_get_conference_connected(const Tox_Events *events, uint32_t index) -{ - assert(index < events->conference_connected_size); - assert(events->conference_connected != nullptr); - return &events->conference_connected[index]; + tox_events_add(events, &event); + return conference_connected; } -bool tox_events_pack_conference_connected(const Tox_Events *events, Bin_Pack *bp) +bool tox_event_conference_connected_unpack( + Tox_Event_Conference_Connected **event, Bin_Unpack *bu, const Memory *mem) { - const uint32_t size = tox_events_get_conference_connected_size(events); + assert(event != nullptr); + *event = tox_event_conference_connected_new(mem); - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_conference_connected_pack(tox_events_get_conference_connected(events, i), bp)) { - return false; - } + if (*event == nullptr) { + return false; } - return true; + + return tox_event_conference_connected_unpack_into(*event, bu); } -bool tox_events_unpack_conference_connected(Tox_Events *events, Bin_Unpack *bu) +non_null() +static Tox_Event_Conference_Connected *tox_event_conference_connected_alloc(void *user_data) { - Tox_Event_Conference_Connected *event = tox_events_add_conference_connected(events); + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); - if (event == nullptr) { - return false; + if (state->events == nullptr) { + return nullptr; } - return tox_event_conference_connected_unpack(event, bu); -} + Tox_Event_Conference_Connected *conference_connected = tox_events_add_conference_connected(state->events, state->mem); + if (conference_connected == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return conference_connected; +} /***************************************************** @@ -169,19 +159,12 @@ bool tox_events_unpack_conference_connected(Tox_Events *events, Bin_Unpack *bu) *****************************************************/ -void tox_events_handle_conference_connected(Tox *tox, uint32_t conference_number, void *user_data) +void tox_events_handle_conference_connected(Tox *tox, uint32_t conference_number, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Conference_Connected *conference_connected = tox_events_add_conference_connected(state->events); + Tox_Event_Conference_Connected *conference_connected = tox_event_conference_connected_alloc(user_data); if (conference_connected == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/conference_invite.c b/toxcore/events/conference_invite.c index a3a4c2380e..e48b0619a4 100644 --- a/toxcore/events/conference_invite.c +++ b/toxcore/events/conference_invite.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,6 +11,7 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" @@ -30,19 +31,6 @@ struct Tox_Event_Conference_Invite { uint32_t cookie_length; }; -non_null() -static void tox_event_conference_invite_construct(Tox_Event_Conference_Invite *conference_invite) -{ - *conference_invite = (Tox_Event_Conference_Invite) { - 0 - }; -} -non_null() -static void tox_event_conference_invite_destruct(Tox_Event_Conference_Invite *conference_invite) -{ - free(conference_invite->cookie); -} - non_null() static void tox_event_conference_invite_set_friend_number(Tox_Event_Conference_Invite *conference_invite, uint32_t friend_number) @@ -104,7 +92,19 @@ const uint8_t *tox_event_conference_invite_get_cookie(const Tox_Event_Conference } non_null() -static bool tox_event_conference_invite_pack( +static void tox_event_conference_invite_construct(Tox_Event_Conference_Invite *conference_invite) +{ + *conference_invite = (Tox_Event_Conference_Invite) { + 0 + }; +} +non_null() +static void tox_event_conference_invite_destruct(Tox_Event_Conference_Invite *conference_invite, const Memory *mem) +{ + free(conference_invite->cookie); +} + +bool tox_event_conference_invite_pack( const Tox_Event_Conference_Invite *event, Bin_Pack *bp) { assert(event != nullptr); @@ -117,7 +117,7 @@ static bool tox_event_conference_invite_pack( } non_null() -static bool tox_event_conference_invite_unpack( +static bool tox_event_conference_invite_unpack_into( Tox_Event_Conference_Invite *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -133,90 +133,84 @@ static bool tox_event_conference_invite_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Conference_Invite *tox_events_add_conference_invite(Tox_Events *events) +const Tox_Event_Conference_Invite *tox_event_get_conference_invite(const Tox_Event *event) { - if (events->conference_invite_size == UINT32_MAX) { - return nullptr; - } - - if (events->conference_invite_size == events->conference_invite_capacity) { - const uint32_t new_conference_invite_capacity = events->conference_invite_capacity * 2 + 1; - Tox_Event_Conference_Invite *new_conference_invite = (Tox_Event_Conference_Invite *)realloc( - events->conference_invite, new_conference_invite_capacity * sizeof(Tox_Event_Conference_Invite)); + return event->type == TOX_EVENT_CONFERENCE_INVITE ? event->data.conference_invite : nullptr; +} - if (new_conference_invite == nullptr) { - return nullptr; - } +Tox_Event_Conference_Invite *tox_event_conference_invite_new(const Memory *mem) +{ + Tox_Event_Conference_Invite *const conference_invite = + (Tox_Event_Conference_Invite *)mem_alloc(mem, sizeof(Tox_Event_Conference_Invite)); - events->conference_invite = new_conference_invite; - events->conference_invite_capacity = new_conference_invite_capacity; + if (conference_invite == nullptr) { + return nullptr; } - Tox_Event_Conference_Invite *const conference_invite = &events->conference_invite[events->conference_invite_size]; tox_event_conference_invite_construct(conference_invite); - ++events->conference_invite_size; return conference_invite; } -void tox_events_clear_conference_invite(Tox_Events *events) +void tox_event_conference_invite_free(Tox_Event_Conference_Invite *conference_invite, const Memory *mem) { - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->conference_invite_size; ++i) { - tox_event_conference_invite_destruct(&events->conference_invite[i]); + if (conference_invite != nullptr) { + tox_event_conference_invite_destruct(conference_invite, mem); } - - free(events->conference_invite); - events->conference_invite = nullptr; - events->conference_invite_size = 0; - events->conference_invite_capacity = 0; + mem_delete(mem, conference_invite); } -uint32_t tox_events_get_conference_invite_size(const Tox_Events *events) +non_null() +static Tox_Event_Conference_Invite *tox_events_add_conference_invite(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Conference_Invite *const conference_invite = tox_event_conference_invite_new(mem); + + if (conference_invite == nullptr) { + return nullptr; } - return events->conference_invite_size; -} + Tox_Event event; + event.type = TOX_EVENT_CONFERENCE_INVITE; + event.data.conference_invite = conference_invite; -const Tox_Event_Conference_Invite *tox_events_get_conference_invite(const Tox_Events *events, uint32_t index) -{ - assert(index < events->conference_invite_size); - assert(events->conference_invite != nullptr); - return &events->conference_invite[index]; + tox_events_add(events, &event); + return conference_invite; } -bool tox_events_pack_conference_invite(const Tox_Events *events, Bin_Pack *bp) +bool tox_event_conference_invite_unpack( + Tox_Event_Conference_Invite **event, Bin_Unpack *bu, const Memory *mem) { - const uint32_t size = tox_events_get_conference_invite_size(events); + assert(event != nullptr); + *event = tox_event_conference_invite_new(mem); - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_conference_invite_pack(tox_events_get_conference_invite(events, i), bp)) { - return false; - } + if (*event == nullptr) { + return false; } - return true; + + return tox_event_conference_invite_unpack_into(*event, bu); } -bool tox_events_unpack_conference_invite(Tox_Events *events, Bin_Unpack *bu) +non_null() +static Tox_Event_Conference_Invite *tox_event_conference_invite_alloc(void *user_data) { - Tox_Event_Conference_Invite *event = tox_events_add_conference_invite(events); + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); - if (event == nullptr) { - return false; + if (state->events == nullptr) { + return nullptr; } - return tox_event_conference_invite_unpack(event, bu); + Tox_Event_Conference_Invite *conference_invite = tox_events_add_conference_invite(state->events, state->mem); + + if (conference_invite == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return conference_invite; } @@ -227,20 +221,12 @@ bool tox_events_unpack_conference_invite(Tox_Events *events, Bin_Unpack *bu) *****************************************************/ -void tox_events_handle_conference_invite(Tox *tox, uint32_t friend_number, Tox_Conference_Type type, - const uint8_t *cookie, size_t length, void *user_data) +void tox_events_handle_conference_invite(Tox *tox, uint32_t friend_number, Tox_Conference_Type type, const uint8_t *cookie, size_t length, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Conference_Invite *conference_invite = tox_events_add_conference_invite(state->events); + Tox_Event_Conference_Invite *conference_invite = tox_event_conference_invite_alloc(user_data); if (conference_invite == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/conference_message.c b/toxcore/events/conference_message.c index 6910086abc..4939af7fff 100644 --- a/toxcore/events/conference_message.c +++ b/toxcore/events/conference_message.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,6 +11,7 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" @@ -31,19 +32,6 @@ struct Tox_Event_Conference_Message { uint32_t message_length; }; -non_null() -static void tox_event_conference_message_construct(Tox_Event_Conference_Message *conference_message) -{ - *conference_message = (Tox_Event_Conference_Message) { - 0 - }; -} -non_null() -static void tox_event_conference_message_destruct(Tox_Event_Conference_Message *conference_message) -{ - free(conference_message->message); -} - non_null() static void tox_event_conference_message_set_conference_number(Tox_Event_Conference_Message *conference_message, uint32_t conference_number) @@ -118,7 +106,19 @@ const uint8_t *tox_event_conference_message_get_message(const Tox_Event_Conferen } non_null() -static bool tox_event_conference_message_pack( +static void tox_event_conference_message_construct(Tox_Event_Conference_Message *conference_message) +{ + *conference_message = (Tox_Event_Conference_Message) { + 0 + }; +} +non_null() +static void tox_event_conference_message_destruct(Tox_Event_Conference_Message *conference_message, const Memory *mem) +{ + free(conference_message->message); +} + +bool tox_event_conference_message_pack( const Tox_Event_Conference_Message *event, Bin_Pack *bp) { assert(event != nullptr); @@ -132,7 +132,7 @@ static bool tox_event_conference_message_pack( } non_null() -static bool tox_event_conference_message_unpack( +static bool tox_event_conference_message_unpack_into( Tox_Event_Conference_Message *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -149,90 +149,84 @@ static bool tox_event_conference_message_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Conference_Message *tox_events_add_conference_message(Tox_Events *events) +const Tox_Event_Conference_Message *tox_event_get_conference_message(const Tox_Event *event) { - if (events->conference_message_size == UINT32_MAX) { - return nullptr; - } - - if (events->conference_message_size == events->conference_message_capacity) { - const uint32_t new_conference_message_capacity = events->conference_message_capacity * 2 + 1; - Tox_Event_Conference_Message *new_conference_message = (Tox_Event_Conference_Message *)realloc( - events->conference_message, new_conference_message_capacity * sizeof(Tox_Event_Conference_Message)); + return event->type == TOX_EVENT_CONFERENCE_MESSAGE ? event->data.conference_message : nullptr; +} - if (new_conference_message == nullptr) { - return nullptr; - } +Tox_Event_Conference_Message *tox_event_conference_message_new(const Memory *mem) +{ + Tox_Event_Conference_Message *const conference_message = + (Tox_Event_Conference_Message *)mem_alloc(mem, sizeof(Tox_Event_Conference_Message)); - events->conference_message = new_conference_message; - events->conference_message_capacity = new_conference_message_capacity; + if (conference_message == nullptr) { + return nullptr; } - Tox_Event_Conference_Message *const conference_message = &events->conference_message[events->conference_message_size]; tox_event_conference_message_construct(conference_message); - ++events->conference_message_size; return conference_message; } -void tox_events_clear_conference_message(Tox_Events *events) +void tox_event_conference_message_free(Tox_Event_Conference_Message *conference_message, const Memory *mem) { - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->conference_message_size; ++i) { - tox_event_conference_message_destruct(&events->conference_message[i]); + if (conference_message != nullptr) { + tox_event_conference_message_destruct(conference_message, mem); } - - free(events->conference_message); - events->conference_message = nullptr; - events->conference_message_size = 0; - events->conference_message_capacity = 0; + mem_delete(mem, conference_message); } -uint32_t tox_events_get_conference_message_size(const Tox_Events *events) +non_null() +static Tox_Event_Conference_Message *tox_events_add_conference_message(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Conference_Message *const conference_message = tox_event_conference_message_new(mem); + + if (conference_message == nullptr) { + return nullptr; } - return events->conference_message_size; -} + Tox_Event event; + event.type = TOX_EVENT_CONFERENCE_MESSAGE; + event.data.conference_message = conference_message; -const Tox_Event_Conference_Message *tox_events_get_conference_message(const Tox_Events *events, uint32_t index) -{ - assert(index < events->conference_message_size); - assert(events->conference_message != nullptr); - return &events->conference_message[index]; + tox_events_add(events, &event); + return conference_message; } -bool tox_events_pack_conference_message(const Tox_Events *events, Bin_Pack *bp) +bool tox_event_conference_message_unpack( + Tox_Event_Conference_Message **event, Bin_Unpack *bu, const Memory *mem) { - const uint32_t size = tox_events_get_conference_message_size(events); + assert(event != nullptr); + *event = tox_event_conference_message_new(mem); - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_conference_message_pack(tox_events_get_conference_message(events, i), bp)) { - return false; - } + if (*event == nullptr) { + return false; } - return true; + + return tox_event_conference_message_unpack_into(*event, bu); } -bool tox_events_unpack_conference_message(Tox_Events *events, Bin_Unpack *bu) +non_null() +static Tox_Event_Conference_Message *tox_event_conference_message_alloc(void *user_data) { - Tox_Event_Conference_Message *event = tox_events_add_conference_message(events); + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); - if (event == nullptr) { - return false; + if (state->events == nullptr) { + return nullptr; } - return tox_event_conference_message_unpack(event, bu); + Tox_Event_Conference_Message *conference_message = tox_events_add_conference_message(state->events, state->mem); + + if (conference_message == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return conference_message; } @@ -243,20 +237,12 @@ bool tox_events_unpack_conference_message(Tox_Events *events, Bin_Unpack *bu) *****************************************************/ -void tox_events_handle_conference_message(Tox *tox, uint32_t conference_number, uint32_t peer_number, - Tox_Message_Type type, const uint8_t *message, size_t length, void *user_data) +void tox_events_handle_conference_message(Tox *tox, uint32_t conference_number, uint32_t peer_number, Tox_Message_Type type, const uint8_t *message, size_t length, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Conference_Message *conference_message = tox_events_add_conference_message(state->events); + Tox_Event_Conference_Message *conference_message = tox_event_conference_message_alloc(user_data); if (conference_message == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/conference_peer_list_changed.c b/toxcore/events/conference_peer_list_changed.c index 9360f89dbf..9c9340c6f7 100644 --- a/toxcore/events/conference_peer_list_changed.c +++ b/toxcore/events/conference_peer_list_changed.c @@ -1,15 +1,15 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" @@ -26,36 +26,32 @@ struct Tox_Event_Conference_Peer_List_Changed { }; non_null() -static void tox_event_conference_peer_list_changed_construct(Tox_Event_Conference_Peer_List_Changed - *conference_peer_list_changed) +static void tox_event_conference_peer_list_changed_set_conference_number(Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed, + uint32_t conference_number) { - *conference_peer_list_changed = (Tox_Event_Conference_Peer_List_Changed) { - 0 - }; + assert(conference_peer_list_changed != nullptr); + conference_peer_list_changed->conference_number = conference_number; } -non_null() -static void tox_event_conference_peer_list_changed_destruct(Tox_Event_Conference_Peer_List_Changed - *conference_peer_list_changed) +uint32_t tox_event_conference_peer_list_changed_get_conference_number(const Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed) { - return; + assert(conference_peer_list_changed != nullptr); + return conference_peer_list_changed->conference_number; } non_null() -static void tox_event_conference_peer_list_changed_set_conference_number(Tox_Event_Conference_Peer_List_Changed - *conference_peer_list_changed, uint32_t conference_number) +static void tox_event_conference_peer_list_changed_construct(Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed) { - assert(conference_peer_list_changed != nullptr); - conference_peer_list_changed->conference_number = conference_number; + *conference_peer_list_changed = (Tox_Event_Conference_Peer_List_Changed) { + 0 + }; } -uint32_t tox_event_conference_peer_list_changed_get_conference_number(const Tox_Event_Conference_Peer_List_Changed - *conference_peer_list_changed) +non_null() +static void tox_event_conference_peer_list_changed_destruct(Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed, const Memory *mem) { - assert(conference_peer_list_changed != nullptr); - return conference_peer_list_changed->conference_number; + return; } -non_null() -static bool tox_event_conference_peer_list_changed_pack( +bool tox_event_conference_peer_list_changed_pack( const Tox_Event_Conference_Peer_List_Changed *event, Bin_Pack *bp) { assert(event != nullptr); @@ -65,7 +61,7 @@ static bool tox_event_conference_peer_list_changed_pack( } non_null() -static bool tox_event_conference_peer_list_changed_unpack( +static bool tox_event_conference_peer_list_changed_unpack_into( Tox_Event_Conference_Peer_List_Changed *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -75,94 +71,84 @@ static bool tox_event_conference_peer_list_changed_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Conference_Peer_List_Changed *tox_events_add_conference_peer_list_changed(Tox_Events *events) +const Tox_Event_Conference_Peer_List_Changed *tox_event_get_conference_peer_list_changed(const Tox_Event *event) { - if (events->conference_peer_list_changed_size == UINT32_MAX) { - return nullptr; - } - - if (events->conference_peer_list_changed_size == events->conference_peer_list_changed_capacity) { - const uint32_t new_conference_peer_list_changed_capacity = events->conference_peer_list_changed_capacity * 2 + 1; - Tox_Event_Conference_Peer_List_Changed *new_conference_peer_list_changed = (Tox_Event_Conference_Peer_List_Changed *) - realloc( - events->conference_peer_list_changed, - new_conference_peer_list_changed_capacity * sizeof(Tox_Event_Conference_Peer_List_Changed)); + return event->type == TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED ? event->data.conference_peer_list_changed : nullptr; +} - if (new_conference_peer_list_changed == nullptr) { - return nullptr; - } +Tox_Event_Conference_Peer_List_Changed *tox_event_conference_peer_list_changed_new(const Memory *mem) +{ + Tox_Event_Conference_Peer_List_Changed *const conference_peer_list_changed = + (Tox_Event_Conference_Peer_List_Changed *)mem_alloc(mem, sizeof(Tox_Event_Conference_Peer_List_Changed)); - events->conference_peer_list_changed = new_conference_peer_list_changed; - events->conference_peer_list_changed_capacity = new_conference_peer_list_changed_capacity; + if (conference_peer_list_changed == nullptr) { + return nullptr; } - Tox_Event_Conference_Peer_List_Changed *const conference_peer_list_changed = - &events->conference_peer_list_changed[events->conference_peer_list_changed_size]; tox_event_conference_peer_list_changed_construct(conference_peer_list_changed); - ++events->conference_peer_list_changed_size; return conference_peer_list_changed; } -void tox_events_clear_conference_peer_list_changed(Tox_Events *events) +void tox_event_conference_peer_list_changed_free(Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed, const Memory *mem) { - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->conference_peer_list_changed_size; ++i) { - tox_event_conference_peer_list_changed_destruct(&events->conference_peer_list_changed[i]); + if (conference_peer_list_changed != nullptr) { + tox_event_conference_peer_list_changed_destruct(conference_peer_list_changed, mem); } - - free(events->conference_peer_list_changed); - events->conference_peer_list_changed = nullptr; - events->conference_peer_list_changed_size = 0; - events->conference_peer_list_changed_capacity = 0; + mem_delete(mem, conference_peer_list_changed); } -uint32_t tox_events_get_conference_peer_list_changed_size(const Tox_Events *events) +non_null() +static Tox_Event_Conference_Peer_List_Changed *tox_events_add_conference_peer_list_changed(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Conference_Peer_List_Changed *const conference_peer_list_changed = tox_event_conference_peer_list_changed_new(mem); + + if (conference_peer_list_changed == nullptr) { + return nullptr; } - return events->conference_peer_list_changed_size; -} + Tox_Event event; + event.type = TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED; + event.data.conference_peer_list_changed = conference_peer_list_changed; -const Tox_Event_Conference_Peer_List_Changed *tox_events_get_conference_peer_list_changed(const Tox_Events *events, - uint32_t index) -{ - assert(index < events->conference_peer_list_changed_size); - assert(events->conference_peer_list_changed != nullptr); - return &events->conference_peer_list_changed[index]; + tox_events_add(events, &event); + return conference_peer_list_changed; } -bool tox_events_pack_conference_peer_list_changed(const Tox_Events *events, Bin_Pack *bp) +bool tox_event_conference_peer_list_changed_unpack( + Tox_Event_Conference_Peer_List_Changed **event, Bin_Unpack *bu, const Memory *mem) { - const uint32_t size = tox_events_get_conference_peer_list_changed_size(events); + assert(event != nullptr); + *event = tox_event_conference_peer_list_changed_new(mem); - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_conference_peer_list_changed_pack(tox_events_get_conference_peer_list_changed(events, i), bp)) { - return false; - } + if (*event == nullptr) { + return false; } - return true; + + return tox_event_conference_peer_list_changed_unpack_into(*event, bu); } -bool tox_events_unpack_conference_peer_list_changed(Tox_Events *events, Bin_Unpack *bu) +non_null() +static Tox_Event_Conference_Peer_List_Changed *tox_event_conference_peer_list_changed_alloc(void *user_data) { - Tox_Event_Conference_Peer_List_Changed *event = tox_events_add_conference_peer_list_changed(events); + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); - if (event == nullptr) { - return false; + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed = tox_events_add_conference_peer_list_changed(state->events, state->mem); + + if (conference_peer_list_changed == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; } - return tox_event_conference_peer_list_changed_unpack(event, bu); + return conference_peer_list_changed; } @@ -173,20 +159,12 @@ bool tox_events_unpack_conference_peer_list_changed(Tox_Events *events, Bin_Unpa *****************************************************/ -void tox_events_handle_conference_peer_list_changed(Tox *tox, uint32_t conference_number, void *user_data) +void tox_events_handle_conference_peer_list_changed(Tox *tox, uint32_t conference_number, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed = tox_events_add_conference_peer_list_changed( - state->events); + Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed = tox_event_conference_peer_list_changed_alloc(user_data); if (conference_peer_list_changed == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/conference_peer_name.c b/toxcore/events/conference_peer_name.c index 0859b99c5d..ad6b9e1877 100644 --- a/toxcore/events/conference_peer_name.c +++ b/toxcore/events/conference_peer_name.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,6 +11,7 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" @@ -29,19 +30,6 @@ struct Tox_Event_Conference_Peer_Name { uint32_t name_length; }; -non_null() -static void tox_event_conference_peer_name_construct(Tox_Event_Conference_Peer_Name *conference_peer_name) -{ - *conference_peer_name = (Tox_Event_Conference_Peer_Name) { - 0 - }; -} -non_null() -static void tox_event_conference_peer_name_destruct(Tox_Event_Conference_Peer_Name *conference_peer_name) -{ - free(conference_peer_name->name); -} - non_null() static void tox_event_conference_peer_name_set_conference_number(Tox_Event_Conference_Peer_Name *conference_peer_name, uint32_t conference_number) @@ -49,8 +37,7 @@ static void tox_event_conference_peer_name_set_conference_number(Tox_Event_Confe assert(conference_peer_name != nullptr); conference_peer_name->conference_number = conference_number; } -uint32_t tox_event_conference_peer_name_get_conference_number(const Tox_Event_Conference_Peer_Name - *conference_peer_name) +uint32_t tox_event_conference_peer_name_get_conference_number(const Tox_Event_Conference_Peer_Name *conference_peer_name) { assert(conference_peer_name != nullptr); return conference_peer_name->conference_number; @@ -104,7 +91,19 @@ const uint8_t *tox_event_conference_peer_name_get_name(const Tox_Event_Conferenc } non_null() -static bool tox_event_conference_peer_name_pack( +static void tox_event_conference_peer_name_construct(Tox_Event_Conference_Peer_Name *conference_peer_name) +{ + *conference_peer_name = (Tox_Event_Conference_Peer_Name) { + 0 + }; +} +non_null() +static void tox_event_conference_peer_name_destruct(Tox_Event_Conference_Peer_Name *conference_peer_name, const Memory *mem) +{ + free(conference_peer_name->name); +} + +bool tox_event_conference_peer_name_pack( const Tox_Event_Conference_Peer_Name *event, Bin_Pack *bp) { assert(event != nullptr); @@ -117,7 +116,7 @@ static bool tox_event_conference_peer_name_pack( } non_null() -static bool tox_event_conference_peer_name_unpack( +static bool tox_event_conference_peer_name_unpack_into( Tox_Event_Conference_Peer_Name *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -133,91 +132,84 @@ static bool tox_event_conference_peer_name_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Conference_Peer_Name *tox_events_add_conference_peer_name(Tox_Events *events) +const Tox_Event_Conference_Peer_Name *tox_event_get_conference_peer_name(const Tox_Event *event) { - if (events->conference_peer_name_size == UINT32_MAX) { - return nullptr; - } - - if (events->conference_peer_name_size == events->conference_peer_name_capacity) { - const uint32_t new_conference_peer_name_capacity = events->conference_peer_name_capacity * 2 + 1; - Tox_Event_Conference_Peer_Name *new_conference_peer_name = (Tox_Event_Conference_Peer_Name *)realloc( - events->conference_peer_name, new_conference_peer_name_capacity * sizeof(Tox_Event_Conference_Peer_Name)); + return event->type == TOX_EVENT_CONFERENCE_PEER_NAME ? event->data.conference_peer_name : nullptr; +} - if (new_conference_peer_name == nullptr) { - return nullptr; - } +Tox_Event_Conference_Peer_Name *tox_event_conference_peer_name_new(const Memory *mem) +{ + Tox_Event_Conference_Peer_Name *const conference_peer_name = + (Tox_Event_Conference_Peer_Name *)mem_alloc(mem, sizeof(Tox_Event_Conference_Peer_Name)); - events->conference_peer_name = new_conference_peer_name; - events->conference_peer_name_capacity = new_conference_peer_name_capacity; + if (conference_peer_name == nullptr) { + return nullptr; } - Tox_Event_Conference_Peer_Name *const conference_peer_name = - &events->conference_peer_name[events->conference_peer_name_size]; tox_event_conference_peer_name_construct(conference_peer_name); - ++events->conference_peer_name_size; return conference_peer_name; } -void tox_events_clear_conference_peer_name(Tox_Events *events) +void tox_event_conference_peer_name_free(Tox_Event_Conference_Peer_Name *conference_peer_name, const Memory *mem) { - if (events == nullptr) { - return; + if (conference_peer_name != nullptr) { + tox_event_conference_peer_name_destruct(conference_peer_name, mem); } - - for (uint32_t i = 0; i < events->conference_peer_name_size; ++i) { - tox_event_conference_peer_name_destruct(&events->conference_peer_name[i]); - } - - free(events->conference_peer_name); - events->conference_peer_name = nullptr; - events->conference_peer_name_size = 0; - events->conference_peer_name_capacity = 0; + mem_delete(mem, conference_peer_name); } -uint32_t tox_events_get_conference_peer_name_size(const Tox_Events *events) +non_null() +static Tox_Event_Conference_Peer_Name *tox_events_add_conference_peer_name(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Conference_Peer_Name *const conference_peer_name = tox_event_conference_peer_name_new(mem); + + if (conference_peer_name == nullptr) { + return nullptr; } - return events->conference_peer_name_size; -} + Tox_Event event; + event.type = TOX_EVENT_CONFERENCE_PEER_NAME; + event.data.conference_peer_name = conference_peer_name; -const Tox_Event_Conference_Peer_Name *tox_events_get_conference_peer_name(const Tox_Events *events, uint32_t index) -{ - assert(index < events->conference_peer_name_size); - assert(events->conference_peer_name != nullptr); - return &events->conference_peer_name[index]; + tox_events_add(events, &event); + return conference_peer_name; } -bool tox_events_pack_conference_peer_name(const Tox_Events *events, Bin_Pack *bp) +bool tox_event_conference_peer_name_unpack( + Tox_Event_Conference_Peer_Name **event, Bin_Unpack *bu, const Memory *mem) { - const uint32_t size = tox_events_get_conference_peer_name_size(events); + assert(event != nullptr); + *event = tox_event_conference_peer_name_new(mem); - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_conference_peer_name_pack(tox_events_get_conference_peer_name(events, i), bp)) { - return false; - } + if (*event == nullptr) { + return false; } - return true; + + return tox_event_conference_peer_name_unpack_into(*event, bu); } -bool tox_events_unpack_conference_peer_name(Tox_Events *events, Bin_Unpack *bu) +non_null() +static Tox_Event_Conference_Peer_Name *tox_event_conference_peer_name_alloc(void *user_data) { - Tox_Event_Conference_Peer_Name *event = tox_events_add_conference_peer_name(events); + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); - if (event == nullptr) { - return false; + if (state->events == nullptr) { + return nullptr; } - return tox_event_conference_peer_name_unpack(event, bu); + Tox_Event_Conference_Peer_Name *conference_peer_name = tox_events_add_conference_peer_name(state->events, state->mem); + + if (conference_peer_name == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return conference_peer_name; } @@ -228,20 +220,12 @@ bool tox_events_unpack_conference_peer_name(Tox_Events *events, Bin_Unpack *bu) *****************************************************/ -void tox_events_handle_conference_peer_name(Tox *tox, uint32_t conference_number, uint32_t peer_number, - const uint8_t *name, size_t length, void *user_data) +void tox_events_handle_conference_peer_name(Tox *tox, uint32_t conference_number, uint32_t peer_number, const uint8_t *name, size_t length, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Conference_Peer_Name *conference_peer_name = tox_events_add_conference_peer_name(state->events); + Tox_Event_Conference_Peer_Name *conference_peer_name = tox_event_conference_peer_name_alloc(user_data); if (conference_peer_name == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/conference_title.c b/toxcore/events/conference_title.c index 2043de132f..1532ba5de9 100644 --- a/toxcore/events/conference_title.c +++ b/toxcore/events/conference_title.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,6 +11,7 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" @@ -29,19 +30,6 @@ struct Tox_Event_Conference_Title { uint32_t title_length; }; -non_null() -static void tox_event_conference_title_construct(Tox_Event_Conference_Title *conference_title) -{ - *conference_title = (Tox_Event_Conference_Title) { - 0 - }; -} -non_null() -static void tox_event_conference_title_destruct(Tox_Event_Conference_Title *conference_title) -{ - free(conference_title->title); -} - non_null() static void tox_event_conference_title_set_conference_number(Tox_Event_Conference_Title *conference_title, uint32_t conference_number) @@ -69,8 +57,8 @@ uint32_t tox_event_conference_title_get_peer_number(const Tox_Event_Conference_T } non_null() -static bool tox_event_conference_title_set_title(Tox_Event_Conference_Title *conference_title, const uint8_t *title, - uint32_t title_length) +static bool tox_event_conference_title_set_title(Tox_Event_Conference_Title *conference_title, + const uint8_t *title, uint32_t title_length) { assert(conference_title != nullptr); @@ -103,7 +91,19 @@ const uint8_t *tox_event_conference_title_get_title(const Tox_Event_Conference_T } non_null() -static bool tox_event_conference_title_pack( +static void tox_event_conference_title_construct(Tox_Event_Conference_Title *conference_title) +{ + *conference_title = (Tox_Event_Conference_Title) { + 0 + }; +} +non_null() +static void tox_event_conference_title_destruct(Tox_Event_Conference_Title *conference_title, const Memory *mem) +{ + free(conference_title->title); +} + +bool tox_event_conference_title_pack( const Tox_Event_Conference_Title *event, Bin_Pack *bp) { assert(event != nullptr); @@ -116,7 +116,7 @@ static bool tox_event_conference_title_pack( } non_null() -static bool tox_event_conference_title_unpack( +static bool tox_event_conference_title_unpack_into( Tox_Event_Conference_Title *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -132,90 +132,84 @@ static bool tox_event_conference_title_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Conference_Title *tox_events_add_conference_title(Tox_Events *events) +const Tox_Event_Conference_Title *tox_event_get_conference_title(const Tox_Event *event) { - if (events->conference_title_size == UINT32_MAX) { - return nullptr; - } - - if (events->conference_title_size == events->conference_title_capacity) { - const uint32_t new_conference_title_capacity = events->conference_title_capacity * 2 + 1; - Tox_Event_Conference_Title *new_conference_title = (Tox_Event_Conference_Title *)realloc( - events->conference_title, new_conference_title_capacity * sizeof(Tox_Event_Conference_Title)); + return event->type == TOX_EVENT_CONFERENCE_TITLE ? event->data.conference_title : nullptr; +} - if (new_conference_title == nullptr) { - return nullptr; - } +Tox_Event_Conference_Title *tox_event_conference_title_new(const Memory *mem) +{ + Tox_Event_Conference_Title *const conference_title = + (Tox_Event_Conference_Title *)mem_alloc(mem, sizeof(Tox_Event_Conference_Title)); - events->conference_title = new_conference_title; - events->conference_title_capacity = new_conference_title_capacity; + if (conference_title == nullptr) { + return nullptr; } - Tox_Event_Conference_Title *const conference_title = &events->conference_title[events->conference_title_size]; tox_event_conference_title_construct(conference_title); - ++events->conference_title_size; return conference_title; } -void tox_events_clear_conference_title(Tox_Events *events) +void tox_event_conference_title_free(Tox_Event_Conference_Title *conference_title, const Memory *mem) { - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->conference_title_size; ++i) { - tox_event_conference_title_destruct(&events->conference_title[i]); + if (conference_title != nullptr) { + tox_event_conference_title_destruct(conference_title, mem); } - - free(events->conference_title); - events->conference_title = nullptr; - events->conference_title_size = 0; - events->conference_title_capacity = 0; + mem_delete(mem, conference_title); } -uint32_t tox_events_get_conference_title_size(const Tox_Events *events) +non_null() +static Tox_Event_Conference_Title *tox_events_add_conference_title(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Conference_Title *const conference_title = tox_event_conference_title_new(mem); + + if (conference_title == nullptr) { + return nullptr; } - return events->conference_title_size; -} + Tox_Event event; + event.type = TOX_EVENT_CONFERENCE_TITLE; + event.data.conference_title = conference_title; -const Tox_Event_Conference_Title *tox_events_get_conference_title(const Tox_Events *events, uint32_t index) -{ - assert(index < events->conference_title_size); - assert(events->conference_title != nullptr); - return &events->conference_title[index]; + tox_events_add(events, &event); + return conference_title; } -bool tox_events_pack_conference_title(const Tox_Events *events, Bin_Pack *bp) +bool tox_event_conference_title_unpack( + Tox_Event_Conference_Title **event, Bin_Unpack *bu, const Memory *mem) { - const uint32_t size = tox_events_get_conference_title_size(events); + assert(event != nullptr); + *event = tox_event_conference_title_new(mem); - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_conference_title_pack(tox_events_get_conference_title(events, i), bp)) { - return false; - } + if (*event == nullptr) { + return false; } - return true; + + return tox_event_conference_title_unpack_into(*event, bu); } -bool tox_events_unpack_conference_title(Tox_Events *events, Bin_Unpack *bu) +non_null() +static Tox_Event_Conference_Title *tox_event_conference_title_alloc(void *user_data) { - Tox_Event_Conference_Title *event = tox_events_add_conference_title(events); + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); - if (event == nullptr) { - return false; + if (state->events == nullptr) { + return nullptr; } - return tox_event_conference_title_unpack(event, bu); + Tox_Event_Conference_Title *conference_title = tox_events_add_conference_title(state->events, state->mem); + + if (conference_title == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return conference_title; } @@ -226,20 +220,12 @@ bool tox_events_unpack_conference_title(Tox_Events *events, Bin_Unpack *bu) *****************************************************/ -void tox_events_handle_conference_title(Tox *tox, uint32_t conference_number, uint32_t peer_number, - const uint8_t *title, size_t length, void *user_data) +void tox_events_handle_conference_title(Tox *tox, uint32_t conference_number, uint32_t peer_number, const uint8_t *title, size_t length, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Conference_Title *conference_title = tox_events_add_conference_title(state->events); + Tox_Event_Conference_Title *conference_title = tox_event_conference_title_alloc(user_data); if (conference_title == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/events_alloc.c b/toxcore/events/events_alloc.c index 2fc5d363ab..271dfc0043 100644 --- a/toxcore/events/events_alloc.c +++ b/toxcore/events/events_alloc.c @@ -5,22 +5,24 @@ #include "events_alloc.h" #include -#include #include "../ccompat.h" +#include "../mem.h" +#include "../tox_event.h" #include "../tox_events.h" Tox_Events_State *tox_events_alloc(void *user_data) { Tox_Events_State *state = (Tox_Events_State *)user_data; assert(state != nullptr); + assert(state->mem != nullptr); if (state->events != nullptr) { // Already allocated. return state; } - Tox_Events *events = (Tox_Events *)calloc(1, sizeof(Tox_Events)); + Tox_Events *events = (Tox_Events *)mem_alloc(state->mem, sizeof(Tox_Events)); if (events == nullptr) { // It's still null => allocation failed. @@ -33,6 +35,7 @@ Tox_Events_State *tox_events_alloc(void *user_data) nullptr }; state->events = events; + state->events->mem = state->mem; return state; } @@ -43,26 +46,35 @@ void tox_events_free(Tox_Events *events) return; } - tox_events_clear_conference_connected(events); - tox_events_clear_conference_invite(events); - tox_events_clear_conference_message(events); - tox_events_clear_conference_peer_list_changed(events); - tox_events_clear_conference_peer_name(events); - tox_events_clear_conference_title(events); - tox_events_clear_file_chunk_request(events); - tox_events_clear_file_recv_chunk(events); - tox_events_clear_file_recv_control(events); - tox_events_clear_file_recv(events); - tox_events_clear_friend_connection_status(events); - tox_events_clear_friend_lossless_packet(events); - tox_events_clear_friend_lossy_packet(events); - tox_events_clear_friend_message(events); - tox_events_clear_friend_name(events); - tox_events_clear_friend_read_receipt(events); - tox_events_clear_friend_request(events); - tox_events_clear_friend_status(events); - tox_events_clear_friend_status_message(events); - tox_events_clear_friend_typing(events); - tox_events_clear_self_connection_status(events); - free(events); + for (uint32_t i = 0; i < events->events_size; ++i) { + tox_event_destruct(&events->events[i], events->mem); + } + + mem_delete(events->mem, events->events); + mem_delete(events->mem, events); +} + +bool tox_events_add(Tox_Events *events, const Tox_Event *event) +{ + if (events->events_size == UINT32_MAX) { + return false; + } + + if (events->events_size == events->events_capacity) { + const uint32_t new_events_capacity = events->events_capacity * 2 + 1; + Tox_Event *new_events = (Tox_Event *)mem_vrealloc( + events->mem, events->events, new_events_capacity, sizeof(Tox_Event)); + + if (new_events == nullptr) { + return false; + } + + events->events = new_events; + events->events_capacity = new_events_capacity; + } + + events->events[events->events_size] = *event; + ++events->events_size; + + return true; } diff --git a/toxcore/events/events_alloc.h b/toxcore/events/events_alloc.h index e59bfcea3a..11ac6c99bc 100644 --- a/toxcore/events/events_alloc.h +++ b/toxcore/events/events_alloc.h @@ -8,100 +8,23 @@ #include "../attributes.h" #include "../bin_pack.h" #include "../bin_unpack.h" -#include "../tox_events.h" +#include "../tox_event.h" #ifdef __cplusplus extern "C" { #endif struct Tox_Events { - Tox_Event_Conference_Connected *conference_connected; - uint32_t conference_connected_size; - uint32_t conference_connected_capacity; + Tox_Event *events; + uint32_t events_size; + uint32_t events_capacity; - Tox_Event_Conference_Invite *conference_invite; - uint32_t conference_invite_size; - uint32_t conference_invite_capacity; - - Tox_Event_Conference_Message *conference_message; - uint32_t conference_message_size; - uint32_t conference_message_capacity; - - Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed; - uint32_t conference_peer_list_changed_size; - uint32_t conference_peer_list_changed_capacity; - - Tox_Event_Conference_Peer_Name *conference_peer_name; - uint32_t conference_peer_name_size; - uint32_t conference_peer_name_capacity; - - Tox_Event_Conference_Title *conference_title; - uint32_t conference_title_size; - uint32_t conference_title_capacity; - - Tox_Event_File_Chunk_Request *file_chunk_request; - uint32_t file_chunk_request_size; - uint32_t file_chunk_request_capacity; - - Tox_Event_File_Recv *file_recv; - uint32_t file_recv_size; - uint32_t file_recv_capacity; - - Tox_Event_File_Recv_Chunk *file_recv_chunk; - uint32_t file_recv_chunk_size; - uint32_t file_recv_chunk_capacity; - - Tox_Event_File_Recv_Control *file_recv_control; - uint32_t file_recv_control_size; - uint32_t file_recv_control_capacity; - - Tox_Event_Friend_Connection_Status *friend_connection_status; - uint32_t friend_connection_status_size; - uint32_t friend_connection_status_capacity; - - Tox_Event_Friend_Lossless_Packet *friend_lossless_packet; - uint32_t friend_lossless_packet_size; - uint32_t friend_lossless_packet_capacity; - - Tox_Event_Friend_Lossy_Packet *friend_lossy_packet; - uint32_t friend_lossy_packet_size; - uint32_t friend_lossy_packet_capacity; - - Tox_Event_Friend_Message *friend_message; - uint32_t friend_message_size; - uint32_t friend_message_capacity; - - Tox_Event_Friend_Name *friend_name; - uint32_t friend_name_size; - uint32_t friend_name_capacity; - - Tox_Event_Friend_Read_Receipt *friend_read_receipt; - uint32_t friend_read_receipt_size; - uint32_t friend_read_receipt_capacity; - - Tox_Event_Friend_Request *friend_request; - uint32_t friend_request_size; - uint32_t friend_request_capacity; - - Tox_Event_Friend_Status *friend_status; - uint32_t friend_status_size; - uint32_t friend_status_capacity; - - Tox_Event_Friend_Status_Message *friend_status_message; - uint32_t friend_status_message_size; - uint32_t friend_status_message_capacity; - - Tox_Event_Friend_Typing *friend_typing; - uint32_t friend_typing_size; - uint32_t friend_typing_capacity; - - Tox_Event_Self_Connection_Status *self_connection_status; - uint32_t self_connection_status_size; - uint32_t self_connection_status_capacity; + const Memory *mem; }; typedef struct Tox_Events_State { Tox_Err_Events_Iterate error; + const Memory *mem; Tox_Events *events; } Tox_Events_State; @@ -126,89 +49,37 @@ 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); -// non_null() -typedef void tox_events_clear_cb(Tox_Events *events); - -tox_events_clear_cb tox_events_clear_conference_connected; -tox_events_clear_cb tox_events_clear_conference_invite; -tox_events_clear_cb tox_events_clear_conference_message; -tox_events_clear_cb tox_events_clear_conference_peer_list_changed; -tox_events_clear_cb tox_events_clear_conference_peer_name; -tox_events_clear_cb tox_events_clear_conference_title; -tox_events_clear_cb tox_events_clear_file_chunk_request; -tox_events_clear_cb tox_events_clear_file_recv_chunk; -tox_events_clear_cb tox_events_clear_file_recv_control; -tox_events_clear_cb tox_events_clear_file_recv; -tox_events_clear_cb tox_events_clear_friend_connection_status; -tox_events_clear_cb tox_events_clear_friend_lossless_packet; -tox_events_clear_cb tox_events_clear_friend_lossy_packet; -tox_events_clear_cb tox_events_clear_friend_message; -tox_events_clear_cb tox_events_clear_friend_name; -tox_events_clear_cb tox_events_clear_friend_read_receipt; -tox_events_clear_cb tox_events_clear_friend_request; -tox_events_clear_cb tox_events_clear_friend_status_message; -tox_events_clear_cb tox_events_clear_friend_status; -tox_events_clear_cb tox_events_clear_friend_typing; -tox_events_clear_cb tox_events_clear_self_connection_status; - -// non_null() -typedef bool tox_events_pack_cb(const Tox_Events *events, Bin_Pack *bp); - -tox_events_pack_cb tox_events_pack_conference_connected; -tox_events_pack_cb tox_events_pack_conference_invite; -tox_events_pack_cb tox_events_pack_conference_message; -tox_events_pack_cb tox_events_pack_conference_peer_list_changed; -tox_events_pack_cb tox_events_pack_conference_peer_name; -tox_events_pack_cb tox_events_pack_conference_title; -tox_events_pack_cb tox_events_pack_file_chunk_request; -tox_events_pack_cb tox_events_pack_file_recv_chunk; -tox_events_pack_cb tox_events_pack_file_recv_control; -tox_events_pack_cb tox_events_pack_file_recv; -tox_events_pack_cb tox_events_pack_friend_connection_status; -tox_events_pack_cb tox_events_pack_friend_lossless_packet; -tox_events_pack_cb tox_events_pack_friend_lossy_packet; -tox_events_pack_cb tox_events_pack_friend_message; -tox_events_pack_cb tox_events_pack_friend_name; -tox_events_pack_cb tox_events_pack_friend_read_receipt; -tox_events_pack_cb tox_events_pack_friend_request; -tox_events_pack_cb tox_events_pack_friend_status_message; -tox_events_pack_cb tox_events_pack_friend_status; -tox_events_pack_cb tox_events_pack_friend_typing; -tox_events_pack_cb tox_events_pack_self_connection_status; - -tox_events_pack_cb tox_events_pack; - -// non_null() -typedef bool tox_events_unpack_cb(Tox_Events *events, Bin_Unpack *bu); - -tox_events_unpack_cb tox_events_unpack_conference_connected; -tox_events_unpack_cb tox_events_unpack_conference_invite; -tox_events_unpack_cb tox_events_unpack_conference_message; -tox_events_unpack_cb tox_events_unpack_conference_peer_list_changed; -tox_events_unpack_cb tox_events_unpack_conference_peer_name; -tox_events_unpack_cb tox_events_unpack_conference_title; -tox_events_unpack_cb tox_events_unpack_file_chunk_request; -tox_events_unpack_cb tox_events_unpack_file_recv_chunk; -tox_events_unpack_cb tox_events_unpack_file_recv_control; -tox_events_unpack_cb tox_events_unpack_file_recv; -tox_events_unpack_cb tox_events_unpack_friend_connection_status; -tox_events_unpack_cb tox_events_unpack_friend_lossless_packet; -tox_events_unpack_cb tox_events_unpack_friend_lossy_packet; -tox_events_unpack_cb tox_events_unpack_friend_message; -tox_events_unpack_cb tox_events_unpack_friend_name; -tox_events_unpack_cb tox_events_unpack_friend_read_receipt; -tox_events_unpack_cb tox_events_unpack_friend_request; -tox_events_unpack_cb tox_events_unpack_friend_status_message; -tox_events_unpack_cb tox_events_unpack_friend_status; -tox_events_unpack_cb tox_events_unpack_friend_typing; -tox_events_unpack_cb tox_events_unpack_self_connection_status; - -tox_events_unpack_cb tox_events_unpack; +non_null() +bool tox_events_unpack(Tox_Events *events, Bin_Unpack *bu, const Memory *mem); non_null() Tox_Events_State *tox_events_alloc(void *user_data); +non_null() +bool tox_events_add(Tox_Events *events, const Tox_Event *event); + #ifdef __cplusplus } #endif diff --git a/toxcore/events/file_chunk_request.c b/toxcore/events/file_chunk_request.c index 4f4deafc0d..0bf586424a 100644 --- a/toxcore/events/file_chunk_request.c +++ b/toxcore/events/file_chunk_request.c @@ -1,15 +1,15 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" @@ -28,19 +28,6 @@ struct Tox_Event_File_Chunk_Request { uint16_t length; }; -non_null() -static void tox_event_file_chunk_request_construct(Tox_Event_File_Chunk_Request *file_chunk_request) -{ - *file_chunk_request = (Tox_Event_File_Chunk_Request) { - 0 - }; -} -non_null() -static void tox_event_file_chunk_request_destruct(Tox_Event_File_Chunk_Request *file_chunk_request) -{ - return; -} - non_null() static void tox_event_file_chunk_request_set_friend_number(Tox_Event_File_Chunk_Request *file_chunk_request, uint32_t friend_number) @@ -81,7 +68,8 @@ uint64_t tox_event_file_chunk_request_get_position(const Tox_Event_File_Chunk_Re } non_null() -static void tox_event_file_chunk_request_set_length(Tox_Event_File_Chunk_Request *file_chunk_request, uint16_t length) +static void tox_event_file_chunk_request_set_length(Tox_Event_File_Chunk_Request *file_chunk_request, + uint16_t length) { assert(file_chunk_request != nullptr); file_chunk_request->length = length; @@ -93,7 +81,19 @@ uint16_t tox_event_file_chunk_request_get_length(const Tox_Event_File_Chunk_Requ } non_null() -static bool tox_event_file_chunk_request_pack( +static void tox_event_file_chunk_request_construct(Tox_Event_File_Chunk_Request *file_chunk_request) +{ + *file_chunk_request = (Tox_Event_File_Chunk_Request) { + 0 + }; +} +non_null() +static void tox_event_file_chunk_request_destruct(Tox_Event_File_Chunk_Request *file_chunk_request, const Memory *mem) +{ + return; +} + +bool tox_event_file_chunk_request_pack( const Tox_Event_File_Chunk_Request *event, Bin_Pack *bp) { assert(event != nullptr); @@ -107,7 +107,7 @@ static bool tox_event_file_chunk_request_pack( } non_null() -static bool tox_event_file_chunk_request_unpack( +static bool tox_event_file_chunk_request_unpack_into( Tox_Event_File_Chunk_Request *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -124,90 +124,84 @@ static bool tox_event_file_chunk_request_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_File_Chunk_Request *tox_events_add_file_chunk_request(Tox_Events *events) +const Tox_Event_File_Chunk_Request *tox_event_get_file_chunk_request(const Tox_Event *event) { - if (events->file_chunk_request_size == UINT32_MAX) { - return nullptr; - } - - if (events->file_chunk_request_size == events->file_chunk_request_capacity) { - const uint32_t new_file_chunk_request_capacity = events->file_chunk_request_capacity * 2 + 1; - Tox_Event_File_Chunk_Request *new_file_chunk_request = (Tox_Event_File_Chunk_Request *)realloc( - events->file_chunk_request, new_file_chunk_request_capacity * sizeof(Tox_Event_File_Chunk_Request)); + return event->type == TOX_EVENT_FILE_CHUNK_REQUEST ? event->data.file_chunk_request : nullptr; +} - if (new_file_chunk_request == nullptr) { - return nullptr; - } +Tox_Event_File_Chunk_Request *tox_event_file_chunk_request_new(const Memory *mem) +{ + Tox_Event_File_Chunk_Request *const file_chunk_request = + (Tox_Event_File_Chunk_Request *)mem_alloc(mem, sizeof(Tox_Event_File_Chunk_Request)); - events->file_chunk_request = new_file_chunk_request; - events->file_chunk_request_capacity = new_file_chunk_request_capacity; + if (file_chunk_request == nullptr) { + return nullptr; } - Tox_Event_File_Chunk_Request *const file_chunk_request = &events->file_chunk_request[events->file_chunk_request_size]; tox_event_file_chunk_request_construct(file_chunk_request); - ++events->file_chunk_request_size; return file_chunk_request; } -void tox_events_clear_file_chunk_request(Tox_Events *events) +void tox_event_file_chunk_request_free(Tox_Event_File_Chunk_Request *file_chunk_request, const Memory *mem) { - if (events == nullptr) { - return; + if (file_chunk_request != nullptr) { + tox_event_file_chunk_request_destruct(file_chunk_request, mem); } - - for (uint32_t i = 0; i < events->file_chunk_request_size; ++i) { - tox_event_file_chunk_request_destruct(&events->file_chunk_request[i]); - } - - free(events->file_chunk_request); - events->file_chunk_request = nullptr; - events->file_chunk_request_size = 0; - events->file_chunk_request_capacity = 0; + mem_delete(mem, file_chunk_request); } -uint32_t tox_events_get_file_chunk_request_size(const Tox_Events *events) +non_null() +static Tox_Event_File_Chunk_Request *tox_events_add_file_chunk_request(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_File_Chunk_Request *const file_chunk_request = tox_event_file_chunk_request_new(mem); + + if (file_chunk_request == nullptr) { + return nullptr; } - return events->file_chunk_request_size; -} + Tox_Event event; + event.type = TOX_EVENT_FILE_CHUNK_REQUEST; + event.data.file_chunk_request = file_chunk_request; -const Tox_Event_File_Chunk_Request *tox_events_get_file_chunk_request(const Tox_Events *events, uint32_t index) -{ - assert(index < events->file_chunk_request_size); - assert(events->file_chunk_request != nullptr); - return &events->file_chunk_request[index]; + tox_events_add(events, &event); + return file_chunk_request; } -bool tox_events_pack_file_chunk_request(const Tox_Events *events, Bin_Pack *bp) +bool tox_event_file_chunk_request_unpack( + Tox_Event_File_Chunk_Request **event, Bin_Unpack *bu, const Memory *mem) { - const uint32_t size = tox_events_get_file_chunk_request_size(events); + assert(event != nullptr); + *event = tox_event_file_chunk_request_new(mem); - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_file_chunk_request_pack(tox_events_get_file_chunk_request(events, i), bp)) { - return false; - } + if (*event == nullptr) { + return false; } - return true; + + return tox_event_file_chunk_request_unpack_into(*event, bu); } -bool tox_events_unpack_file_chunk_request(Tox_Events *events, Bin_Unpack *bu) +non_null() +static Tox_Event_File_Chunk_Request *tox_event_file_chunk_request_alloc(void *user_data) { - Tox_Event_File_Chunk_Request *event = tox_events_add_file_chunk_request(events); + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); - if (event == nullptr) { - return false; + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_File_Chunk_Request *file_chunk_request = tox_events_add_file_chunk_request(state->events, state->mem); + + if (file_chunk_request == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; } - return tox_event_file_chunk_request_unpack(event, bu); + return file_chunk_request; } @@ -218,20 +212,12 @@ bool tox_events_unpack_file_chunk_request(Tox_Events *events, Bin_Unpack *bu) *****************************************************/ -void tox_events_handle_file_chunk_request(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, - size_t length, void *user_data) +void tox_events_handle_file_chunk_request(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, size_t length, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_File_Chunk_Request *file_chunk_request = tox_events_add_file_chunk_request(state->events); + Tox_Event_File_Chunk_Request *file_chunk_request = tox_event_file_chunk_request_alloc(user_data); if (file_chunk_request == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/file_recv.c b/toxcore/events/file_recv.c index 0aefb23d14..25ea7039db 100644 --- a/toxcore/events/file_recv.c +++ b/toxcore/events/file_recv.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,6 +11,7 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" @@ -31,19 +32,6 @@ struct Tox_Event_File_Recv { uint32_t filename_length; }; -non_null() -static void tox_event_file_recv_construct(Tox_Event_File_Recv *file_recv) -{ - *file_recv = (Tox_Event_File_Recv) { - 0 - }; -} -non_null() -static void tox_event_file_recv_destruct(Tox_Event_File_Recv *file_recv) -{ - free(file_recv->filename); -} - non_null() static void tox_event_file_recv_set_friend_number(Tox_Event_File_Recv *file_recv, uint32_t friend_number) @@ -97,8 +85,8 @@ uint64_t tox_event_file_recv_get_file_size(const Tox_Event_File_Recv *file_recv) } non_null() -static bool tox_event_file_recv_set_filename(Tox_Event_File_Recv *file_recv, const uint8_t *filename, - uint32_t filename_length) +static bool tox_event_file_recv_set_filename(Tox_Event_File_Recv *file_recv, + const uint8_t *filename, uint32_t filename_length) { assert(file_recv != nullptr); @@ -131,7 +119,19 @@ const uint8_t *tox_event_file_recv_get_filename(const Tox_Event_File_Recv *file_ } non_null() -static bool tox_event_file_recv_pack( +static void tox_event_file_recv_construct(Tox_Event_File_Recv *file_recv) +{ + *file_recv = (Tox_Event_File_Recv) { + 0 + }; +} +non_null() +static void tox_event_file_recv_destruct(Tox_Event_File_Recv *file_recv, const Memory *mem) +{ + free(file_recv->filename); +} + +bool tox_event_file_recv_pack( const Tox_Event_File_Recv *event, Bin_Pack *bp) { assert(event != nullptr); @@ -146,7 +146,7 @@ static bool tox_event_file_recv_pack( } non_null() -static bool tox_event_file_recv_unpack( +static bool tox_event_file_recv_unpack_into( Tox_Event_File_Recv *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -164,90 +164,84 @@ static bool tox_event_file_recv_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_File_Recv *tox_events_add_file_recv(Tox_Events *events) +const Tox_Event_File_Recv *tox_event_get_file_recv(const Tox_Event *event) { - if (events->file_recv_size == UINT32_MAX) { - return nullptr; - } - - if (events->file_recv_size == events->file_recv_capacity) { - const uint32_t new_file_recv_capacity = events->file_recv_capacity * 2 + 1; - Tox_Event_File_Recv *new_file_recv = (Tox_Event_File_Recv *)realloc( - events->file_recv, new_file_recv_capacity * sizeof(Tox_Event_File_Recv)); + return event->type == TOX_EVENT_FILE_RECV ? event->data.file_recv : nullptr; +} - if (new_file_recv == nullptr) { - return nullptr; - } +Tox_Event_File_Recv *tox_event_file_recv_new(const Memory *mem) +{ + Tox_Event_File_Recv *const file_recv = + (Tox_Event_File_Recv *)mem_alloc(mem, sizeof(Tox_Event_File_Recv)); - events->file_recv = new_file_recv; - events->file_recv_capacity = new_file_recv_capacity; + if (file_recv == nullptr) { + return nullptr; } - Tox_Event_File_Recv *const file_recv = &events->file_recv[events->file_recv_size]; tox_event_file_recv_construct(file_recv); - ++events->file_recv_size; return file_recv; } -void tox_events_clear_file_recv(Tox_Events *events) +void tox_event_file_recv_free(Tox_Event_File_Recv *file_recv, const Memory *mem) { - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->file_recv_size; ++i) { - tox_event_file_recv_destruct(&events->file_recv[i]); + if (file_recv != nullptr) { + tox_event_file_recv_destruct(file_recv, mem); } - - free(events->file_recv); - events->file_recv = nullptr; - events->file_recv_size = 0; - events->file_recv_capacity = 0; + mem_delete(mem, file_recv); } -uint32_t tox_events_get_file_recv_size(const Tox_Events *events) +non_null() +static Tox_Event_File_Recv *tox_events_add_file_recv(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_File_Recv *const file_recv = tox_event_file_recv_new(mem); + + if (file_recv == nullptr) { + return nullptr; } - return events->file_recv_size; -} + Tox_Event event; + event.type = TOX_EVENT_FILE_RECV; + event.data.file_recv = file_recv; -const Tox_Event_File_Recv *tox_events_get_file_recv(const Tox_Events *events, uint32_t index) -{ - assert(index < events->file_recv_size); - assert(events->file_recv != nullptr); - return &events->file_recv[index]; + tox_events_add(events, &event); + return file_recv; } -bool tox_events_pack_file_recv(const Tox_Events *events, Bin_Pack *bp) +bool tox_event_file_recv_unpack( + Tox_Event_File_Recv **event, Bin_Unpack *bu, const Memory *mem) { - const uint32_t size = tox_events_get_file_recv_size(events); + assert(event != nullptr); + *event = tox_event_file_recv_new(mem); - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_file_recv_pack(tox_events_get_file_recv(events, i), bp)) { - return false; - } + if (*event == nullptr) { + return false; } - return true; + + return tox_event_file_recv_unpack_into(*event, bu); } -bool tox_events_unpack_file_recv(Tox_Events *events, Bin_Unpack *bu) +non_null() +static Tox_Event_File_Recv *tox_event_file_recv_alloc(void *user_data) { - Tox_Event_File_Recv *event = tox_events_add_file_recv(events); + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); - if (event == nullptr) { - return false; + if (state->events == nullptr) { + return nullptr; } - return tox_event_file_recv_unpack(event, bu); + Tox_Event_File_Recv *file_recv = tox_events_add_file_recv(state->events, state->mem); + + if (file_recv == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return file_recv; } @@ -258,20 +252,12 @@ bool tox_events_unpack_file_recv(Tox_Events *events, Bin_Unpack *bu) *****************************************************/ -void tox_events_handle_file_recv(Tox *tox, uint32_t friend_number, uint32_t file_number, uint32_t kind, - uint64_t file_size, const uint8_t *filename, size_t filename_length, void *user_data) +void tox_events_handle_file_recv(Tox *tox, uint32_t friend_number, uint32_t file_number, uint32_t kind, uint64_t file_size, const uint8_t *filename, size_t filename_length, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_File_Recv *file_recv = tox_events_add_file_recv(state->events); + Tox_Event_File_Recv *file_recv = tox_event_file_recv_alloc(user_data); if (file_recv == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/file_recv_chunk.c b/toxcore/events/file_recv_chunk.c index 98c667a13a..eda5818a49 100644 --- a/toxcore/events/file_recv_chunk.c +++ b/toxcore/events/file_recv_chunk.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,6 +11,7 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" @@ -30,19 +31,6 @@ struct Tox_Event_File_Recv_Chunk { uint32_t data_length; }; -non_null() -static void tox_event_file_recv_chunk_construct(Tox_Event_File_Recv_Chunk *file_recv_chunk) -{ - *file_recv_chunk = (Tox_Event_File_Recv_Chunk) { - 0 - }; -} -non_null() -static void tox_event_file_recv_chunk_destruct(Tox_Event_File_Recv_Chunk *file_recv_chunk) -{ - free(file_recv_chunk->data); -} - non_null() static void tox_event_file_recv_chunk_set_friend_number(Tox_Event_File_Recv_Chunk *file_recv_chunk, uint32_t friend_number) @@ -83,8 +71,8 @@ uint64_t tox_event_file_recv_chunk_get_position(const Tox_Event_File_Recv_Chunk } non_null() -static bool tox_event_file_recv_chunk_set_data(Tox_Event_File_Recv_Chunk *file_recv_chunk, const uint8_t *data, - uint32_t data_length) +static bool tox_event_file_recv_chunk_set_data(Tox_Event_File_Recv_Chunk *file_recv_chunk, + const uint8_t *data, uint32_t data_length) { assert(file_recv_chunk != nullptr); @@ -105,7 +93,7 @@ static bool tox_event_file_recv_chunk_set_data(Tox_Event_File_Recv_Chunk *file_r file_recv_chunk->data_length = data_length; return true; } -uint32_t tox_event_file_recv_chunk_get_length(const Tox_Event_File_Recv_Chunk *file_recv_chunk) +uint32_t tox_event_file_recv_chunk_get_data_length(const Tox_Event_File_Recv_Chunk *file_recv_chunk) { assert(file_recv_chunk != nullptr); return file_recv_chunk->data_length; @@ -117,7 +105,19 @@ const uint8_t *tox_event_file_recv_chunk_get_data(const Tox_Event_File_Recv_Chun } non_null() -static bool tox_event_file_recv_chunk_pack( +static void tox_event_file_recv_chunk_construct(Tox_Event_File_Recv_Chunk *file_recv_chunk) +{ + *file_recv_chunk = (Tox_Event_File_Recv_Chunk) { + 0 + }; +} +non_null() +static void tox_event_file_recv_chunk_destruct(Tox_Event_File_Recv_Chunk *file_recv_chunk, const Memory *mem) +{ + free(file_recv_chunk->data); +} + +bool tox_event_file_recv_chunk_pack( const Tox_Event_File_Recv_Chunk *event, Bin_Pack *bp) { assert(event != nullptr); @@ -131,7 +131,7 @@ static bool tox_event_file_recv_chunk_pack( } non_null() -static bool tox_event_file_recv_chunk_unpack( +static bool tox_event_file_recv_chunk_unpack_into( Tox_Event_File_Recv_Chunk *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -148,90 +148,84 @@ static bool tox_event_file_recv_chunk_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_File_Recv_Chunk *tox_events_add_file_recv_chunk(Tox_Events *events) +const Tox_Event_File_Recv_Chunk *tox_event_get_file_recv_chunk(const Tox_Event *event) { - if (events->file_recv_chunk_size == UINT32_MAX) { - return nullptr; - } - - if (events->file_recv_chunk_size == events->file_recv_chunk_capacity) { - const uint32_t new_file_recv_chunk_capacity = events->file_recv_chunk_capacity * 2 + 1; - Tox_Event_File_Recv_Chunk *new_file_recv_chunk = (Tox_Event_File_Recv_Chunk *)realloc( - events->file_recv_chunk, new_file_recv_chunk_capacity * sizeof(Tox_Event_File_Recv_Chunk)); + return event->type == TOX_EVENT_FILE_RECV_CHUNK ? event->data.file_recv_chunk : nullptr; +} - if (new_file_recv_chunk == nullptr) { - return nullptr; - } +Tox_Event_File_Recv_Chunk *tox_event_file_recv_chunk_new(const Memory *mem) +{ + Tox_Event_File_Recv_Chunk *const file_recv_chunk = + (Tox_Event_File_Recv_Chunk *)mem_alloc(mem, sizeof(Tox_Event_File_Recv_Chunk)); - events->file_recv_chunk = new_file_recv_chunk; - events->file_recv_chunk_capacity = new_file_recv_chunk_capacity; + if (file_recv_chunk == nullptr) { + return nullptr; } - Tox_Event_File_Recv_Chunk *const file_recv_chunk = &events->file_recv_chunk[events->file_recv_chunk_size]; tox_event_file_recv_chunk_construct(file_recv_chunk); - ++events->file_recv_chunk_size; return file_recv_chunk; } -void tox_events_clear_file_recv_chunk(Tox_Events *events) +void tox_event_file_recv_chunk_free(Tox_Event_File_Recv_Chunk *file_recv_chunk, const Memory *mem) { - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->file_recv_chunk_size; ++i) { - tox_event_file_recv_chunk_destruct(&events->file_recv_chunk[i]); + if (file_recv_chunk != nullptr) { + tox_event_file_recv_chunk_destruct(file_recv_chunk, mem); } - - free(events->file_recv_chunk); - events->file_recv_chunk = nullptr; - events->file_recv_chunk_size = 0; - events->file_recv_chunk_capacity = 0; + mem_delete(mem, file_recv_chunk); } -uint32_t tox_events_get_file_recv_chunk_size(const Tox_Events *events) +non_null() +static Tox_Event_File_Recv_Chunk *tox_events_add_file_recv_chunk(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_File_Recv_Chunk *const file_recv_chunk = tox_event_file_recv_chunk_new(mem); + + if (file_recv_chunk == nullptr) { + return nullptr; } - return events->file_recv_chunk_size; -} + Tox_Event event; + event.type = TOX_EVENT_FILE_RECV_CHUNK; + event.data.file_recv_chunk = file_recv_chunk; -const Tox_Event_File_Recv_Chunk *tox_events_get_file_recv_chunk(const Tox_Events *events, uint32_t index) -{ - assert(index < events->file_recv_chunk_size); - assert(events->file_recv_chunk != nullptr); - return &events->file_recv_chunk[index]; + tox_events_add(events, &event); + return file_recv_chunk; } -bool tox_events_pack_file_recv_chunk(const Tox_Events *events, Bin_Pack *bp) +bool tox_event_file_recv_chunk_unpack( + Tox_Event_File_Recv_Chunk **event, Bin_Unpack *bu, const Memory *mem) { - const uint32_t size = tox_events_get_file_recv_chunk_size(events); + assert(event != nullptr); + *event = tox_event_file_recv_chunk_new(mem); - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_file_recv_chunk_pack(tox_events_get_file_recv_chunk(events, i), bp)) { - return false; - } + if (*event == nullptr) { + return false; } - return true; + + return tox_event_file_recv_chunk_unpack_into(*event, bu); } -bool tox_events_unpack_file_recv_chunk(Tox_Events *events, Bin_Unpack *bu) +non_null() +static Tox_Event_File_Recv_Chunk *tox_event_file_recv_chunk_alloc(void *user_data) { - Tox_Event_File_Recv_Chunk *event = tox_events_add_file_recv_chunk(events); + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); - if (event == nullptr) { - return false; + if (state->events == nullptr) { + return nullptr; } - return tox_event_file_recv_chunk_unpack(event, bu); + Tox_Event_File_Recv_Chunk *file_recv_chunk = tox_events_add_file_recv_chunk(state->events, state->mem); + + if (file_recv_chunk == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return file_recv_chunk; } @@ -242,20 +236,12 @@ bool tox_events_unpack_file_recv_chunk(Tox_Events *events, Bin_Unpack *bu) *****************************************************/ -void tox_events_handle_file_recv_chunk(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, - const uint8_t *data, size_t length, void *user_data) +void tox_events_handle_file_recv_chunk(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, const uint8_t *data, size_t length, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_File_Recv_Chunk *file_recv_chunk = tox_events_add_file_recv_chunk(state->events); + Tox_Event_File_Recv_Chunk *file_recv_chunk = tox_event_file_recv_chunk_alloc(user_data); if (file_recv_chunk == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/file_recv_control.c b/toxcore/events/file_recv_control.c index 21ef33f5ea..e9aac24d27 100644 --- a/toxcore/events/file_recv_control.c +++ b/toxcore/events/file_recv_control.c @@ -1,15 +1,15 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" @@ -28,19 +28,6 @@ struct Tox_Event_File_Recv_Control { Tox_File_Control control; }; -non_null() -static void tox_event_file_recv_control_construct(Tox_Event_File_Recv_Control *file_recv_control) -{ - *file_recv_control = (Tox_Event_File_Recv_Control) { - 0 - }; -} -non_null() -static void tox_event_file_recv_control_destruct(Tox_Event_File_Recv_Control *file_recv_control) -{ - return; -} - non_null() static void tox_event_file_recv_control_set_friend_number(Tox_Event_File_Recv_Control *file_recv_control, uint32_t friend_number) @@ -81,7 +68,19 @@ Tox_File_Control tox_event_file_recv_control_get_control(const Tox_Event_File_Re } non_null() -static bool tox_event_file_recv_control_pack( +static void tox_event_file_recv_control_construct(Tox_Event_File_Recv_Control *file_recv_control) +{ + *file_recv_control = (Tox_Event_File_Recv_Control) { + 0 + }; +} +non_null() +static void tox_event_file_recv_control_destruct(Tox_Event_File_Recv_Control *file_recv_control, const Memory *mem) +{ + return; +} + +bool tox_event_file_recv_control_pack( const Tox_Event_File_Recv_Control *event, Bin_Pack *bp) { assert(event != nullptr); @@ -94,7 +93,7 @@ static bool tox_event_file_recv_control_pack( } non_null() -static bool tox_event_file_recv_control_unpack( +static bool tox_event_file_recv_control_unpack_into( Tox_Event_File_Recv_Control *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -110,90 +109,84 @@ static bool tox_event_file_recv_control_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_File_Recv_Control *tox_events_add_file_recv_control(Tox_Events *events) +const Tox_Event_File_Recv_Control *tox_event_get_file_recv_control(const Tox_Event *event) { - if (events->file_recv_control_size == UINT32_MAX) { - return nullptr; - } - - if (events->file_recv_control_size == events->file_recv_control_capacity) { - const uint32_t new_file_recv_control_capacity = events->file_recv_control_capacity * 2 + 1; - Tox_Event_File_Recv_Control *new_file_recv_control = (Tox_Event_File_Recv_Control *)realloc( - events->file_recv_control, new_file_recv_control_capacity * sizeof(Tox_Event_File_Recv_Control)); + return event->type == TOX_EVENT_FILE_RECV_CONTROL ? event->data.file_recv_control : nullptr; +} - if (new_file_recv_control == nullptr) { - return nullptr; - } +Tox_Event_File_Recv_Control *tox_event_file_recv_control_new(const Memory *mem) +{ + Tox_Event_File_Recv_Control *const file_recv_control = + (Tox_Event_File_Recv_Control *)mem_alloc(mem, sizeof(Tox_Event_File_Recv_Control)); - events->file_recv_control = new_file_recv_control; - events->file_recv_control_capacity = new_file_recv_control_capacity; + if (file_recv_control == nullptr) { + return nullptr; } - Tox_Event_File_Recv_Control *const file_recv_control = &events->file_recv_control[events->file_recv_control_size]; tox_event_file_recv_control_construct(file_recv_control); - ++events->file_recv_control_size; return file_recv_control; } -void tox_events_clear_file_recv_control(Tox_Events *events) +void tox_event_file_recv_control_free(Tox_Event_File_Recv_Control *file_recv_control, const Memory *mem) { - if (events == nullptr) { - return; + if (file_recv_control != nullptr) { + tox_event_file_recv_control_destruct(file_recv_control, mem); } - - for (uint32_t i = 0; i < events->file_recv_control_size; ++i) { - tox_event_file_recv_control_destruct(&events->file_recv_control[i]); - } - - free(events->file_recv_control); - events->file_recv_control = nullptr; - events->file_recv_control_size = 0; - events->file_recv_control_capacity = 0; + mem_delete(mem, file_recv_control); } -uint32_t tox_events_get_file_recv_control_size(const Tox_Events *events) +non_null() +static Tox_Event_File_Recv_Control *tox_events_add_file_recv_control(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_File_Recv_Control *const file_recv_control = tox_event_file_recv_control_new(mem); + + if (file_recv_control == nullptr) { + return nullptr; } - return events->file_recv_control_size; -} + Tox_Event event; + event.type = TOX_EVENT_FILE_RECV_CONTROL; + event.data.file_recv_control = file_recv_control; -const Tox_Event_File_Recv_Control *tox_events_get_file_recv_control(const Tox_Events *events, uint32_t index) -{ - assert(index < events->file_recv_control_size); - assert(events->file_recv_control != nullptr); - return &events->file_recv_control[index]; + tox_events_add(events, &event); + return file_recv_control; } -bool tox_events_pack_file_recv_control(const Tox_Events *events, Bin_Pack *bp) +bool tox_event_file_recv_control_unpack( + Tox_Event_File_Recv_Control **event, Bin_Unpack *bu, const Memory *mem) { - const uint32_t size = tox_events_get_file_recv_control_size(events); + assert(event != nullptr); + *event = tox_event_file_recv_control_new(mem); - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_file_recv_control_pack(tox_events_get_file_recv_control(events, i), bp)) { - return false; - } + if (*event == nullptr) { + return false; } - return true; + + return tox_event_file_recv_control_unpack_into(*event, bu); } -bool tox_events_unpack_file_recv_control(Tox_Events *events, Bin_Unpack *bu) +non_null() +static Tox_Event_File_Recv_Control *tox_event_file_recv_control_alloc(void *user_data) { - Tox_Event_File_Recv_Control *event = tox_events_add_file_recv_control(events); + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); - if (event == nullptr) { - return false; + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_File_Recv_Control *file_recv_control = tox_events_add_file_recv_control(state->events, state->mem); + + if (file_recv_control == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; } - return tox_event_file_recv_control_unpack(event, bu); + return file_recv_control; } @@ -204,20 +197,12 @@ bool tox_events_unpack_file_recv_control(Tox_Events *events, Bin_Unpack *bu) *****************************************************/ -void tox_events_handle_file_recv_control(Tox *tox, uint32_t friend_number, uint32_t file_number, - Tox_File_Control control, void *user_data) +void tox_events_handle_file_recv_control(Tox *tox, uint32_t friend_number, uint32_t file_number, Tox_File_Control control, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_File_Recv_Control *file_recv_control = tox_events_add_file_recv_control(state->events); + Tox_Event_File_Recv_Control *file_recv_control = tox_event_file_recv_control_alloc(user_data); if (file_recv_control == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/friend_connection_status.c b/toxcore/events/friend_connection_status.c index 4341f72d05..9eded8b50c 100644 --- a/toxcore/events/friend_connection_status.c +++ b/toxcore/events/friend_connection_status.c @@ -1,15 +1,15 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" @@ -28,48 +28,45 @@ struct Tox_Event_Friend_Connection_Status { }; non_null() -static void tox_event_friend_connection_status_construct(Tox_Event_Friend_Connection_Status *friend_connection_status) -{ - *friend_connection_status = (Tox_Event_Friend_Connection_Status) { - 0 - }; -} -non_null() -static void tox_event_friend_connection_status_destruct(Tox_Event_Friend_Connection_Status *friend_connection_status) -{ - return; -} - -non_null() -static void tox_event_friend_connection_status_set_friend_number(Tox_Event_Friend_Connection_Status - *friend_connection_status, uint32_t friend_number) +static void tox_event_friend_connection_status_set_friend_number(Tox_Event_Friend_Connection_Status *friend_connection_status, + uint32_t friend_number) { assert(friend_connection_status != nullptr); friend_connection_status->friend_number = friend_number; } -uint32_t tox_event_friend_connection_status_get_friend_number(const Tox_Event_Friend_Connection_Status - *friend_connection_status) +uint32_t tox_event_friend_connection_status_get_friend_number(const Tox_Event_Friend_Connection_Status *friend_connection_status) { assert(friend_connection_status != nullptr); return friend_connection_status->friend_number; } non_null() -static void tox_event_friend_connection_status_set_connection_status(Tox_Event_Friend_Connection_Status - *friend_connection_status, Tox_Connection connection_status) +static void tox_event_friend_connection_status_set_connection_status(Tox_Event_Friend_Connection_Status *friend_connection_status, + Tox_Connection connection_status) { assert(friend_connection_status != nullptr); friend_connection_status->connection_status = connection_status; } -Tox_Connection tox_event_friend_connection_status_get_connection_status(const Tox_Event_Friend_Connection_Status - *friend_connection_status) +Tox_Connection tox_event_friend_connection_status_get_connection_status(const Tox_Event_Friend_Connection_Status *friend_connection_status) { assert(friend_connection_status != nullptr); return friend_connection_status->connection_status; } non_null() -static bool tox_event_friend_connection_status_pack( +static void tox_event_friend_connection_status_construct(Tox_Event_Friend_Connection_Status *friend_connection_status) +{ + *friend_connection_status = (Tox_Event_Friend_Connection_Status) { + 0 + }; +} +non_null() +static void tox_event_friend_connection_status_destruct(Tox_Event_Friend_Connection_Status *friend_connection_status, const Memory *mem) +{ + return; +} + +bool tox_event_friend_connection_status_pack( const Tox_Event_Friend_Connection_Status *event, Bin_Pack *bp) { assert(event != nullptr); @@ -81,7 +78,7 @@ static bool tox_event_friend_connection_status_pack( } non_null() -static bool tox_event_friend_connection_status_unpack( +static bool tox_event_friend_connection_status_unpack_into( Tox_Event_Friend_Connection_Status *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -96,92 +93,84 @@ static bool tox_event_friend_connection_status_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Friend_Connection_Status *tox_events_add_friend_connection_status(Tox_Events *events) +const Tox_Event_Friend_Connection_Status *tox_event_get_friend_connection_status(const Tox_Event *event) { - if (events->friend_connection_status_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_connection_status_size == events->friend_connection_status_capacity) { - const uint32_t new_friend_connection_status_capacity = events->friend_connection_status_capacity * 2 + 1; - Tox_Event_Friend_Connection_Status *new_friend_connection_status = (Tox_Event_Friend_Connection_Status *)realloc( - events->friend_connection_status, new_friend_connection_status_capacity * sizeof(Tox_Event_Friend_Connection_Status)); + return event->type == TOX_EVENT_FRIEND_CONNECTION_STATUS ? event->data.friend_connection_status : nullptr; +} - if (new_friend_connection_status == nullptr) { - return nullptr; - } +Tox_Event_Friend_Connection_Status *tox_event_friend_connection_status_new(const Memory *mem) +{ + Tox_Event_Friend_Connection_Status *const friend_connection_status = + (Tox_Event_Friend_Connection_Status *)mem_alloc(mem, sizeof(Tox_Event_Friend_Connection_Status)); - events->friend_connection_status = new_friend_connection_status; - events->friend_connection_status_capacity = new_friend_connection_status_capacity; + if (friend_connection_status == nullptr) { + return nullptr; } - Tox_Event_Friend_Connection_Status *const friend_connection_status = - &events->friend_connection_status[events->friend_connection_status_size]; tox_event_friend_connection_status_construct(friend_connection_status); - ++events->friend_connection_status_size; return friend_connection_status; } -void tox_events_clear_friend_connection_status(Tox_Events *events) +void tox_event_friend_connection_status_free(Tox_Event_Friend_Connection_Status *friend_connection_status, const Memory *mem) { - if (events == nullptr) { - return; + if (friend_connection_status != nullptr) { + tox_event_friend_connection_status_destruct(friend_connection_status, mem); } - - for (uint32_t i = 0; i < events->friend_connection_status_size; ++i) { - tox_event_friend_connection_status_destruct(&events->friend_connection_status[i]); - } - - free(events->friend_connection_status); - events->friend_connection_status = nullptr; - events->friend_connection_status_size = 0; - events->friend_connection_status_capacity = 0; + mem_delete(mem, friend_connection_status); } -uint32_t tox_events_get_friend_connection_status_size(const Tox_Events *events) +non_null() +static Tox_Event_Friend_Connection_Status *tox_events_add_friend_connection_status(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Friend_Connection_Status *const friend_connection_status = tox_event_friend_connection_status_new(mem); + + if (friend_connection_status == nullptr) { + return nullptr; } - return events->friend_connection_status_size; -} + Tox_Event event; + event.type = TOX_EVENT_FRIEND_CONNECTION_STATUS; + event.data.friend_connection_status = friend_connection_status; -const Tox_Event_Friend_Connection_Status *tox_events_get_friend_connection_status(const Tox_Events *events, - uint32_t index) -{ - assert(index < events->friend_connection_status_size); - assert(events->friend_connection_status != nullptr); - return &events->friend_connection_status[index]; + tox_events_add(events, &event); + return friend_connection_status; } -bool tox_events_pack_friend_connection_status(const Tox_Events *events, Bin_Pack *bp) +bool tox_event_friend_connection_status_unpack( + Tox_Event_Friend_Connection_Status **event, Bin_Unpack *bu, const Memory *mem) { - const uint32_t size = tox_events_get_friend_connection_status_size(events); + assert(event != nullptr); + *event = tox_event_friend_connection_status_new(mem); - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_connection_status_pack(tox_events_get_friend_connection_status(events, i), bp)) { - return false; - } + if (*event == nullptr) { + return false; } - return true; + + return tox_event_friend_connection_status_unpack_into(*event, bu); } -bool tox_events_unpack_friend_connection_status(Tox_Events *events, Bin_Unpack *bu) +non_null() +static Tox_Event_Friend_Connection_Status *tox_event_friend_connection_status_alloc(void *user_data) { - Tox_Event_Friend_Connection_Status *event = tox_events_add_friend_connection_status(events); + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); - if (event == nullptr) { - return false; + if (state->events == nullptr) { + return nullptr; } - return tox_event_friend_connection_status_unpack(event, bu); + Tox_Event_Friend_Connection_Status *friend_connection_status = tox_events_add_friend_connection_status(state->events, state->mem); + + if (friend_connection_status == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return friend_connection_status; } @@ -195,17 +184,9 @@ bool tox_events_unpack_friend_connection_status(Tox_Events *events, Bin_Unpack * void tox_events_handle_friend_connection_status(Tox *tox, uint32_t friend_number, Tox_Connection connection_status, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Connection_Status *friend_connection_status = tox_events_add_friend_connection_status(state->events); + Tox_Event_Friend_Connection_Status *friend_connection_status = tox_event_friend_connection_status_alloc(user_data); if (friend_connection_status == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/friend_lossless_packet.c b/toxcore/events/friend_lossless_packet.c index c1d4ec03a1..2d5da3bf5a 100644 --- a/toxcore/events/friend_lossless_packet.c +++ b/toxcore/events/friend_lossless_packet.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,6 +11,7 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" @@ -28,19 +29,6 @@ struct Tox_Event_Friend_Lossless_Packet { uint32_t data_length; }; -non_null() -static void tox_event_friend_lossless_packet_construct(Tox_Event_Friend_Lossless_Packet *friend_lossless_packet) -{ - *friend_lossless_packet = (Tox_Event_Friend_Lossless_Packet) { - 0 - }; -} -non_null() -static void tox_event_friend_lossless_packet_destruct(Tox_Event_Friend_Lossless_Packet *friend_lossless_packet) -{ - free(friend_lossless_packet->data); -} - non_null() static void tox_event_friend_lossless_packet_set_friend_number(Tox_Event_Friend_Lossless_Packet *friend_lossless_packet, uint32_t friend_number) @@ -48,8 +36,7 @@ static void tox_event_friend_lossless_packet_set_friend_number(Tox_Event_Friend_ assert(friend_lossless_packet != nullptr); friend_lossless_packet->friend_number = friend_number; } -uint32_t tox_event_friend_lossless_packet_get_friend_number(const Tox_Event_Friend_Lossless_Packet - *friend_lossless_packet) +uint32_t tox_event_friend_lossless_packet_get_friend_number(const Tox_Event_Friend_Lossless_Packet *friend_lossless_packet) { assert(friend_lossless_packet != nullptr); return friend_lossless_packet->friend_number; @@ -90,7 +77,19 @@ const uint8_t *tox_event_friend_lossless_packet_get_data(const Tox_Event_Friend_ } non_null() -static bool tox_event_friend_lossless_packet_pack( +static void tox_event_friend_lossless_packet_construct(Tox_Event_Friend_Lossless_Packet *friend_lossless_packet) +{ + *friend_lossless_packet = (Tox_Event_Friend_Lossless_Packet) { + 0 + }; +} +non_null() +static void tox_event_friend_lossless_packet_destruct(Tox_Event_Friend_Lossless_Packet *friend_lossless_packet, const Memory *mem) +{ + free(friend_lossless_packet->data); +} + +bool tox_event_friend_lossless_packet_pack( const Tox_Event_Friend_Lossless_Packet *event, Bin_Pack *bp) { assert(event != nullptr); @@ -102,7 +101,7 @@ static bool tox_event_friend_lossless_packet_pack( } non_null() -static bool tox_event_friend_lossless_packet_unpack( +static bool tox_event_friend_lossless_packet_unpack_into( Tox_Event_Friend_Lossless_Packet *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -117,91 +116,84 @@ static bool tox_event_friend_lossless_packet_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Friend_Lossless_Packet *tox_events_add_friend_lossless_packet(Tox_Events *events) +const Tox_Event_Friend_Lossless_Packet *tox_event_get_friend_lossless_packet(const Tox_Event *event) { - if (events->friend_lossless_packet_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_lossless_packet_size == events->friend_lossless_packet_capacity) { - const uint32_t new_friend_lossless_packet_capacity = events->friend_lossless_packet_capacity * 2 + 1; - Tox_Event_Friend_Lossless_Packet *new_friend_lossless_packet = (Tox_Event_Friend_Lossless_Packet *)realloc( - events->friend_lossless_packet, new_friend_lossless_packet_capacity * sizeof(Tox_Event_Friend_Lossless_Packet)); + return event->type == TOX_EVENT_FRIEND_LOSSLESS_PACKET ? event->data.friend_lossless_packet : nullptr; +} - if (new_friend_lossless_packet == nullptr) { - return nullptr; - } +Tox_Event_Friend_Lossless_Packet *tox_event_friend_lossless_packet_new(const Memory *mem) +{ + Tox_Event_Friend_Lossless_Packet *const friend_lossless_packet = + (Tox_Event_Friend_Lossless_Packet *)mem_alloc(mem, sizeof(Tox_Event_Friend_Lossless_Packet)); - events->friend_lossless_packet = new_friend_lossless_packet; - events->friend_lossless_packet_capacity = new_friend_lossless_packet_capacity; + if (friend_lossless_packet == nullptr) { + return nullptr; } - Tox_Event_Friend_Lossless_Packet *const friend_lossless_packet = - &events->friend_lossless_packet[events->friend_lossless_packet_size]; tox_event_friend_lossless_packet_construct(friend_lossless_packet); - ++events->friend_lossless_packet_size; return friend_lossless_packet; } -void tox_events_clear_friend_lossless_packet(Tox_Events *events) +void tox_event_friend_lossless_packet_free(Tox_Event_Friend_Lossless_Packet *friend_lossless_packet, const Memory *mem) { - if (events == nullptr) { - return; + if (friend_lossless_packet != nullptr) { + tox_event_friend_lossless_packet_destruct(friend_lossless_packet, mem); } - - for (uint32_t i = 0; i < events->friend_lossless_packet_size; ++i) { - tox_event_friend_lossless_packet_destruct(&events->friend_lossless_packet[i]); - } - - free(events->friend_lossless_packet); - events->friend_lossless_packet = nullptr; - events->friend_lossless_packet_size = 0; - events->friend_lossless_packet_capacity = 0; + mem_delete(mem, friend_lossless_packet); } -uint32_t tox_events_get_friend_lossless_packet_size(const Tox_Events *events) +non_null() +static Tox_Event_Friend_Lossless_Packet *tox_events_add_friend_lossless_packet(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Friend_Lossless_Packet *const friend_lossless_packet = tox_event_friend_lossless_packet_new(mem); + + if (friend_lossless_packet == nullptr) { + return nullptr; } - return events->friend_lossless_packet_size; -} + Tox_Event event; + event.type = TOX_EVENT_FRIEND_LOSSLESS_PACKET; + event.data.friend_lossless_packet = friend_lossless_packet; -const Tox_Event_Friend_Lossless_Packet *tox_events_get_friend_lossless_packet(const Tox_Events *events, uint32_t index) -{ - assert(index < events->friend_lossless_packet_size); - assert(events->friend_lossless_packet != nullptr); - return &events->friend_lossless_packet[index]; + tox_events_add(events, &event); + return friend_lossless_packet; } -bool tox_events_pack_friend_lossless_packet(const Tox_Events *events, Bin_Pack *bp) +bool tox_event_friend_lossless_packet_unpack( + Tox_Event_Friend_Lossless_Packet **event, Bin_Unpack *bu, const Memory *mem) { - const uint32_t size = tox_events_get_friend_lossless_packet_size(events); + assert(event != nullptr); + *event = tox_event_friend_lossless_packet_new(mem); - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_lossless_packet_pack(tox_events_get_friend_lossless_packet(events, i), bp)) { - return false; - } + if (*event == nullptr) { + return false; } - return true; + + return tox_event_friend_lossless_packet_unpack_into(*event, bu); } -bool tox_events_unpack_friend_lossless_packet(Tox_Events *events, Bin_Unpack *bu) +non_null() +static Tox_Event_Friend_Lossless_Packet *tox_event_friend_lossless_packet_alloc(void *user_data) { - Tox_Event_Friend_Lossless_Packet *event = tox_events_add_friend_lossless_packet(events); + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); - if (event == nullptr) { - return false; + if (state->events == nullptr) { + return nullptr; } - return tox_event_friend_lossless_packet_unpack(event, bu); + Tox_Event_Friend_Lossless_Packet *friend_lossless_packet = tox_events_add_friend_lossless_packet(state->events, state->mem); + + if (friend_lossless_packet == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return friend_lossless_packet; } @@ -215,17 +207,9 @@ bool tox_events_unpack_friend_lossless_packet(Tox_Events *events, Bin_Unpack *bu void tox_events_handle_friend_lossless_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Lossless_Packet *friend_lossless_packet = tox_events_add_friend_lossless_packet(state->events); + Tox_Event_Friend_Lossless_Packet *friend_lossless_packet = tox_event_friend_lossless_packet_alloc(user_data); if (friend_lossless_packet == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/friend_lossy_packet.c b/toxcore/events/friend_lossy_packet.c index e3ba13615b..d7319f533c 100644 --- a/toxcore/events/friend_lossy_packet.c +++ b/toxcore/events/friend_lossy_packet.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,6 +11,7 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" @@ -28,19 +29,6 @@ struct Tox_Event_Friend_Lossy_Packet { uint32_t data_length; }; -non_null() -static void tox_event_friend_lossy_packet_construct(Tox_Event_Friend_Lossy_Packet *friend_lossy_packet) -{ - *friend_lossy_packet = (Tox_Event_Friend_Lossy_Packet) { - 0 - }; -} -non_null() -static void tox_event_friend_lossy_packet_destruct(Tox_Event_Friend_Lossy_Packet *friend_lossy_packet) -{ - free(friend_lossy_packet->data); -} - non_null() static void tox_event_friend_lossy_packet_set_friend_number(Tox_Event_Friend_Lossy_Packet *friend_lossy_packet, uint32_t friend_number) @@ -89,7 +77,19 @@ const uint8_t *tox_event_friend_lossy_packet_get_data(const Tox_Event_Friend_Los } non_null() -static bool tox_event_friend_lossy_packet_pack( +static void tox_event_friend_lossy_packet_construct(Tox_Event_Friend_Lossy_Packet *friend_lossy_packet) +{ + *friend_lossy_packet = (Tox_Event_Friend_Lossy_Packet) { + 0 + }; +} +non_null() +static void tox_event_friend_lossy_packet_destruct(Tox_Event_Friend_Lossy_Packet *friend_lossy_packet, const Memory *mem) +{ + free(friend_lossy_packet->data); +} + +bool tox_event_friend_lossy_packet_pack( const Tox_Event_Friend_Lossy_Packet *event, Bin_Pack *bp) { assert(event != nullptr); @@ -101,7 +101,7 @@ static bool tox_event_friend_lossy_packet_pack( } non_null() -static bool tox_event_friend_lossy_packet_unpack( +static bool tox_event_friend_lossy_packet_unpack_into( Tox_Event_Friend_Lossy_Packet *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -116,91 +116,84 @@ static bool tox_event_friend_lossy_packet_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Friend_Lossy_Packet *tox_events_add_friend_lossy_packet(Tox_Events *events) +const Tox_Event_Friend_Lossy_Packet *tox_event_get_friend_lossy_packet(const Tox_Event *event) { - if (events->friend_lossy_packet_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_lossy_packet_size == events->friend_lossy_packet_capacity) { - const uint32_t new_friend_lossy_packet_capacity = events->friend_lossy_packet_capacity * 2 + 1; - Tox_Event_Friend_Lossy_Packet *new_friend_lossy_packet = (Tox_Event_Friend_Lossy_Packet *)realloc( - events->friend_lossy_packet, new_friend_lossy_packet_capacity * sizeof(Tox_Event_Friend_Lossy_Packet)); + return event->type == TOX_EVENT_FRIEND_LOSSY_PACKET ? event->data.friend_lossy_packet : nullptr; +} - if (new_friend_lossy_packet == nullptr) { - return nullptr; - } +Tox_Event_Friend_Lossy_Packet *tox_event_friend_lossy_packet_new(const Memory *mem) +{ + Tox_Event_Friend_Lossy_Packet *const friend_lossy_packet = + (Tox_Event_Friend_Lossy_Packet *)mem_alloc(mem, sizeof(Tox_Event_Friend_Lossy_Packet)); - events->friend_lossy_packet = new_friend_lossy_packet; - events->friend_lossy_packet_capacity = new_friend_lossy_packet_capacity; + if (friend_lossy_packet == nullptr) { + return nullptr; } - Tox_Event_Friend_Lossy_Packet *const friend_lossy_packet = - &events->friend_lossy_packet[events->friend_lossy_packet_size]; tox_event_friend_lossy_packet_construct(friend_lossy_packet); - ++events->friend_lossy_packet_size; return friend_lossy_packet; } -void tox_events_clear_friend_lossy_packet(Tox_Events *events) +void tox_event_friend_lossy_packet_free(Tox_Event_Friend_Lossy_Packet *friend_lossy_packet, const Memory *mem) { - if (events == nullptr) { - return; + if (friend_lossy_packet != nullptr) { + tox_event_friend_lossy_packet_destruct(friend_lossy_packet, mem); } - - for (uint32_t i = 0; i < events->friend_lossy_packet_size; ++i) { - tox_event_friend_lossy_packet_destruct(&events->friend_lossy_packet[i]); - } - - free(events->friend_lossy_packet); - events->friend_lossy_packet = nullptr; - events->friend_lossy_packet_size = 0; - events->friend_lossy_packet_capacity = 0; + mem_delete(mem, friend_lossy_packet); } -uint32_t tox_events_get_friend_lossy_packet_size(const Tox_Events *events) +non_null() +static Tox_Event_Friend_Lossy_Packet *tox_events_add_friend_lossy_packet(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Friend_Lossy_Packet *const friend_lossy_packet = tox_event_friend_lossy_packet_new(mem); + + if (friend_lossy_packet == nullptr) { + return nullptr; } - return events->friend_lossy_packet_size; -} + Tox_Event event; + event.type = TOX_EVENT_FRIEND_LOSSY_PACKET; + event.data.friend_lossy_packet = friend_lossy_packet; -const Tox_Event_Friend_Lossy_Packet *tox_events_get_friend_lossy_packet(const Tox_Events *events, uint32_t index) -{ - assert(index < events->friend_lossy_packet_size); - assert(events->friend_lossy_packet != nullptr); - return &events->friend_lossy_packet[index]; + tox_events_add(events, &event); + return friend_lossy_packet; } -bool tox_events_pack_friend_lossy_packet(const Tox_Events *events, Bin_Pack *bp) +bool tox_event_friend_lossy_packet_unpack( + Tox_Event_Friend_Lossy_Packet **event, Bin_Unpack *bu, const Memory *mem) { - const uint32_t size = tox_events_get_friend_lossy_packet_size(events); + assert(event != nullptr); + *event = tox_event_friend_lossy_packet_new(mem); - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_lossy_packet_pack(tox_events_get_friend_lossy_packet(events, i), bp)) { - return false; - } + if (*event == nullptr) { + return false; } - return true; + + return tox_event_friend_lossy_packet_unpack_into(*event, bu); } -bool tox_events_unpack_friend_lossy_packet(Tox_Events *events, Bin_Unpack *bu) +non_null() +static Tox_Event_Friend_Lossy_Packet *tox_event_friend_lossy_packet_alloc(void *user_data) { - Tox_Event_Friend_Lossy_Packet *event = tox_events_add_friend_lossy_packet(events); + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); - if (event == nullptr) { - return false; + if (state->events == nullptr) { + return nullptr; } - return tox_event_friend_lossy_packet_unpack(event, bu); + Tox_Event_Friend_Lossy_Packet *friend_lossy_packet = tox_events_add_friend_lossy_packet(state->events, state->mem); + + if (friend_lossy_packet == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return friend_lossy_packet; } @@ -214,17 +207,9 @@ bool tox_events_unpack_friend_lossy_packet(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_friend_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Lossy_Packet *friend_lossy_packet = tox_events_add_friend_lossy_packet(state->events); + Tox_Event_Friend_Lossy_Packet *friend_lossy_packet = tox_event_friend_lossy_packet_alloc(user_data); if (friend_lossy_packet == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/friend_message.c b/toxcore/events/friend_message.c index 0487b7bfdb..2149549784 100644 --- a/toxcore/events/friend_message.c +++ b/toxcore/events/friend_message.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,6 +11,7 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" @@ -30,19 +31,6 @@ struct Tox_Event_Friend_Message { uint32_t message_length; }; -non_null() -static void tox_event_friend_message_construct(Tox_Event_Friend_Message *friend_message) -{ - *friend_message = (Tox_Event_Friend_Message) { - 0 - }; -} -non_null() -static void tox_event_friend_message_destruct(Tox_Event_Friend_Message *friend_message) -{ - free(friend_message->message); -} - non_null() static void tox_event_friend_message_set_friend_number(Tox_Event_Friend_Message *friend_message, uint32_t friend_number) @@ -57,7 +45,8 @@ uint32_t tox_event_friend_message_get_friend_number(const Tox_Event_Friend_Messa } non_null() -static void tox_event_friend_message_set_type(Tox_Event_Friend_Message *friend_message, Tox_Message_Type type) +static void tox_event_friend_message_set_type(Tox_Event_Friend_Message *friend_message, + Tox_Message_Type type) { assert(friend_message != nullptr); friend_message->type = type; @@ -69,8 +58,8 @@ Tox_Message_Type tox_event_friend_message_get_type(const Tox_Event_Friend_Messag } non_null() -static bool tox_event_friend_message_set_message(Tox_Event_Friend_Message *friend_message, const uint8_t *message, - uint32_t message_length) +static bool tox_event_friend_message_set_message(Tox_Event_Friend_Message *friend_message, + const uint8_t *message, uint32_t message_length) { assert(friend_message != nullptr); @@ -103,7 +92,19 @@ const uint8_t *tox_event_friend_message_get_message(const Tox_Event_Friend_Messa } non_null() -static bool tox_event_friend_message_pack( +static void tox_event_friend_message_construct(Tox_Event_Friend_Message *friend_message) +{ + *friend_message = (Tox_Event_Friend_Message) { + 0 + }; +} +non_null() +static void tox_event_friend_message_destruct(Tox_Event_Friend_Message *friend_message, const Memory *mem) +{ + free(friend_message->message); +} + +bool tox_event_friend_message_pack( const Tox_Event_Friend_Message *event, Bin_Pack *bp) { assert(event != nullptr); @@ -116,7 +117,7 @@ static bool tox_event_friend_message_pack( } non_null() -static bool tox_event_friend_message_unpack( +static bool tox_event_friend_message_unpack_into( Tox_Event_Friend_Message *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -132,90 +133,84 @@ static bool tox_event_friend_message_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Friend_Message *tox_events_add_friend_message(Tox_Events *events) +const Tox_Event_Friend_Message *tox_event_get_friend_message(const Tox_Event *event) { - if (events->friend_message_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_message_size == events->friend_message_capacity) { - const uint32_t new_friend_message_capacity = events->friend_message_capacity * 2 + 1; - Tox_Event_Friend_Message *new_friend_message = (Tox_Event_Friend_Message *)realloc( - events->friend_message, new_friend_message_capacity * sizeof(Tox_Event_Friend_Message)); + return event->type == TOX_EVENT_FRIEND_MESSAGE ? event->data.friend_message : nullptr; +} - if (new_friend_message == nullptr) { - return nullptr; - } +Tox_Event_Friend_Message *tox_event_friend_message_new(const Memory *mem) +{ + Tox_Event_Friend_Message *const friend_message = + (Tox_Event_Friend_Message *)mem_alloc(mem, sizeof(Tox_Event_Friend_Message)); - events->friend_message = new_friend_message; - events->friend_message_capacity = new_friend_message_capacity; + if (friend_message == nullptr) { + return nullptr; } - Tox_Event_Friend_Message *const friend_message = &events->friend_message[events->friend_message_size]; tox_event_friend_message_construct(friend_message); - ++events->friend_message_size; return friend_message; } -void tox_events_clear_friend_message(Tox_Events *events) +void tox_event_friend_message_free(Tox_Event_Friend_Message *friend_message, const Memory *mem) { - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->friend_message_size; ++i) { - tox_event_friend_message_destruct(&events->friend_message[i]); + if (friend_message != nullptr) { + tox_event_friend_message_destruct(friend_message, mem); } - - free(events->friend_message); - events->friend_message = nullptr; - events->friend_message_size = 0; - events->friend_message_capacity = 0; + mem_delete(mem, friend_message); } -uint32_t tox_events_get_friend_message_size(const Tox_Events *events) +non_null() +static Tox_Event_Friend_Message *tox_events_add_friend_message(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Friend_Message *const friend_message = tox_event_friend_message_new(mem); + + if (friend_message == nullptr) { + return nullptr; } - return events->friend_message_size; -} + Tox_Event event; + event.type = TOX_EVENT_FRIEND_MESSAGE; + event.data.friend_message = friend_message; -const Tox_Event_Friend_Message *tox_events_get_friend_message(const Tox_Events *events, uint32_t index) -{ - assert(index < events->friend_message_size); - assert(events->friend_message != nullptr); - return &events->friend_message[index]; + tox_events_add(events, &event); + return friend_message; } -bool tox_events_pack_friend_message(const Tox_Events *events, Bin_Pack *bp) +bool tox_event_friend_message_unpack( + Tox_Event_Friend_Message **event, Bin_Unpack *bu, const Memory *mem) { - const uint32_t size = tox_events_get_friend_message_size(events); + assert(event != nullptr); + *event = tox_event_friend_message_new(mem); - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_message_pack(tox_events_get_friend_message(events, i), bp)) { - return false; - } + if (*event == nullptr) { + return false; } - return true; + + return tox_event_friend_message_unpack_into(*event, bu); } -bool tox_events_unpack_friend_message(Tox_Events *events, Bin_Unpack *bu) +non_null() +static Tox_Event_Friend_Message *tox_event_friend_message_alloc(void *user_data) { - Tox_Event_Friend_Message *event = tox_events_add_friend_message(events); + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); - if (event == nullptr) { - return false; + if (state->events == nullptr) { + return nullptr; } - return tox_event_friend_message_unpack(event, bu); + Tox_Event_Friend_Message *friend_message = tox_events_add_friend_message(state->events, state->mem); + + if (friend_message == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return friend_message; } @@ -226,20 +221,12 @@ bool tox_events_unpack_friend_message(Tox_Events *events, Bin_Unpack *bu) *****************************************************/ -void tox_events_handle_friend_message(Tox *tox, uint32_t friend_number, Tox_Message_Type type, const uint8_t *message, - size_t length, void *user_data) +void tox_events_handle_friend_message(Tox *tox, uint32_t friend_number, Tox_Message_Type type, const uint8_t *message, size_t length, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Message *friend_message = tox_events_add_friend_message(state->events); + Tox_Event_Friend_Message *friend_message = tox_event_friend_message_alloc(user_data); if (friend_message == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/friend_name.c b/toxcore/events/friend_name.c index 6dfc3ddf0e..53fa9d8550 100644 --- a/toxcore/events/friend_name.c +++ b/toxcore/events/friend_name.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,6 +11,7 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" @@ -28,19 +29,6 @@ struct Tox_Event_Friend_Name { uint32_t name_length; }; -non_null() -static void tox_event_friend_name_construct(Tox_Event_Friend_Name *friend_name) -{ - *friend_name = (Tox_Event_Friend_Name) { - 0 - }; -} -non_null() -static void tox_event_friend_name_destruct(Tox_Event_Friend_Name *friend_name) -{ - free(friend_name->name); -} - non_null() static void tox_event_friend_name_set_friend_number(Tox_Event_Friend_Name *friend_name, uint32_t friend_number) @@ -55,8 +43,8 @@ uint32_t tox_event_friend_name_get_friend_number(const Tox_Event_Friend_Name *fr } non_null() -static bool tox_event_friend_name_set_name(Tox_Event_Friend_Name *friend_name, const uint8_t *name, - uint32_t name_length) +static bool tox_event_friend_name_set_name(Tox_Event_Friend_Name *friend_name, + const uint8_t *name, uint32_t name_length) { assert(friend_name != nullptr); @@ -89,7 +77,19 @@ const uint8_t *tox_event_friend_name_get_name(const Tox_Event_Friend_Name *frien } non_null() -static bool tox_event_friend_name_pack( +static void tox_event_friend_name_construct(Tox_Event_Friend_Name *friend_name) +{ + *friend_name = (Tox_Event_Friend_Name) { + 0 + }; +} +non_null() +static void tox_event_friend_name_destruct(Tox_Event_Friend_Name *friend_name, const Memory *mem) +{ + free(friend_name->name); +} + +bool tox_event_friend_name_pack( const Tox_Event_Friend_Name *event, Bin_Pack *bp) { assert(event != nullptr); @@ -101,7 +101,7 @@ static bool tox_event_friend_name_pack( } non_null() -static bool tox_event_friend_name_unpack( +static bool tox_event_friend_name_unpack_into( Tox_Event_Friend_Name *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -116,90 +116,84 @@ static bool tox_event_friend_name_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Friend_Name *tox_events_add_friend_name(Tox_Events *events) +const Tox_Event_Friend_Name *tox_event_get_friend_name(const Tox_Event *event) { - if (events->friend_name_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_name_size == events->friend_name_capacity) { - const uint32_t new_friend_name_capacity = events->friend_name_capacity * 2 + 1; - Tox_Event_Friend_Name *new_friend_name = (Tox_Event_Friend_Name *)realloc( - events->friend_name, new_friend_name_capacity * sizeof(Tox_Event_Friend_Name)); + return event->type == TOX_EVENT_FRIEND_NAME ? event->data.friend_name : nullptr; +} - if (new_friend_name == nullptr) { - return nullptr; - } +Tox_Event_Friend_Name *tox_event_friend_name_new(const Memory *mem) +{ + Tox_Event_Friend_Name *const friend_name = + (Tox_Event_Friend_Name *)mem_alloc(mem, sizeof(Tox_Event_Friend_Name)); - events->friend_name = new_friend_name; - events->friend_name_capacity = new_friend_name_capacity; + if (friend_name == nullptr) { + return nullptr; } - Tox_Event_Friend_Name *const friend_name = &events->friend_name[events->friend_name_size]; tox_event_friend_name_construct(friend_name); - ++events->friend_name_size; return friend_name; } -void tox_events_clear_friend_name(Tox_Events *events) +void tox_event_friend_name_free(Tox_Event_Friend_Name *friend_name, const Memory *mem) { - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->friend_name_size; ++i) { - tox_event_friend_name_destruct(&events->friend_name[i]); + if (friend_name != nullptr) { + tox_event_friend_name_destruct(friend_name, mem); } - - free(events->friend_name); - events->friend_name = nullptr; - events->friend_name_size = 0; - events->friend_name_capacity = 0; + mem_delete(mem, friend_name); } -uint32_t tox_events_get_friend_name_size(const Tox_Events *events) +non_null() +static Tox_Event_Friend_Name *tox_events_add_friend_name(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Friend_Name *const friend_name = tox_event_friend_name_new(mem); + + if (friend_name == nullptr) { + return nullptr; } - return events->friend_name_size; -} + Tox_Event event; + event.type = TOX_EVENT_FRIEND_NAME; + event.data.friend_name = friend_name; -const Tox_Event_Friend_Name *tox_events_get_friend_name(const Tox_Events *events, uint32_t index) -{ - assert(index < events->friend_name_size); - assert(events->friend_name != nullptr); - return &events->friend_name[index]; + tox_events_add(events, &event); + return friend_name; } -bool tox_events_pack_friend_name(const Tox_Events *events, Bin_Pack *bp) +bool tox_event_friend_name_unpack( + Tox_Event_Friend_Name **event, Bin_Unpack *bu, const Memory *mem) { - const uint32_t size = tox_events_get_friend_name_size(events); + assert(event != nullptr); + *event = tox_event_friend_name_new(mem); - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_name_pack(tox_events_get_friend_name(events, i), bp)) { - return false; - } + if (*event == nullptr) { + return false; } - return true; + + return tox_event_friend_name_unpack_into(*event, bu); } -bool tox_events_unpack_friend_name(Tox_Events *events, Bin_Unpack *bu) +non_null() +static Tox_Event_Friend_Name *tox_event_friend_name_alloc(void *user_data) { - Tox_Event_Friend_Name *event = tox_events_add_friend_name(events); + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); - if (event == nullptr) { - return false; + if (state->events == nullptr) { + return nullptr; } - return tox_event_friend_name_unpack(event, bu); + Tox_Event_Friend_Name *friend_name = tox_events_add_friend_name(state->events, state->mem); + + if (friend_name == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return friend_name; } @@ -211,19 +205,11 @@ bool tox_events_unpack_friend_name(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_friend_name(Tox *tox, uint32_t friend_number, const uint8_t *name, size_t length, - void *user_data) + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Name *friend_name = tox_events_add_friend_name(state->events); + Tox_Event_Friend_Name *friend_name = tox_event_friend_name_alloc(user_data); if (friend_name == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/friend_read_receipt.c b/toxcore/events/friend_read_receipt.c index 0a8cddb48a..8c88b4177d 100644 --- a/toxcore/events/friend_read_receipt.c +++ b/toxcore/events/friend_read_receipt.c @@ -1,15 +1,15 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" @@ -26,19 +26,6 @@ struct Tox_Event_Friend_Read_Receipt { uint32_t message_id; }; -non_null() -static void tox_event_friend_read_receipt_construct(Tox_Event_Friend_Read_Receipt *friend_read_receipt) -{ - *friend_read_receipt = (Tox_Event_Friend_Read_Receipt) { - 0 - }; -} -non_null() -static void tox_event_friend_read_receipt_destruct(Tox_Event_Friend_Read_Receipt *friend_read_receipt) -{ - return; -} - non_null() static void tox_event_friend_read_receipt_set_friend_number(Tox_Event_Friend_Read_Receipt *friend_read_receipt, uint32_t friend_number) @@ -66,7 +53,19 @@ uint32_t tox_event_friend_read_receipt_get_message_id(const Tox_Event_Friend_Rea } non_null() -static bool tox_event_friend_read_receipt_pack( +static void tox_event_friend_read_receipt_construct(Tox_Event_Friend_Read_Receipt *friend_read_receipt) +{ + *friend_read_receipt = (Tox_Event_Friend_Read_Receipt) { + 0 + }; +} +non_null() +static void tox_event_friend_read_receipt_destruct(Tox_Event_Friend_Read_Receipt *friend_read_receipt, const Memory *mem) +{ + return; +} + +bool tox_event_friend_read_receipt_pack( const Tox_Event_Friend_Read_Receipt *event, Bin_Pack *bp) { assert(event != nullptr); @@ -78,7 +77,7 @@ static bool tox_event_friend_read_receipt_pack( } non_null() -static bool tox_event_friend_read_receipt_unpack( +static bool tox_event_friend_read_receipt_unpack_into( Tox_Event_Friend_Read_Receipt *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -93,91 +92,84 @@ static bool tox_event_friend_read_receipt_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Friend_Read_Receipt *tox_events_add_friend_read_receipt(Tox_Events *events) +const Tox_Event_Friend_Read_Receipt *tox_event_get_friend_read_receipt(const Tox_Event *event) { - if (events->friend_read_receipt_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_read_receipt_size == events->friend_read_receipt_capacity) { - const uint32_t new_friend_read_receipt_capacity = events->friend_read_receipt_capacity * 2 + 1; - Tox_Event_Friend_Read_Receipt *new_friend_read_receipt = (Tox_Event_Friend_Read_Receipt *)realloc( - events->friend_read_receipt, new_friend_read_receipt_capacity * sizeof(Tox_Event_Friend_Read_Receipt)); + return event->type == TOX_EVENT_FRIEND_READ_RECEIPT ? event->data.friend_read_receipt : nullptr; +} - if (new_friend_read_receipt == nullptr) { - return nullptr; - } +Tox_Event_Friend_Read_Receipt *tox_event_friend_read_receipt_new(const Memory *mem) +{ + Tox_Event_Friend_Read_Receipt *const friend_read_receipt = + (Tox_Event_Friend_Read_Receipt *)mem_alloc(mem, sizeof(Tox_Event_Friend_Read_Receipt)); - events->friend_read_receipt = new_friend_read_receipt; - events->friend_read_receipt_capacity = new_friend_read_receipt_capacity; + if (friend_read_receipt == nullptr) { + return nullptr; } - Tox_Event_Friend_Read_Receipt *const friend_read_receipt = - &events->friend_read_receipt[events->friend_read_receipt_size]; tox_event_friend_read_receipt_construct(friend_read_receipt); - ++events->friend_read_receipt_size; return friend_read_receipt; } -void tox_events_clear_friend_read_receipt(Tox_Events *events) +void tox_event_friend_read_receipt_free(Tox_Event_Friend_Read_Receipt *friend_read_receipt, const Memory *mem) { - if (events == nullptr) { - return; + if (friend_read_receipt != nullptr) { + tox_event_friend_read_receipt_destruct(friend_read_receipt, mem); } - - for (uint32_t i = 0; i < events->friend_read_receipt_size; ++i) { - tox_event_friend_read_receipt_destruct(&events->friend_read_receipt[i]); - } - - free(events->friend_read_receipt); - events->friend_read_receipt = nullptr; - events->friend_read_receipt_size = 0; - events->friend_read_receipt_capacity = 0; + mem_delete(mem, friend_read_receipt); } -uint32_t tox_events_get_friend_read_receipt_size(const Tox_Events *events) +non_null() +static Tox_Event_Friend_Read_Receipt *tox_events_add_friend_read_receipt(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Friend_Read_Receipt *const friend_read_receipt = tox_event_friend_read_receipt_new(mem); + + if (friend_read_receipt == nullptr) { + return nullptr; } - return events->friend_read_receipt_size; -} + Tox_Event event; + event.type = TOX_EVENT_FRIEND_READ_RECEIPT; + event.data.friend_read_receipt = friend_read_receipt; -const Tox_Event_Friend_Read_Receipt *tox_events_get_friend_read_receipt(const Tox_Events *events, uint32_t index) -{ - assert(index < events->friend_read_receipt_size); - assert(events->friend_read_receipt != nullptr); - return &events->friend_read_receipt[index]; + tox_events_add(events, &event); + return friend_read_receipt; } -bool tox_events_pack_friend_read_receipt(const Tox_Events *events, Bin_Pack *bp) +bool tox_event_friend_read_receipt_unpack( + Tox_Event_Friend_Read_Receipt **event, Bin_Unpack *bu, const Memory *mem) { - const uint32_t size = tox_events_get_friend_read_receipt_size(events); + assert(event != nullptr); + *event = tox_event_friend_read_receipt_new(mem); - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_read_receipt_pack(tox_events_get_friend_read_receipt(events, i), bp)) { - return false; - } + if (*event == nullptr) { + return false; } - return true; + + return tox_event_friend_read_receipt_unpack_into(*event, bu); } -bool tox_events_unpack_friend_read_receipt(Tox_Events *events, Bin_Unpack *bu) +non_null() +static Tox_Event_Friend_Read_Receipt *tox_event_friend_read_receipt_alloc(void *user_data) { - Tox_Event_Friend_Read_Receipt *event = tox_events_add_friend_read_receipt(events); + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); - if (event == nullptr) { - return false; + if (state->events == nullptr) { + return nullptr; } - return tox_event_friend_read_receipt_unpack(event, bu); + Tox_Event_Friend_Read_Receipt *friend_read_receipt = tox_events_add_friend_read_receipt(state->events, state->mem); + + if (friend_read_receipt == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return friend_read_receipt; } @@ -188,19 +180,12 @@ bool tox_events_unpack_friend_read_receipt(Tox_Events *events, Bin_Unpack *bu) *****************************************************/ -void tox_events_handle_friend_read_receipt(Tox *tox, uint32_t friend_number, uint32_t message_id, void *user_data) +void tox_events_handle_friend_read_receipt(Tox *tox, uint32_t friend_number, uint32_t message_id, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Read_Receipt *friend_read_receipt = tox_events_add_friend_read_receipt(state->events); + Tox_Event_Friend_Read_Receipt *friend_read_receipt = tox_event_friend_read_receipt_alloc(user_data); if (friend_read_receipt == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/friend_request.c b/toxcore/events/friend_request.c index 401085803d..4a1936085d 100644 --- a/toxcore/events/friend_request.c +++ b/toxcore/events/friend_request.c @@ -5,14 +5,15 @@ #include "events_alloc.h" #include -#include #include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_private.h" /***************************************************** @@ -28,19 +29,6 @@ struct Tox_Event_Friend_Request { uint32_t message_length; }; -non_null() -static void tox_event_friend_request_construct(Tox_Event_Friend_Request *friend_request) -{ - *friend_request = (Tox_Event_Friend_Request) { - 0 - }; -} -non_null() -static void tox_event_friend_request_destruct(Tox_Event_Friend_Request *friend_request) -{ - free(friend_request->message); -} - non_null() static bool tox_event_friend_request_set_public_key(Tox_Event_Friend_Request *friend_request, const uint8_t *public_key) { @@ -56,18 +44,18 @@ const uint8_t *tox_event_friend_request_get_public_key(const Tox_Event_Friend_Re } non_null() -static bool tox_event_friend_request_set_message(Tox_Event_Friend_Request *friend_request, const uint8_t *message, - uint32_t message_length) +static bool tox_event_friend_request_set_message(Tox_Event_Friend_Request *friend_request, + const uint8_t *message, uint32_t message_length, const Memory *mem) { assert(friend_request != nullptr); if (friend_request->message != nullptr) { - free(friend_request->message); + mem_delete(mem, friend_request->message); friend_request->message = nullptr; friend_request->message_length = 0; } - uint8_t *message_copy = (uint8_t *)malloc(message_length); + uint8_t *message_copy = (uint8_t *)mem_balloc(mem, message_length); if (message_copy == nullptr) { return false; @@ -90,7 +78,19 @@ const uint8_t *tox_event_friend_request_get_message(const Tox_Event_Friend_Reque } non_null() -static bool tox_event_friend_request_pack( +static void tox_event_friend_request_construct(Tox_Event_Friend_Request *friend_request) +{ + *friend_request = (Tox_Event_Friend_Request) { + 0 + }; +} +non_null() +static void tox_event_friend_request_destruct(Tox_Event_Friend_Request *friend_request, const Memory *mem) +{ + mem_delete(mem, friend_request->message); +} + +bool tox_event_friend_request_pack( const Tox_Event_Friend_Request *event, Bin_Pack *bp) { assert(event != nullptr); @@ -102,7 +102,7 @@ static bool tox_event_friend_request_pack( } non_null() -static bool tox_event_friend_request_unpack( +static bool tox_event_friend_request_unpack_into( Tox_Event_Friend_Request *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -114,93 +114,81 @@ static bool tox_event_friend_request_unpack( && bin_unpack_bin(bu, &event->message, &event->message_length); } - -/***************************************************** - * - * :: add/clear/get - * - *****************************************************/ - - -non_null() -static Tox_Event_Friend_Request *tox_events_add_friend_request(Tox_Events *events) +const Tox_Event_Friend_Request *tox_event_get_friend_request( + const Tox_Event *event) { - if (events->friend_request_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_request_size == events->friend_request_capacity) { - const uint32_t new_friend_request_capacity = events->friend_request_capacity * 2 + 1; - Tox_Event_Friend_Request *new_friend_request = (Tox_Event_Friend_Request *)realloc( - events->friend_request, new_friend_request_capacity * sizeof(Tox_Event_Friend_Request)); + return event->type == TOX_EVENT_FRIEND_REQUEST ? event->data.friend_request : nullptr; +} - if (new_friend_request == nullptr) { - return nullptr; - } +Tox_Event_Friend_Request *tox_event_friend_request_new(const Memory *mem) +{ + Tox_Event_Friend_Request *const friend_request = + (Tox_Event_Friend_Request *)mem_alloc(mem, sizeof(Tox_Event_Friend_Request)); - events->friend_request = new_friend_request; - events->friend_request_capacity = new_friend_request_capacity; + if (friend_request == nullptr) { + return nullptr; } - Tox_Event_Friend_Request *const friend_request = &events->friend_request[events->friend_request_size]; tox_event_friend_request_construct(friend_request); - ++events->friend_request_size; return friend_request; } -void tox_events_clear_friend_request(Tox_Events *events) +void tox_event_friend_request_free(Tox_Event_Friend_Request *friend_request, const Memory *mem) { - if (events == nullptr) { - return; + if (friend_request != nullptr) { + tox_event_friend_request_destruct(friend_request, mem); } - - for (uint32_t i = 0; i < events->friend_request_size; ++i) { - tox_event_friend_request_destruct(&events->friend_request[i]); - } - - free(events->friend_request); - events->friend_request = nullptr; - events->friend_request_size = 0; - events->friend_request_capacity = 0; + mem_delete(mem, friend_request); } -uint32_t tox_events_get_friend_request_size(const Tox_Events *events) +non_null() +static Tox_Event_Friend_Request *tox_events_add_friend_request(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Friend_Request *const friend_request = tox_event_friend_request_new(mem); + + if (friend_request == nullptr) { + return nullptr; } - return events->friend_request_size; -} + Tox_Event event; + event.type = TOX_EVENT_FRIEND_REQUEST; + event.data.friend_request = friend_request; -const Tox_Event_Friend_Request *tox_events_get_friend_request(const Tox_Events *events, uint32_t index) -{ - assert(index < events->friend_request_size); - assert(events->friend_request != nullptr); - return &events->friend_request[index]; + tox_events_add(events, &event); + return friend_request; } -bool tox_events_pack_friend_request(const Tox_Events *events, Bin_Pack *bp) +bool tox_event_friend_request_unpack( + Tox_Event_Friend_Request **event, Bin_Unpack *bu, const Memory *mem) { - const uint32_t size = tox_events_get_friend_request_size(events); + assert(event != nullptr); + *event = tox_event_friend_request_new(mem); - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_request_pack(tox_events_get_friend_request(events, i), bp)) { - return false; - } + if (*event == nullptr) { + return false; } - return true; + + return tox_event_friend_request_unpack_into(*event, bu); } -bool tox_events_unpack_friend_request(Tox_Events *events, Bin_Unpack *bu) +non_null() +static Tox_Event_Friend_Request *tox_event_friend_request_alloc(void *user_data) { - Tox_Event_Friend_Request *event = tox_events_add_friend_request(events); + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); - if (event == nullptr) { - return false; + if (state->events == nullptr) { + return nullptr; } - return tox_event_friend_request_unpack(event, bu); + Tox_Event_Friend_Request *friend_request = tox_events_add_friend_request(state->events, state->mem); + + if (friend_request == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return friend_request; } @@ -214,20 +202,14 @@ bool tox_events_unpack_friend_request(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_friend_request(Tox *tox, const uint8_t *public_key, const uint8_t *message, size_t length, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Request *friend_request = tox_events_add_friend_request(state->events); + Tox_Event_Friend_Request *friend_request = tox_event_friend_request_alloc(user_data); if (friend_request == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } + const Tox_System *sys = tox_get_system(tox); + tox_event_friend_request_set_public_key(friend_request, public_key); - tox_event_friend_request_set_message(friend_request, message, length); + tox_event_friend_request_set_message(friend_request, message, length, sys->mem); } diff --git a/toxcore/events/friend_status.c b/toxcore/events/friend_status.c index 3c2a55732d..1b0a8a41b0 100644 --- a/toxcore/events/friend_status.c +++ b/toxcore/events/friend_status.c @@ -1,15 +1,15 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" @@ -27,19 +27,6 @@ struct Tox_Event_Friend_Status { Tox_User_Status status; }; -non_null() -static void tox_event_friend_status_construct(Tox_Event_Friend_Status *friend_status) -{ - *friend_status = (Tox_Event_Friend_Status) { - 0 - }; -} -non_null() -static void tox_event_friend_status_destruct(Tox_Event_Friend_Status *friend_status) -{ - return; -} - non_null() static void tox_event_friend_status_set_friend_number(Tox_Event_Friend_Status *friend_status, uint32_t friend_number) @@ -67,7 +54,19 @@ Tox_User_Status tox_event_friend_status_get_status(const Tox_Event_Friend_Status } non_null() -static bool tox_event_friend_status_pack( +static void tox_event_friend_status_construct(Tox_Event_Friend_Status *friend_status) +{ + *friend_status = (Tox_Event_Friend_Status) { + 0 + }; +} +non_null() +static void tox_event_friend_status_destruct(Tox_Event_Friend_Status *friend_status, const Memory *mem) +{ + return; +} + +bool tox_event_friend_status_pack( const Tox_Event_Friend_Status *event, Bin_Pack *bp) { assert(event != nullptr); @@ -79,7 +78,7 @@ static bool tox_event_friend_status_pack( } non_null() -static bool tox_event_friend_status_unpack( +static bool tox_event_friend_status_unpack_into( Tox_Event_Friend_Status *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -94,90 +93,84 @@ static bool tox_event_friend_status_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Friend_Status *tox_events_add_friend_status(Tox_Events *events) +const Tox_Event_Friend_Status *tox_event_get_friend_status(const Tox_Event *event) { - if (events->friend_status_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_status_size == events->friend_status_capacity) { - const uint32_t new_friend_status_capacity = events->friend_status_capacity * 2 + 1; - Tox_Event_Friend_Status *new_friend_status = (Tox_Event_Friend_Status *)realloc( - events->friend_status, new_friend_status_capacity * sizeof(Tox_Event_Friend_Status)); + return event->type == TOX_EVENT_FRIEND_STATUS ? event->data.friend_status : nullptr; +} - if (new_friend_status == nullptr) { - return nullptr; - } +Tox_Event_Friend_Status *tox_event_friend_status_new(const Memory *mem) +{ + Tox_Event_Friend_Status *const friend_status = + (Tox_Event_Friend_Status *)mem_alloc(mem, sizeof(Tox_Event_Friend_Status)); - events->friend_status = new_friend_status; - events->friend_status_capacity = new_friend_status_capacity; + if (friend_status == nullptr) { + return nullptr; } - Tox_Event_Friend_Status *const friend_status = &events->friend_status[events->friend_status_size]; tox_event_friend_status_construct(friend_status); - ++events->friend_status_size; return friend_status; } -void tox_events_clear_friend_status(Tox_Events *events) +void tox_event_friend_status_free(Tox_Event_Friend_Status *friend_status, const Memory *mem) { - if (events == nullptr) { - return; + if (friend_status != nullptr) { + tox_event_friend_status_destruct(friend_status, mem); } - - for (uint32_t i = 0; i < events->friend_status_size; ++i) { - tox_event_friend_status_destruct(&events->friend_status[i]); - } - - free(events->friend_status); - events->friend_status = nullptr; - events->friend_status_size = 0; - events->friend_status_capacity = 0; + mem_delete(mem, friend_status); } -uint32_t tox_events_get_friend_status_size(const Tox_Events *events) +non_null() +static Tox_Event_Friend_Status *tox_events_add_friend_status(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Friend_Status *const friend_status = tox_event_friend_status_new(mem); + + if (friend_status == nullptr) { + return nullptr; } - return events->friend_status_size; -} + Tox_Event event; + event.type = TOX_EVENT_FRIEND_STATUS; + event.data.friend_status = friend_status; -const Tox_Event_Friend_Status *tox_events_get_friend_status(const Tox_Events *events, uint32_t index) -{ - assert(index < events->friend_status_size); - assert(events->friend_status != nullptr); - return &events->friend_status[index]; + tox_events_add(events, &event); + return friend_status; } -bool tox_events_pack_friend_status(const Tox_Events *events, Bin_Pack *bp) +bool tox_event_friend_status_unpack( + Tox_Event_Friend_Status **event, Bin_Unpack *bu, const Memory *mem) { - const uint32_t size = tox_events_get_friend_status_size(events); + assert(event != nullptr); + *event = tox_event_friend_status_new(mem); - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_status_pack(tox_events_get_friend_status(events, i), bp)) { - return false; - } + if (*event == nullptr) { + return false; } - return true; + + return tox_event_friend_status_unpack_into(*event, bu); } -bool tox_events_unpack_friend_status(Tox_Events *events, Bin_Unpack *bu) +non_null() +static Tox_Event_Friend_Status *tox_event_friend_status_alloc(void *user_data) { - Tox_Event_Friend_Status *event = tox_events_add_friend_status(events); + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); - if (event == nullptr) { - return false; + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Friend_Status *friend_status = tox_events_add_friend_status(state->events, state->mem); + + if (friend_status == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; } - return tox_event_friend_status_unpack(event, bu); + return friend_status; } @@ -189,19 +182,11 @@ bool tox_events_unpack_friend_status(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_friend_status(Tox *tox, uint32_t friend_number, Tox_User_Status status, - void *user_data) + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Status *friend_status = tox_events_add_friend_status(state->events); + Tox_Event_Friend_Status *friend_status = tox_event_friend_status_alloc(user_data); if (friend_status == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/friend_status_message.c b/toxcore/events/friend_status_message.c index 58bb0b97a7..876a9f735f 100644 --- a/toxcore/events/friend_status_message.c +++ b/toxcore/events/friend_status_message.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,6 +11,7 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" @@ -28,19 +29,6 @@ struct Tox_Event_Friend_Status_Message { uint32_t message_length; }; -non_null() -static void tox_event_friend_status_message_construct(Tox_Event_Friend_Status_Message *friend_status_message) -{ - *friend_status_message = (Tox_Event_Friend_Status_Message) { - 0 - }; -} -non_null() -static void tox_event_friend_status_message_destruct(Tox_Event_Friend_Status_Message *friend_status_message) -{ - free(friend_status_message->message); -} - non_null() static void tox_event_friend_status_message_set_friend_number(Tox_Event_Friend_Status_Message *friend_status_message, uint32_t friend_number) @@ -77,21 +65,31 @@ static bool tox_event_friend_status_message_set_message(Tox_Event_Friend_Status_ friend_status_message->message_length = message_length; return true; } -uint32_t tox_event_friend_status_message_get_message_length(const Tox_Event_Friend_Status_Message - *friend_status_message) +uint32_t tox_event_friend_status_message_get_message_length(const Tox_Event_Friend_Status_Message *friend_status_message) { assert(friend_status_message != nullptr); return friend_status_message->message_length; } -const uint8_t *tox_event_friend_status_message_get_message(const Tox_Event_Friend_Status_Message - *friend_status_message) +const uint8_t *tox_event_friend_status_message_get_message(const Tox_Event_Friend_Status_Message *friend_status_message) { assert(friend_status_message != nullptr); return friend_status_message->message; } non_null() -static bool tox_event_friend_status_message_pack( +static void tox_event_friend_status_message_construct(Tox_Event_Friend_Status_Message *friend_status_message) +{ + *friend_status_message = (Tox_Event_Friend_Status_Message) { + 0 + }; +} +non_null() +static void tox_event_friend_status_message_destruct(Tox_Event_Friend_Status_Message *friend_status_message, const Memory *mem) +{ + free(friend_status_message->message); +} + +bool tox_event_friend_status_message_pack( const Tox_Event_Friend_Status_Message *event, Bin_Pack *bp) { assert(event != nullptr); @@ -103,7 +101,7 @@ static bool tox_event_friend_status_message_pack( } non_null() -static bool tox_event_friend_status_message_unpack( +static bool tox_event_friend_status_message_unpack_into( Tox_Event_Friend_Status_Message *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -118,91 +116,84 @@ static bool tox_event_friend_status_message_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Friend_Status_Message *tox_events_add_friend_status_message(Tox_Events *events) +const Tox_Event_Friend_Status_Message *tox_event_get_friend_status_message(const Tox_Event *event) { - if (events->friend_status_message_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_status_message_size == events->friend_status_message_capacity) { - const uint32_t new_friend_status_message_capacity = events->friend_status_message_capacity * 2 + 1; - Tox_Event_Friend_Status_Message *new_friend_status_message = (Tox_Event_Friend_Status_Message *)realloc( - events->friend_status_message, new_friend_status_message_capacity * sizeof(Tox_Event_Friend_Status_Message)); + return event->type == TOX_EVENT_FRIEND_STATUS_MESSAGE ? event->data.friend_status_message : nullptr; +} - if (new_friend_status_message == nullptr) { - return nullptr; - } +Tox_Event_Friend_Status_Message *tox_event_friend_status_message_new(const Memory *mem) +{ + Tox_Event_Friend_Status_Message *const friend_status_message = + (Tox_Event_Friend_Status_Message *)mem_alloc(mem, sizeof(Tox_Event_Friend_Status_Message)); - events->friend_status_message = new_friend_status_message; - events->friend_status_message_capacity = new_friend_status_message_capacity; + if (friend_status_message == nullptr) { + return nullptr; } - Tox_Event_Friend_Status_Message *const friend_status_message = - &events->friend_status_message[events->friend_status_message_size]; tox_event_friend_status_message_construct(friend_status_message); - ++events->friend_status_message_size; return friend_status_message; } -void tox_events_clear_friend_status_message(Tox_Events *events) +void tox_event_friend_status_message_free(Tox_Event_Friend_Status_Message *friend_status_message, const Memory *mem) { - if (events == nullptr) { - return; + if (friend_status_message != nullptr) { + tox_event_friend_status_message_destruct(friend_status_message, mem); } - - for (uint32_t i = 0; i < events->friend_status_message_size; ++i) { - tox_event_friend_status_message_destruct(&events->friend_status_message[i]); - } - - free(events->friend_status_message); - events->friend_status_message = nullptr; - events->friend_status_message_size = 0; - events->friend_status_message_capacity = 0; + mem_delete(mem, friend_status_message); } -uint32_t tox_events_get_friend_status_message_size(const Tox_Events *events) +non_null() +static Tox_Event_Friend_Status_Message *tox_events_add_friend_status_message(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Friend_Status_Message *const friend_status_message = tox_event_friend_status_message_new(mem); + + if (friend_status_message == nullptr) { + return nullptr; } - return events->friend_status_message_size; -} + Tox_Event event; + event.type = TOX_EVENT_FRIEND_STATUS_MESSAGE; + event.data.friend_status_message = friend_status_message; -const Tox_Event_Friend_Status_Message *tox_events_get_friend_status_message(const Tox_Events *events, uint32_t index) -{ - assert(index < events->friend_status_message_size); - assert(events->friend_status_message != nullptr); - return &events->friend_status_message[index]; + tox_events_add(events, &event); + return friend_status_message; } -bool tox_events_pack_friend_status_message(const Tox_Events *events, Bin_Pack *bp) +bool tox_event_friend_status_message_unpack( + Tox_Event_Friend_Status_Message **event, Bin_Unpack *bu, const Memory *mem) { - const uint32_t size = tox_events_get_friend_status_message_size(events); + assert(event != nullptr); + *event = tox_event_friend_status_message_new(mem); - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_status_message_pack(tox_events_get_friend_status_message(events, i), bp)) { - return false; - } + if (*event == nullptr) { + return false; } - return true; + + return tox_event_friend_status_message_unpack_into(*event, bu); } -bool tox_events_unpack_friend_status_message(Tox_Events *events, Bin_Unpack *bu) +non_null() +static Tox_Event_Friend_Status_Message *tox_event_friend_status_message_alloc(void *user_data) { - Tox_Event_Friend_Status_Message *event = tox_events_add_friend_status_message(events); + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); - if (event == nullptr) { - return false; + if (state->events == nullptr) { + return nullptr; } - return tox_event_friend_status_message_unpack(event, bu); + Tox_Event_Friend_Status_Message *friend_status_message = tox_events_add_friend_status_message(state->events, state->mem); + + if (friend_status_message == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return friend_status_message; } @@ -213,20 +204,12 @@ bool tox_events_unpack_friend_status_message(Tox_Events *events, Bin_Unpack *bu) *****************************************************/ -void tox_events_handle_friend_status_message(Tox *tox, uint32_t friend_number, const uint8_t *message, - size_t length, void *user_data) +void tox_events_handle_friend_status_message(Tox *tox, uint32_t friend_number, const uint8_t *message, size_t length, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Status_Message *friend_status_message = tox_events_add_friend_status_message(state->events); + Tox_Event_Friend_Status_Message *friend_status_message = tox_event_friend_status_message_alloc(user_data); if (friend_status_message == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/friend_typing.c b/toxcore/events/friend_typing.c index 64e494d741..80458e78b2 100644 --- a/toxcore/events/friend_typing.c +++ b/toxcore/events/friend_typing.c @@ -1,15 +1,15 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" @@ -26,19 +26,6 @@ struct Tox_Event_Friend_Typing { bool typing; }; -non_null() -static void tox_event_friend_typing_construct(Tox_Event_Friend_Typing *friend_typing) -{ - *friend_typing = (Tox_Event_Friend_Typing) { - 0 - }; -} -non_null() -static void tox_event_friend_typing_destruct(Tox_Event_Friend_Typing *friend_typing) -{ - return; -} - non_null() static void tox_event_friend_typing_set_friend_number(Tox_Event_Friend_Typing *friend_typing, uint32_t friend_number) @@ -53,7 +40,8 @@ uint32_t tox_event_friend_typing_get_friend_number(const Tox_Event_Friend_Typing } non_null() -static void tox_event_friend_typing_set_typing(Tox_Event_Friend_Typing *friend_typing, bool typing) +static void tox_event_friend_typing_set_typing(Tox_Event_Friend_Typing *friend_typing, + bool typing) { assert(friend_typing != nullptr); friend_typing->typing = typing; @@ -65,7 +53,19 @@ bool tox_event_friend_typing_get_typing(const Tox_Event_Friend_Typing *friend_ty } non_null() -static bool tox_event_friend_typing_pack( +static void tox_event_friend_typing_construct(Tox_Event_Friend_Typing *friend_typing) +{ + *friend_typing = (Tox_Event_Friend_Typing) { + 0 + }; +} +non_null() +static void tox_event_friend_typing_destruct(Tox_Event_Friend_Typing *friend_typing, const Memory *mem) +{ + return; +} + +bool tox_event_friend_typing_pack( const Tox_Event_Friend_Typing *event, Bin_Pack *bp) { assert(event != nullptr); @@ -77,7 +77,7 @@ static bool tox_event_friend_typing_pack( } non_null() -static bool tox_event_friend_typing_unpack( +static bool tox_event_friend_typing_unpack_into( Tox_Event_Friend_Typing *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -92,90 +92,84 @@ static bool tox_event_friend_typing_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Friend_Typing *tox_events_add_friend_typing(Tox_Events *events) +const Tox_Event_Friend_Typing *tox_event_get_friend_typing(const Tox_Event *event) { - if (events->friend_typing_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_typing_size == events->friend_typing_capacity) { - const uint32_t new_friend_typing_capacity = events->friend_typing_capacity * 2 + 1; - Tox_Event_Friend_Typing *new_friend_typing = (Tox_Event_Friend_Typing *)realloc( - events->friend_typing, new_friend_typing_capacity * sizeof(Tox_Event_Friend_Typing)); + return event->type == TOX_EVENT_FRIEND_TYPING ? event->data.friend_typing : nullptr; +} - if (new_friend_typing == nullptr) { - return nullptr; - } +Tox_Event_Friend_Typing *tox_event_friend_typing_new(const Memory *mem) +{ + Tox_Event_Friend_Typing *const friend_typing = + (Tox_Event_Friend_Typing *)mem_alloc(mem, sizeof(Tox_Event_Friend_Typing)); - events->friend_typing = new_friend_typing; - events->friend_typing_capacity = new_friend_typing_capacity; + if (friend_typing == nullptr) { + return nullptr; } - Tox_Event_Friend_Typing *const friend_typing = &events->friend_typing[events->friend_typing_size]; tox_event_friend_typing_construct(friend_typing); - ++events->friend_typing_size; return friend_typing; } -void tox_events_clear_friend_typing(Tox_Events *events) +void tox_event_friend_typing_free(Tox_Event_Friend_Typing *friend_typing, const Memory *mem) { - if (events == nullptr) { - return; + if (friend_typing != nullptr) { + tox_event_friend_typing_destruct(friend_typing, mem); } - - for (uint32_t i = 0; i < events->friend_typing_size; ++i) { - tox_event_friend_typing_destruct(&events->friend_typing[i]); - } - - free(events->friend_typing); - events->friend_typing = nullptr; - events->friend_typing_size = 0; - events->friend_typing_capacity = 0; + mem_delete(mem, friend_typing); } -uint32_t tox_events_get_friend_typing_size(const Tox_Events *events) +non_null() +static Tox_Event_Friend_Typing *tox_events_add_friend_typing(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Friend_Typing *const friend_typing = tox_event_friend_typing_new(mem); + + if (friend_typing == nullptr) { + return nullptr; } - return events->friend_typing_size; -} + Tox_Event event; + event.type = TOX_EVENT_FRIEND_TYPING; + event.data.friend_typing = friend_typing; -const Tox_Event_Friend_Typing *tox_events_get_friend_typing(const Tox_Events *events, uint32_t index) -{ - assert(index < events->friend_typing_size); - assert(events->friend_typing != nullptr); - return &events->friend_typing[index]; + tox_events_add(events, &event); + return friend_typing; } -bool tox_events_pack_friend_typing(const Tox_Events *events, Bin_Pack *bp) +bool tox_event_friend_typing_unpack( + Tox_Event_Friend_Typing **event, Bin_Unpack *bu, const Memory *mem) { - const uint32_t size = tox_events_get_friend_typing_size(events); + assert(event != nullptr); + *event = tox_event_friend_typing_new(mem); - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_typing_pack(tox_events_get_friend_typing(events, i), bp)) { - return false; - } + if (*event == nullptr) { + return false; } - return true; + + return tox_event_friend_typing_unpack_into(*event, bu); } -bool tox_events_unpack_friend_typing(Tox_Events *events, Bin_Unpack *bu) +non_null() +static Tox_Event_Friend_Typing *tox_event_friend_typing_alloc(void *user_data) { - Tox_Event_Friend_Typing *event = tox_events_add_friend_typing(events); + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); - if (event == nullptr) { - return false; + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Friend_Typing *friend_typing = tox_events_add_friend_typing(state->events, state->mem); + + if (friend_typing == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; } - return tox_event_friend_typing_unpack(event, bu); + return friend_typing; } @@ -186,19 +180,12 @@ bool tox_events_unpack_friend_typing(Tox_Events *events, Bin_Unpack *bu) *****************************************************/ -void tox_events_handle_friend_typing(Tox *tox, uint32_t friend_number, bool typing, void *user_data) +void tox_events_handle_friend_typing(Tox *tox, uint32_t friend_number, bool typing, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Typing *friend_typing = tox_events_add_friend_typing(state->events); + Tox_Event_Friend_Typing *friend_typing = tox_event_friend_typing_alloc(user_data); if (friend_typing == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/group_custom_packet.c b/toxcore/events/group_custom_packet.c new file mode 100644 index 0000000000..a265ee4a38 --- /dev/null +++ b/toxcore/events/group_custom_packet.c @@ -0,0 +1,235 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2023-2024 The TokTok team. + */ + +#include "events_alloc.h" + +#include +#include +#include + +#include "../bin_pack.h" +#include "../bin_unpack.h" +#include "../ccompat.h" +#include "../mem.h" +#include "../tox.h" +#include "../tox_events.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; + } + + uint8_t *data_copy = (uint8_t *)malloc(data_length); + + if (data_copy == nullptr) { + return false; + } + + memcpy(data_copy, data, data_length); + group_custom_packet->data = data_copy; + 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; +} + +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); +} diff --git a/toxcore/events/group_custom_private_packet.c b/toxcore/events/group_custom_private_packet.c new file mode 100644 index 0000000000..bcb7cb3ecd --- /dev/null +++ b/toxcore/events/group_custom_private_packet.c @@ -0,0 +1,235 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2023-2024 The TokTok team. + */ + +#include "events_alloc.h" + +#include +#include +#include + +#include "../bin_pack.h" +#include "../bin_unpack.h" +#include "../ccompat.h" +#include "../mem.h" +#include "../tox.h" +#include "../tox_events.h" + + +/***************************************************** + * + * :: struct and accessors + * + *****************************************************/ + + +struct Tox_Event_Group_Custom_Private_Packet { + uint32_t group_number; + uint32_t peer_id; + uint8_t *data; + uint32_t data_length; +}; + +non_null() +static void tox_event_group_custom_private_packet_set_group_number(Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet, + uint32_t group_number) +{ + assert(group_custom_private_packet != nullptr); + group_custom_private_packet->group_number = group_number; +} +uint32_t tox_event_group_custom_private_packet_get_group_number(const Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet) +{ + assert(group_custom_private_packet != nullptr); + return group_custom_private_packet->group_number; +} + +non_null() +static void tox_event_group_custom_private_packet_set_peer_id(Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet, + uint32_t peer_id) +{ + assert(group_custom_private_packet != nullptr); + group_custom_private_packet->peer_id = peer_id; +} +uint32_t tox_event_group_custom_private_packet_get_peer_id(const Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet) +{ + assert(group_custom_private_packet != nullptr); + return group_custom_private_packet->peer_id; +} + +non_null() +static bool tox_event_group_custom_private_packet_set_data(Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet, + const uint8_t *data, uint32_t data_length) +{ + assert(group_custom_private_packet != nullptr); + + if (group_custom_private_packet->data != nullptr) { + free(group_custom_private_packet->data); + group_custom_private_packet->data = nullptr; + group_custom_private_packet->data_length = 0; + } + + uint8_t *data_copy = (uint8_t *)malloc(data_length); + + if (data_copy == nullptr) { + return false; + } + + memcpy(data_copy, data, data_length); + group_custom_private_packet->data = data_copy; + group_custom_private_packet->data_length = data_length; + return true; +} +uint32_t tox_event_group_custom_private_packet_get_data_length(const Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet) +{ + assert(group_custom_private_packet != nullptr); + return group_custom_private_packet->data_length; +} +const uint8_t *tox_event_group_custom_private_packet_get_data(const Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet) +{ + assert(group_custom_private_packet != nullptr); + return group_custom_private_packet->data; +} + +non_null() +static void tox_event_group_custom_private_packet_construct(Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet) +{ + *group_custom_private_packet = (Tox_Event_Group_Custom_Private_Packet) { + 0 + }; +} +non_null() +static void tox_event_group_custom_private_packet_destruct(Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet, const Memory *mem) +{ + free(group_custom_private_packet->data); +} + +bool tox_event_group_custom_private_packet_pack( + const Tox_Event_Group_Custom_Private_Packet *event, Bin_Pack *bp) +{ + assert(event != nullptr); + return bin_pack_array(bp, 2) + && bin_pack_u32(bp, TOX_EVENT_GROUP_CUSTOM_PRIVATE_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_private_packet_unpack_into( + Tox_Event_Group_Custom_Private_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_Private_Packet *tox_event_get_group_custom_private_packet(const Tox_Event *event) +{ + return event->type == TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET ? event->data.group_custom_private_packet : nullptr; +} + +Tox_Event_Group_Custom_Private_Packet *tox_event_group_custom_private_packet_new(const Memory *mem) +{ + Tox_Event_Group_Custom_Private_Packet *const group_custom_private_packet = + (Tox_Event_Group_Custom_Private_Packet *)mem_alloc(mem, sizeof(Tox_Event_Group_Custom_Private_Packet)); + + if (group_custom_private_packet == nullptr) { + return nullptr; + } + + tox_event_group_custom_private_packet_construct(group_custom_private_packet); + return group_custom_private_packet; +} + +void tox_event_group_custom_private_packet_free(Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet, const Memory *mem) +{ + if (group_custom_private_packet != nullptr) { + tox_event_group_custom_private_packet_destruct(group_custom_private_packet, mem); + } + mem_delete(mem, group_custom_private_packet); +} + +non_null() +static Tox_Event_Group_Custom_Private_Packet *tox_events_add_group_custom_private_packet(Tox_Events *events, const Memory *mem) +{ + Tox_Event_Group_Custom_Private_Packet *const group_custom_private_packet = tox_event_group_custom_private_packet_new(mem); + + if (group_custom_private_packet == nullptr) { + return nullptr; + } + + Tox_Event event; + event.type = TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET; + event.data.group_custom_private_packet = group_custom_private_packet; + + tox_events_add(events, &event); + return group_custom_private_packet; +} + +bool tox_event_group_custom_private_packet_unpack( + Tox_Event_Group_Custom_Private_Packet **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_custom_private_packet_new(mem); + + if (*event == nullptr) { + return false; + } + + return tox_event_group_custom_private_packet_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Custom_Private_Packet *tox_event_group_custom_private_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_Private_Packet *group_custom_private_packet = tox_events_add_group_custom_private_packet(state->events, state->mem); + + if (group_custom_private_packet == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_custom_private_packet; +} + + +/***************************************************** + * + * :: event handler + * + *****************************************************/ + + +void tox_events_handle_group_custom_private_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_Private_Packet *group_custom_private_packet = tox_event_group_custom_private_packet_alloc(user_data); + + if (group_custom_private_packet == nullptr) { + return; + } + + tox_event_group_custom_private_packet_set_group_number(group_custom_private_packet, group_number); + tox_event_group_custom_private_packet_set_peer_id(group_custom_private_packet, peer_id); + tox_event_group_custom_private_packet_set_data(group_custom_private_packet, data, length); +} diff --git a/toxcore/events/group_invite.c b/toxcore/events/group_invite.c new file mode 100644 index 0000000000..97fad95c83 --- /dev/null +++ b/toxcore/events/group_invite.c @@ -0,0 +1,258 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2023-2024 The TokTok team. + */ + +#include "events_alloc.h" + +#include +#include +#include + +#include "../bin_pack.h" +#include "../bin_unpack.h" +#include "../ccompat.h" +#include "../mem.h" +#include "../tox.h" +#include "../tox_events.h" + + +/***************************************************** + * + * :: struct and accessors + * + *****************************************************/ + + +struct Tox_Event_Group_Invite { + uint32_t friend_number; + uint8_t *invite_data; + uint32_t invite_data_length; + uint8_t *group_name; + uint32_t group_name_length; +}; + +non_null() +static void tox_event_group_invite_set_friend_number(Tox_Event_Group_Invite *group_invite, + uint32_t friend_number) +{ + assert(group_invite != nullptr); + group_invite->friend_number = friend_number; +} +uint32_t tox_event_group_invite_get_friend_number(const Tox_Event_Group_Invite *group_invite) +{ + assert(group_invite != nullptr); + return group_invite->friend_number; +} + +non_null() +static bool tox_event_group_invite_set_invite_data(Tox_Event_Group_Invite *group_invite, + const uint8_t *invite_data, uint32_t invite_data_length) +{ + assert(group_invite != nullptr); + + if (group_invite->invite_data != nullptr) { + free(group_invite->invite_data); + group_invite->invite_data = nullptr; + group_invite->invite_data_length = 0; + } + + uint8_t *invite_data_copy = (uint8_t *)malloc(invite_data_length); + + if (invite_data_copy == nullptr) { + return false; + } + + memcpy(invite_data_copy, invite_data, invite_data_length); + group_invite->invite_data = invite_data_copy; + group_invite->invite_data_length = invite_data_length; + return true; +} +uint32_t tox_event_group_invite_get_invite_data_length(const Tox_Event_Group_Invite *group_invite) +{ + assert(group_invite != nullptr); + return group_invite->invite_data_length; +} +const uint8_t *tox_event_group_invite_get_invite_data(const Tox_Event_Group_Invite *group_invite) +{ + assert(group_invite != nullptr); + return group_invite->invite_data; +} + +non_null() +static bool tox_event_group_invite_set_group_name(Tox_Event_Group_Invite *group_invite, + const uint8_t *group_name, uint32_t group_name_length) +{ + assert(group_invite != nullptr); + + if (group_invite->group_name != nullptr) { + free(group_invite->group_name); + group_invite->group_name = nullptr; + group_invite->group_name_length = 0; + } + + uint8_t *group_name_copy = (uint8_t *)malloc(group_name_length); + + if (group_name_copy == nullptr) { + return false; + } + + memcpy(group_name_copy, group_name, group_name_length); + group_invite->group_name = group_name_copy; + group_invite->group_name_length = group_name_length; + return true; +} +uint32_t tox_event_group_invite_get_group_name_length(const Tox_Event_Group_Invite *group_invite) +{ + assert(group_invite != nullptr); + return group_invite->group_name_length; +} +const uint8_t *tox_event_group_invite_get_group_name(const Tox_Event_Group_Invite *group_invite) +{ + assert(group_invite != nullptr); + return group_invite->group_name; +} + +non_null() +static void tox_event_group_invite_construct(Tox_Event_Group_Invite *group_invite) +{ + *group_invite = (Tox_Event_Group_Invite) { + 0 + }; +} +non_null() +static void tox_event_group_invite_destruct(Tox_Event_Group_Invite *group_invite, const Memory *mem) +{ + free(group_invite->invite_data); + free(group_invite->group_name); +} + +bool tox_event_group_invite_pack( + const Tox_Event_Group_Invite *event, Bin_Pack *bp) +{ + assert(event != nullptr); + return bin_pack_array(bp, 2) + && bin_pack_u32(bp, TOX_EVENT_GROUP_INVITE) + && bin_pack_array(bp, 3) + && bin_pack_u32(bp, event->friend_number) + && bin_pack_bin(bp, event->invite_data, event->invite_data_length) + && bin_pack_bin(bp, event->group_name, event->group_name_length); +} + +non_null() +static bool tox_event_group_invite_unpack_into( + Tox_Event_Group_Invite *event, Bin_Unpack *bu) +{ + assert(event != nullptr); + if (!bin_unpack_array_fixed(bu, 3, nullptr)) { + return false; + } + + return bin_unpack_u32(bu, &event->friend_number) + && bin_unpack_bin(bu, &event->invite_data, &event->invite_data_length) + && bin_unpack_bin(bu, &event->group_name, &event->group_name_length); +} + + +/***************************************************** + * + * :: new/free/add/get/size/unpack + * + *****************************************************/ + +const Tox_Event_Group_Invite *tox_event_get_group_invite(const Tox_Event *event) +{ + return event->type == TOX_EVENT_GROUP_INVITE ? event->data.group_invite : nullptr; +} + +Tox_Event_Group_Invite *tox_event_group_invite_new(const Memory *mem) +{ + Tox_Event_Group_Invite *const group_invite = + (Tox_Event_Group_Invite *)mem_alloc(mem, sizeof(Tox_Event_Group_Invite)); + + if (group_invite == nullptr) { + return nullptr; + } + + tox_event_group_invite_construct(group_invite); + return group_invite; +} + +void tox_event_group_invite_free(Tox_Event_Group_Invite *group_invite, const Memory *mem) +{ + if (group_invite != nullptr) { + tox_event_group_invite_destruct(group_invite, mem); + } + mem_delete(mem, group_invite); +} + +non_null() +static Tox_Event_Group_Invite *tox_events_add_group_invite(Tox_Events *events, const Memory *mem) +{ + Tox_Event_Group_Invite *const group_invite = tox_event_group_invite_new(mem); + + if (group_invite == nullptr) { + return nullptr; + } + + Tox_Event event; + event.type = TOX_EVENT_GROUP_INVITE; + event.data.group_invite = group_invite; + + tox_events_add(events, &event); + return group_invite; +} + +bool tox_event_group_invite_unpack( + Tox_Event_Group_Invite **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_invite_new(mem); + + if (*event == nullptr) { + return false; + } + + return tox_event_group_invite_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Invite *tox_event_group_invite_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_Invite *group_invite = tox_events_add_group_invite(state->events, state->mem); + + if (group_invite == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_invite; +} + + +/***************************************************** + * + * :: event handler + * + *****************************************************/ + + +void tox_events_handle_group_invite(Tox *tox, uint32_t friend_number, const uint8_t *invite_data, size_t length, const uint8_t *group_name, size_t group_name_length, + void *user_data) +{ + Tox_Event_Group_Invite *group_invite = tox_event_group_invite_alloc(user_data); + + if (group_invite == nullptr) { + return; + } + + tox_event_group_invite_set_friend_number(group_invite, friend_number); + tox_event_group_invite_set_invite_data(group_invite, invite_data, length); + tox_event_group_invite_set_group_name(group_invite, group_name, group_name_length); +} diff --git a/toxcore/events/group_join_fail.c b/toxcore/events/group_join_fail.c new file mode 100644 index 0000000000..10a9516dca --- /dev/null +++ b/toxcore/events/group_join_fail.c @@ -0,0 +1,195 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2023-2024 The TokTok team. + */ + +#include "events_alloc.h" + +#include + +#include "../bin_pack.h" +#include "../bin_unpack.h" +#include "../ccompat.h" +#include "../mem.h" +#include "../tox.h" +#include "../tox_events.h" +#include "../tox_unpack.h" + + +/***************************************************** + * + * :: struct and accessors + * + *****************************************************/ + + +struct Tox_Event_Group_Join_Fail { + uint32_t group_number; + Tox_Group_Join_Fail fail_type; +}; + +non_null() +static void tox_event_group_join_fail_set_group_number(Tox_Event_Group_Join_Fail *group_join_fail, + uint32_t group_number) +{ + assert(group_join_fail != nullptr); + group_join_fail->group_number = group_number; +} +uint32_t tox_event_group_join_fail_get_group_number(const Tox_Event_Group_Join_Fail *group_join_fail) +{ + assert(group_join_fail != nullptr); + return group_join_fail->group_number; +} + +non_null() +static void tox_event_group_join_fail_set_fail_type(Tox_Event_Group_Join_Fail *group_join_fail, + Tox_Group_Join_Fail fail_type) +{ + assert(group_join_fail != nullptr); + group_join_fail->fail_type = fail_type; +} +Tox_Group_Join_Fail tox_event_group_join_fail_get_fail_type(const Tox_Event_Group_Join_Fail *group_join_fail) +{ + assert(group_join_fail != nullptr); + return group_join_fail->fail_type; +} + +non_null() +static void tox_event_group_join_fail_construct(Tox_Event_Group_Join_Fail *group_join_fail) +{ + *group_join_fail = (Tox_Event_Group_Join_Fail) { + 0 + }; +} +non_null() +static void tox_event_group_join_fail_destruct(Tox_Event_Group_Join_Fail *group_join_fail, const Memory *mem) +{ + return; +} + +bool tox_event_group_join_fail_pack( + const Tox_Event_Group_Join_Fail *event, Bin_Pack *bp) +{ + assert(event != nullptr); + return bin_pack_array(bp, 2) + && bin_pack_u32(bp, TOX_EVENT_GROUP_JOIN_FAIL) + && bin_pack_array(bp, 2) + && bin_pack_u32(bp, event->group_number) + && bin_pack_u32(bp, event->fail_type); +} + +non_null() +static bool tox_event_group_join_fail_unpack_into( + Tox_Event_Group_Join_Fail *event, Bin_Unpack *bu) +{ + assert(event != nullptr); + if (!bin_unpack_array_fixed(bu, 2, nullptr)) { + return false; + } + + return bin_unpack_u32(bu, &event->group_number) + && tox_group_join_fail_unpack(bu, &event->fail_type); +} + + +/***************************************************** + * + * :: new/free/add/get/size/unpack + * + *****************************************************/ + +const Tox_Event_Group_Join_Fail *tox_event_get_group_join_fail(const Tox_Event *event) +{ + return event->type == TOX_EVENT_GROUP_JOIN_FAIL ? event->data.group_join_fail : nullptr; +} + +Tox_Event_Group_Join_Fail *tox_event_group_join_fail_new(const Memory *mem) +{ + Tox_Event_Group_Join_Fail *const group_join_fail = + (Tox_Event_Group_Join_Fail *)mem_alloc(mem, sizeof(Tox_Event_Group_Join_Fail)); + + if (group_join_fail == nullptr) { + return nullptr; + } + + tox_event_group_join_fail_construct(group_join_fail); + return group_join_fail; +} + +void tox_event_group_join_fail_free(Tox_Event_Group_Join_Fail *group_join_fail, const Memory *mem) +{ + if (group_join_fail != nullptr) { + tox_event_group_join_fail_destruct(group_join_fail, mem); + } + mem_delete(mem, group_join_fail); +} + +non_null() +static Tox_Event_Group_Join_Fail *tox_events_add_group_join_fail(Tox_Events *events, const Memory *mem) +{ + Tox_Event_Group_Join_Fail *const group_join_fail = tox_event_group_join_fail_new(mem); + + if (group_join_fail == nullptr) { + return nullptr; + } + + Tox_Event event; + event.type = TOX_EVENT_GROUP_JOIN_FAIL; + event.data.group_join_fail = group_join_fail; + + tox_events_add(events, &event); + return group_join_fail; +} + +bool tox_event_group_join_fail_unpack( + Tox_Event_Group_Join_Fail **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_join_fail_new(mem); + + if (*event == nullptr) { + return false; + } + + return tox_event_group_join_fail_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Join_Fail *tox_event_group_join_fail_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_Join_Fail *group_join_fail = tox_events_add_group_join_fail(state->events, state->mem); + + if (group_join_fail == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_join_fail; +} + + +/***************************************************** + * + * :: event handler + * + *****************************************************/ + + +void tox_events_handle_group_join_fail(Tox *tox, uint32_t group_number, Tox_Group_Join_Fail fail_type, + void *user_data) +{ + Tox_Event_Group_Join_Fail *group_join_fail = tox_event_group_join_fail_alloc(user_data); + + if (group_join_fail == nullptr) { + return; + } + + tox_event_group_join_fail_set_group_number(group_join_fail, group_number); + tox_event_group_join_fail_set_fail_type(group_join_fail, fail_type); +} diff --git a/toxcore/events/group_message.c b/toxcore/events/group_message.c new file mode 100644 index 0000000000..06a04a763e --- /dev/null +++ b/toxcore/events/group_message.c @@ -0,0 +1,270 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2023-2024 The TokTok team. + */ + +#include "events_alloc.h" + +#include +#include +#include + +#include "../bin_pack.h" +#include "../bin_unpack.h" +#include "../ccompat.h" +#include "../mem.h" +#include "../tox.h" +#include "../tox_events.h" +#include "../tox_unpack.h" + + +/***************************************************** + * + * :: struct and accessors + * + *****************************************************/ + + +struct Tox_Event_Group_Message { + uint32_t group_number; + uint32_t peer_id; + Tox_Message_Type type; + uint8_t *message; + uint32_t message_length; + uint32_t message_id; +}; + +non_null() +static void tox_event_group_message_set_group_number(Tox_Event_Group_Message *group_message, + uint32_t group_number) +{ + assert(group_message != nullptr); + group_message->group_number = group_number; +} +uint32_t tox_event_group_message_get_group_number(const Tox_Event_Group_Message *group_message) +{ + assert(group_message != nullptr); + return group_message->group_number; +} + +non_null() +static void tox_event_group_message_set_peer_id(Tox_Event_Group_Message *group_message, + uint32_t peer_id) +{ + assert(group_message != nullptr); + group_message->peer_id = peer_id; +} +uint32_t tox_event_group_message_get_peer_id(const Tox_Event_Group_Message *group_message) +{ + assert(group_message != nullptr); + return group_message->peer_id; +} + +non_null() +static void tox_event_group_message_set_type(Tox_Event_Group_Message *group_message, + Tox_Message_Type type) +{ + assert(group_message != nullptr); + group_message->type = type; +} +Tox_Message_Type tox_event_group_message_get_type(const Tox_Event_Group_Message *group_message) +{ + assert(group_message != nullptr); + return group_message->type; +} + +non_null() +static bool tox_event_group_message_set_message(Tox_Event_Group_Message *group_message, + const uint8_t *message, uint32_t message_length) +{ + assert(group_message != nullptr); + + if (group_message->message != nullptr) { + free(group_message->message); + group_message->message = nullptr; + group_message->message_length = 0; + } + + uint8_t *message_copy = (uint8_t *)malloc(message_length); + + if (message_copy == nullptr) { + return false; + } + + memcpy(message_copy, message, message_length); + group_message->message = message_copy; + group_message->message_length = message_length; + return true; +} +uint32_t tox_event_group_message_get_message_length(const Tox_Event_Group_Message *group_message) +{ + assert(group_message != nullptr); + return group_message->message_length; +} +const uint8_t *tox_event_group_message_get_message(const Tox_Event_Group_Message *group_message) +{ + assert(group_message != nullptr); + return group_message->message; +} + +non_null() +static void tox_event_group_message_set_message_id(Tox_Event_Group_Message *group_message, + uint32_t message_id) +{ + assert(group_message != nullptr); + group_message->message_id = message_id; +} +uint32_t tox_event_group_message_get_message_id(const Tox_Event_Group_Message *group_message) +{ + assert(group_message != nullptr); + return group_message->message_id; +} + +non_null() +static void tox_event_group_message_construct(Tox_Event_Group_Message *group_message) +{ + *group_message = (Tox_Event_Group_Message) { + 0 + }; +} +non_null() +static void tox_event_group_message_destruct(Tox_Event_Group_Message *group_message, const Memory *mem) +{ + free(group_message->message); +} + +bool tox_event_group_message_pack( + const Tox_Event_Group_Message *event, Bin_Pack *bp) +{ + assert(event != nullptr); + return bin_pack_array(bp, 2) + && bin_pack_u32(bp, TOX_EVENT_GROUP_MESSAGE) + && bin_pack_array(bp, 5) + && bin_pack_u32(bp, event->group_number) + && bin_pack_u32(bp, event->peer_id) + && bin_pack_u32(bp, event->type) + && bin_pack_bin(bp, event->message, event->message_length) + && bin_pack_u32(bp, event->message_id); +} + +non_null() +static bool tox_event_group_message_unpack_into( + Tox_Event_Group_Message *event, Bin_Unpack *bu) +{ + assert(event != nullptr); + if (!bin_unpack_array_fixed(bu, 5, nullptr)) { + return false; + } + + return bin_unpack_u32(bu, &event->group_number) + && bin_unpack_u32(bu, &event->peer_id) + && tox_message_type_unpack(bu, &event->type) + && bin_unpack_bin(bu, &event->message, &event->message_length) + && bin_unpack_u32(bu, &event->message_id); +} + + +/***************************************************** + * + * :: new/free/add/get/size/unpack + * + *****************************************************/ + +const Tox_Event_Group_Message *tox_event_get_group_message(const Tox_Event *event) +{ + return event->type == TOX_EVENT_GROUP_MESSAGE ? event->data.group_message : nullptr; +} + +Tox_Event_Group_Message *tox_event_group_message_new(const Memory *mem) +{ + Tox_Event_Group_Message *const group_message = + (Tox_Event_Group_Message *)mem_alloc(mem, sizeof(Tox_Event_Group_Message)); + + if (group_message == nullptr) { + return nullptr; + } + + tox_event_group_message_construct(group_message); + return group_message; +} + +void tox_event_group_message_free(Tox_Event_Group_Message *group_message, const Memory *mem) +{ + if (group_message != nullptr) { + tox_event_group_message_destruct(group_message, mem); + } + mem_delete(mem, group_message); +} + +non_null() +static Tox_Event_Group_Message *tox_events_add_group_message(Tox_Events *events, const Memory *mem) +{ + Tox_Event_Group_Message *const group_message = tox_event_group_message_new(mem); + + if (group_message == nullptr) { + return nullptr; + } + + Tox_Event event; + event.type = TOX_EVENT_GROUP_MESSAGE; + event.data.group_message = group_message; + + tox_events_add(events, &event); + return group_message; +} + +bool tox_event_group_message_unpack( + Tox_Event_Group_Message **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_message_new(mem); + + if (*event == nullptr) { + return false; + } + + return tox_event_group_message_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Message *tox_event_group_message_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_Message *group_message = tox_events_add_group_message(state->events, state->mem); + + if (group_message == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_message; +} + + +/***************************************************** + * + * :: event handler + * + *****************************************************/ + + +void tox_events_handle_group_message(Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Message_Type type, const uint8_t *message, size_t length, uint32_t message_id, + void *user_data) +{ + Tox_Event_Group_Message *group_message = tox_event_group_message_alloc(user_data); + + if (group_message == nullptr) { + return; + } + + tox_event_group_message_set_group_number(group_message, group_number); + tox_event_group_message_set_peer_id(group_message, peer_id); + tox_event_group_message_set_type(group_message, type); + tox_event_group_message_set_message(group_message, message, length); + tox_event_group_message_set_message_id(group_message, message_id); +} diff --git a/toxcore/events/group_moderation.c b/toxcore/events/group_moderation.c new file mode 100644 index 0000000000..f728a270e2 --- /dev/null +++ b/toxcore/events/group_moderation.c @@ -0,0 +1,229 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2023-2024 The TokTok team. + */ + +#include "events_alloc.h" + +#include + +#include "../bin_pack.h" +#include "../bin_unpack.h" +#include "../ccompat.h" +#include "../mem.h" +#include "../tox.h" +#include "../tox_events.h" +#include "../tox_unpack.h" + + +/***************************************************** + * + * :: struct and accessors + * + *****************************************************/ + + +struct Tox_Event_Group_Moderation { + uint32_t group_number; + uint32_t source_peer_id; + uint32_t target_peer_id; + Tox_Group_Mod_Event mod_type; +}; + +non_null() +static void tox_event_group_moderation_set_group_number(Tox_Event_Group_Moderation *group_moderation, + uint32_t group_number) +{ + assert(group_moderation != nullptr); + group_moderation->group_number = group_number; +} +uint32_t tox_event_group_moderation_get_group_number(const Tox_Event_Group_Moderation *group_moderation) +{ + assert(group_moderation != nullptr); + return group_moderation->group_number; +} + +non_null() +static void tox_event_group_moderation_set_source_peer_id(Tox_Event_Group_Moderation *group_moderation, + uint32_t source_peer_id) +{ + assert(group_moderation != nullptr); + group_moderation->source_peer_id = source_peer_id; +} +uint32_t tox_event_group_moderation_get_source_peer_id(const Tox_Event_Group_Moderation *group_moderation) +{ + assert(group_moderation != nullptr); + return group_moderation->source_peer_id; +} + +non_null() +static void tox_event_group_moderation_set_target_peer_id(Tox_Event_Group_Moderation *group_moderation, + uint32_t target_peer_id) +{ + assert(group_moderation != nullptr); + group_moderation->target_peer_id = target_peer_id; +} +uint32_t tox_event_group_moderation_get_target_peer_id(const Tox_Event_Group_Moderation *group_moderation) +{ + assert(group_moderation != nullptr); + return group_moderation->target_peer_id; +} + +non_null() +static void tox_event_group_moderation_set_mod_type(Tox_Event_Group_Moderation *group_moderation, + Tox_Group_Mod_Event mod_type) +{ + assert(group_moderation != nullptr); + group_moderation->mod_type = mod_type; +} +Tox_Group_Mod_Event tox_event_group_moderation_get_mod_type(const Tox_Event_Group_Moderation *group_moderation) +{ + assert(group_moderation != nullptr); + return group_moderation->mod_type; +} + +non_null() +static void tox_event_group_moderation_construct(Tox_Event_Group_Moderation *group_moderation) +{ + *group_moderation = (Tox_Event_Group_Moderation) { + 0 + }; +} +non_null() +static void tox_event_group_moderation_destruct(Tox_Event_Group_Moderation *group_moderation, const Memory *mem) +{ + return; +} + +bool tox_event_group_moderation_pack( + const Tox_Event_Group_Moderation *event, Bin_Pack *bp) +{ + assert(event != nullptr); + return bin_pack_array(bp, 2) + && bin_pack_u32(bp, TOX_EVENT_GROUP_MODERATION) + && bin_pack_array(bp, 4) + && bin_pack_u32(bp, event->group_number) + && bin_pack_u32(bp, event->source_peer_id) + && bin_pack_u32(bp, event->target_peer_id) + && bin_pack_u32(bp, event->mod_type); +} + +non_null() +static bool tox_event_group_moderation_unpack_into( + Tox_Event_Group_Moderation *event, Bin_Unpack *bu) +{ + assert(event != nullptr); + if (!bin_unpack_array_fixed(bu, 4, nullptr)) { + return false; + } + + return bin_unpack_u32(bu, &event->group_number) + && bin_unpack_u32(bu, &event->source_peer_id) + && bin_unpack_u32(bu, &event->target_peer_id) + && tox_group_mod_event_unpack(bu, &event->mod_type); +} + + +/***************************************************** + * + * :: new/free/add/get/size/unpack + * + *****************************************************/ + +const Tox_Event_Group_Moderation *tox_event_get_group_moderation(const Tox_Event *event) +{ + return event->type == TOX_EVENT_GROUP_MODERATION ? event->data.group_moderation : nullptr; +} + +Tox_Event_Group_Moderation *tox_event_group_moderation_new(const Memory *mem) +{ + Tox_Event_Group_Moderation *const group_moderation = + (Tox_Event_Group_Moderation *)mem_alloc(mem, sizeof(Tox_Event_Group_Moderation)); + + if (group_moderation == nullptr) { + return nullptr; + } + + tox_event_group_moderation_construct(group_moderation); + return group_moderation; +} + +void tox_event_group_moderation_free(Tox_Event_Group_Moderation *group_moderation, const Memory *mem) +{ + if (group_moderation != nullptr) { + tox_event_group_moderation_destruct(group_moderation, mem); + } + mem_delete(mem, group_moderation); +} + +non_null() +static Tox_Event_Group_Moderation *tox_events_add_group_moderation(Tox_Events *events, const Memory *mem) +{ + Tox_Event_Group_Moderation *const group_moderation = tox_event_group_moderation_new(mem); + + if (group_moderation == nullptr) { + return nullptr; + } + + Tox_Event event; + event.type = TOX_EVENT_GROUP_MODERATION; + event.data.group_moderation = group_moderation; + + tox_events_add(events, &event); + return group_moderation; +} + +bool tox_event_group_moderation_unpack( + Tox_Event_Group_Moderation **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_moderation_new(mem); + + if (*event == nullptr) { + return false; + } + + return tox_event_group_moderation_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Moderation *tox_event_group_moderation_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_Moderation *group_moderation = tox_events_add_group_moderation(state->events, state->mem); + + if (group_moderation == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_moderation; +} + + +/***************************************************** + * + * :: event handler + * + *****************************************************/ + + +void tox_events_handle_group_moderation(Tox *tox, uint32_t group_number, uint32_t source_peer_id, uint32_t target_peer_id, Tox_Group_Mod_Event mod_type, + void *user_data) +{ + Tox_Event_Group_Moderation *group_moderation = tox_event_group_moderation_alloc(user_data); + + if (group_moderation == nullptr) { + return; + } + + tox_event_group_moderation_set_group_number(group_moderation, group_number); + tox_event_group_moderation_set_source_peer_id(group_moderation, source_peer_id); + tox_event_group_moderation_set_target_peer_id(group_moderation, target_peer_id); + tox_event_group_moderation_set_mod_type(group_moderation, mod_type); +} diff --git a/toxcore/events/group_password.c b/toxcore/events/group_password.c new file mode 100644 index 0000000000..6f19bd5a17 --- /dev/null +++ b/toxcore/events/group_password.c @@ -0,0 +1,218 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2023-2024 The TokTok team. + */ + +#include "events_alloc.h" + +#include +#include +#include + +#include "../bin_pack.h" +#include "../bin_unpack.h" +#include "../ccompat.h" +#include "../mem.h" +#include "../tox.h" +#include "../tox_events.h" + + +/***************************************************** + * + * :: struct and accessors + * + *****************************************************/ + + +struct Tox_Event_Group_Password { + uint32_t group_number; + uint8_t *password; + uint32_t password_length; +}; + +non_null() +static void tox_event_group_password_set_group_number(Tox_Event_Group_Password *group_password, + uint32_t group_number) +{ + assert(group_password != nullptr); + group_password->group_number = group_number; +} +uint32_t tox_event_group_password_get_group_number(const Tox_Event_Group_Password *group_password) +{ + assert(group_password != nullptr); + return group_password->group_number; +} + +non_null() +static bool tox_event_group_password_set_password(Tox_Event_Group_Password *group_password, + const uint8_t *password, uint32_t password_length) +{ + assert(group_password != nullptr); + + if (group_password->password != nullptr) { + free(group_password->password); + group_password->password = nullptr; + group_password->password_length = 0; + } + + uint8_t *password_copy = (uint8_t *)malloc(password_length); + + if (password_copy == nullptr) { + return false; + } + + memcpy(password_copy, password, password_length); + group_password->password = password_copy; + group_password->password_length = password_length; + return true; +} +uint32_t tox_event_group_password_get_password_length(const Tox_Event_Group_Password *group_password) +{ + assert(group_password != nullptr); + return group_password->password_length; +} +const uint8_t *tox_event_group_password_get_password(const Tox_Event_Group_Password *group_password) +{ + assert(group_password != nullptr); + return group_password->password; +} + +non_null() +static void tox_event_group_password_construct(Tox_Event_Group_Password *group_password) +{ + *group_password = (Tox_Event_Group_Password) { + 0 + }; +} +non_null() +static void tox_event_group_password_destruct(Tox_Event_Group_Password *group_password, const Memory *mem) +{ + free(group_password->password); +} + +bool tox_event_group_password_pack( + const Tox_Event_Group_Password *event, Bin_Pack *bp) +{ + assert(event != nullptr); + return bin_pack_array(bp, 2) + && bin_pack_u32(bp, TOX_EVENT_GROUP_PASSWORD) + && bin_pack_array(bp, 2) + && bin_pack_u32(bp, event->group_number) + && bin_pack_bin(bp, event->password, event->password_length); +} + +non_null() +static bool tox_event_group_password_unpack_into( + Tox_Event_Group_Password *event, Bin_Unpack *bu) +{ + assert(event != nullptr); + if (!bin_unpack_array_fixed(bu, 2, nullptr)) { + return false; + } + + return bin_unpack_u32(bu, &event->group_number) + && bin_unpack_bin(bu, &event->password, &event->password_length); +} + + +/***************************************************** + * + * :: new/free/add/get/size/unpack + * + *****************************************************/ + +const Tox_Event_Group_Password *tox_event_get_group_password(const Tox_Event *event) +{ + return event->type == TOX_EVENT_GROUP_PASSWORD ? event->data.group_password : nullptr; +} + +Tox_Event_Group_Password *tox_event_group_password_new(const Memory *mem) +{ + Tox_Event_Group_Password *const group_password = + (Tox_Event_Group_Password *)mem_alloc(mem, sizeof(Tox_Event_Group_Password)); + + if (group_password == nullptr) { + return nullptr; + } + + tox_event_group_password_construct(group_password); + return group_password; +} + +void tox_event_group_password_free(Tox_Event_Group_Password *group_password, const Memory *mem) +{ + if (group_password != nullptr) { + tox_event_group_password_destruct(group_password, mem); + } + mem_delete(mem, group_password); +} + +non_null() +static Tox_Event_Group_Password *tox_events_add_group_password(Tox_Events *events, const Memory *mem) +{ + Tox_Event_Group_Password *const group_password = tox_event_group_password_new(mem); + + if (group_password == nullptr) { + return nullptr; + } + + Tox_Event event; + event.type = TOX_EVENT_GROUP_PASSWORD; + event.data.group_password = group_password; + + tox_events_add(events, &event); + return group_password; +} + +bool tox_event_group_password_unpack( + Tox_Event_Group_Password **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_password_new(mem); + + if (*event == nullptr) { + return false; + } + + return tox_event_group_password_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Password *tox_event_group_password_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_Password *group_password = tox_events_add_group_password(state->events, state->mem); + + if (group_password == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_password; +} + + +/***************************************************** + * + * :: event handler + * + *****************************************************/ + + +void tox_events_handle_group_password(Tox *tox, uint32_t group_number, const uint8_t *password, size_t length, + void *user_data) +{ + Tox_Event_Group_Password *group_password = tox_event_group_password_alloc(user_data); + + if (group_password == nullptr) { + return; + } + + tox_event_group_password_set_group_number(group_password, group_number); + tox_event_group_password_set_password(group_password, password, length); +} diff --git a/toxcore/events/group_peer_exit.c b/toxcore/events/group_peer_exit.c new file mode 100644 index 0000000000..5a94cc2009 --- /dev/null +++ b/toxcore/events/group_peer_exit.c @@ -0,0 +1,293 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2023-2024 The TokTok team. + */ + +#include "events_alloc.h" + +#include +#include +#include + +#include "../bin_pack.h" +#include "../bin_unpack.h" +#include "../ccompat.h" +#include "../mem.h" +#include "../tox.h" +#include "../tox_events.h" +#include "../tox_unpack.h" + + +/***************************************************** + * + * :: struct and accessors + * + *****************************************************/ + + +struct Tox_Event_Group_Peer_Exit { + uint32_t group_number; + uint32_t peer_id; + Tox_Group_Exit_Type exit_type; + uint8_t *name; + uint32_t name_length; + uint8_t *part_message; + uint32_t part_message_length; +}; + +non_null() +static void tox_event_group_peer_exit_set_group_number(Tox_Event_Group_Peer_Exit *group_peer_exit, + uint32_t group_number) +{ + assert(group_peer_exit != nullptr); + group_peer_exit->group_number = group_number; +} +uint32_t tox_event_group_peer_exit_get_group_number(const Tox_Event_Group_Peer_Exit *group_peer_exit) +{ + assert(group_peer_exit != nullptr); + return group_peer_exit->group_number; +} + +non_null() +static void tox_event_group_peer_exit_set_peer_id(Tox_Event_Group_Peer_Exit *group_peer_exit, + uint32_t peer_id) +{ + assert(group_peer_exit != nullptr); + group_peer_exit->peer_id = peer_id; +} +uint32_t tox_event_group_peer_exit_get_peer_id(const Tox_Event_Group_Peer_Exit *group_peer_exit) +{ + assert(group_peer_exit != nullptr); + return group_peer_exit->peer_id; +} + +non_null() +static void tox_event_group_peer_exit_set_exit_type(Tox_Event_Group_Peer_Exit *group_peer_exit, + Tox_Group_Exit_Type exit_type) +{ + assert(group_peer_exit != nullptr); + group_peer_exit->exit_type = exit_type; +} +Tox_Group_Exit_Type tox_event_group_peer_exit_get_exit_type(const Tox_Event_Group_Peer_Exit *group_peer_exit) +{ + assert(group_peer_exit != nullptr); + return group_peer_exit->exit_type; +} + +non_null() +static bool tox_event_group_peer_exit_set_name(Tox_Event_Group_Peer_Exit *group_peer_exit, + const uint8_t *name, uint32_t name_length) +{ + assert(group_peer_exit != nullptr); + + if (group_peer_exit->name != nullptr) { + free(group_peer_exit->name); + group_peer_exit->name = nullptr; + group_peer_exit->name_length = 0; + } + + uint8_t *name_copy = (uint8_t *)malloc(name_length); + + if (name_copy == nullptr) { + return false; + } + + memcpy(name_copy, name, name_length); + group_peer_exit->name = name_copy; + group_peer_exit->name_length = name_length; + return true; +} +uint32_t tox_event_group_peer_exit_get_name_length(const Tox_Event_Group_Peer_Exit *group_peer_exit) +{ + assert(group_peer_exit != nullptr); + return group_peer_exit->name_length; +} +const uint8_t *tox_event_group_peer_exit_get_name(const Tox_Event_Group_Peer_Exit *group_peer_exit) +{ + assert(group_peer_exit != nullptr); + return group_peer_exit->name; +} + +non_null() +static bool tox_event_group_peer_exit_set_part_message(Tox_Event_Group_Peer_Exit *group_peer_exit, + const uint8_t *part_message, uint32_t part_message_length) +{ + assert(group_peer_exit != nullptr); + + if (group_peer_exit->part_message != nullptr) { + free(group_peer_exit->part_message); + group_peer_exit->part_message = nullptr; + group_peer_exit->part_message_length = 0; + } + + uint8_t *part_message_copy = (uint8_t *)malloc(part_message_length); + + if (part_message_copy == nullptr) { + return false; + } + + memcpy(part_message_copy, part_message, part_message_length); + group_peer_exit->part_message = part_message_copy; + group_peer_exit->part_message_length = part_message_length; + return true; +} +uint32_t tox_event_group_peer_exit_get_part_message_length(const Tox_Event_Group_Peer_Exit *group_peer_exit) +{ + assert(group_peer_exit != nullptr); + return group_peer_exit->part_message_length; +} +const uint8_t *tox_event_group_peer_exit_get_part_message(const Tox_Event_Group_Peer_Exit *group_peer_exit) +{ + assert(group_peer_exit != nullptr); + return group_peer_exit->part_message; +} + +non_null() +static void tox_event_group_peer_exit_construct(Tox_Event_Group_Peer_Exit *group_peer_exit) +{ + *group_peer_exit = (Tox_Event_Group_Peer_Exit) { + 0 + }; +} +non_null() +static void tox_event_group_peer_exit_destruct(Tox_Event_Group_Peer_Exit *group_peer_exit, const Memory *mem) +{ + free(group_peer_exit->name); + free(group_peer_exit->part_message); +} + +bool tox_event_group_peer_exit_pack( + const Tox_Event_Group_Peer_Exit *event, Bin_Pack *bp) +{ + assert(event != nullptr); + return bin_pack_array(bp, 2) + && bin_pack_u32(bp, TOX_EVENT_GROUP_PEER_EXIT) + && bin_pack_array(bp, 5) + && bin_pack_u32(bp, event->group_number) + && bin_pack_u32(bp, event->peer_id) + && bin_pack_u32(bp, event->exit_type) + && bin_pack_bin(bp, event->name, event->name_length) + && bin_pack_bin(bp, event->part_message, event->part_message_length); +} + +non_null() +static bool tox_event_group_peer_exit_unpack_into( + Tox_Event_Group_Peer_Exit *event, Bin_Unpack *bu) +{ + assert(event != nullptr); + if (!bin_unpack_array_fixed(bu, 5, nullptr)) { + return false; + } + + return bin_unpack_u32(bu, &event->group_number) + && bin_unpack_u32(bu, &event->peer_id) + && tox_group_exit_type_unpack(bu, &event->exit_type) + && bin_unpack_bin(bu, &event->name, &event->name_length) + && bin_unpack_bin(bu, &event->part_message, &event->part_message_length); +} + + +/***************************************************** + * + * :: new/free/add/get/size/unpack + * + *****************************************************/ + +const Tox_Event_Group_Peer_Exit *tox_event_get_group_peer_exit(const Tox_Event *event) +{ + return event->type == TOX_EVENT_GROUP_PEER_EXIT ? event->data.group_peer_exit : nullptr; +} + +Tox_Event_Group_Peer_Exit *tox_event_group_peer_exit_new(const Memory *mem) +{ + Tox_Event_Group_Peer_Exit *const group_peer_exit = + (Tox_Event_Group_Peer_Exit *)mem_alloc(mem, sizeof(Tox_Event_Group_Peer_Exit)); + + if (group_peer_exit == nullptr) { + return nullptr; + } + + tox_event_group_peer_exit_construct(group_peer_exit); + return group_peer_exit; +} + +void tox_event_group_peer_exit_free(Tox_Event_Group_Peer_Exit *group_peer_exit, const Memory *mem) +{ + if (group_peer_exit != nullptr) { + tox_event_group_peer_exit_destruct(group_peer_exit, mem); + } + mem_delete(mem, group_peer_exit); +} + +non_null() +static Tox_Event_Group_Peer_Exit *tox_events_add_group_peer_exit(Tox_Events *events, const Memory *mem) +{ + Tox_Event_Group_Peer_Exit *const group_peer_exit = tox_event_group_peer_exit_new(mem); + + if (group_peer_exit == nullptr) { + return nullptr; + } + + Tox_Event event; + event.type = TOX_EVENT_GROUP_PEER_EXIT; + event.data.group_peer_exit = group_peer_exit; + + tox_events_add(events, &event); + return group_peer_exit; +} + +bool tox_event_group_peer_exit_unpack( + Tox_Event_Group_Peer_Exit **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_peer_exit_new(mem); + + if (*event == nullptr) { + return false; + } + + return tox_event_group_peer_exit_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Peer_Exit *tox_event_group_peer_exit_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_Peer_Exit *group_peer_exit = tox_events_add_group_peer_exit(state->events, state->mem); + + if (group_peer_exit == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_peer_exit; +} + + +/***************************************************** + * + * :: event handler + * + *****************************************************/ + + +void tox_events_handle_group_peer_exit(Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Group_Exit_Type exit_type, const uint8_t *name, size_t name_length, const uint8_t *part_message, size_t part_message_length, + void *user_data) +{ + Tox_Event_Group_Peer_Exit *group_peer_exit = tox_event_group_peer_exit_alloc(user_data); + + if (group_peer_exit == nullptr) { + return; + } + + tox_event_group_peer_exit_set_group_number(group_peer_exit, group_number); + tox_event_group_peer_exit_set_peer_id(group_peer_exit, peer_id); + tox_event_group_peer_exit_set_exit_type(group_peer_exit, exit_type); + tox_event_group_peer_exit_set_name(group_peer_exit, name, name_length); + tox_event_group_peer_exit_set_part_message(group_peer_exit, part_message, part_message_length); +} diff --git a/toxcore/events/group_peer_join.c b/toxcore/events/group_peer_join.c new file mode 100644 index 0000000000..ec366cfc8c --- /dev/null +++ b/toxcore/events/group_peer_join.c @@ -0,0 +1,194 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2023-2024 The TokTok team. + */ + +#include "events_alloc.h" + +#include + +#include "../bin_pack.h" +#include "../bin_unpack.h" +#include "../ccompat.h" +#include "../mem.h" +#include "../tox.h" +#include "../tox_events.h" + + +/***************************************************** + * + * :: struct and accessors + * + *****************************************************/ + + +struct Tox_Event_Group_Peer_Join { + uint32_t group_number; + uint32_t peer_id; +}; + +non_null() +static void tox_event_group_peer_join_set_group_number(Tox_Event_Group_Peer_Join *group_peer_join, + uint32_t group_number) +{ + assert(group_peer_join != nullptr); + group_peer_join->group_number = group_number; +} +uint32_t tox_event_group_peer_join_get_group_number(const Tox_Event_Group_Peer_Join *group_peer_join) +{ + assert(group_peer_join != nullptr); + return group_peer_join->group_number; +} + +non_null() +static void tox_event_group_peer_join_set_peer_id(Tox_Event_Group_Peer_Join *group_peer_join, + uint32_t peer_id) +{ + assert(group_peer_join != nullptr); + group_peer_join->peer_id = peer_id; +} +uint32_t tox_event_group_peer_join_get_peer_id(const Tox_Event_Group_Peer_Join *group_peer_join) +{ + assert(group_peer_join != nullptr); + return group_peer_join->peer_id; +} + +non_null() +static void tox_event_group_peer_join_construct(Tox_Event_Group_Peer_Join *group_peer_join) +{ + *group_peer_join = (Tox_Event_Group_Peer_Join) { + 0 + }; +} +non_null() +static void tox_event_group_peer_join_destruct(Tox_Event_Group_Peer_Join *group_peer_join, const Memory *mem) +{ + return; +} + +bool tox_event_group_peer_join_pack( + const Tox_Event_Group_Peer_Join *event, Bin_Pack *bp) +{ + assert(event != nullptr); + return bin_pack_array(bp, 2) + && bin_pack_u32(bp, TOX_EVENT_GROUP_PEER_JOIN) + && bin_pack_array(bp, 2) + && bin_pack_u32(bp, event->group_number) + && bin_pack_u32(bp, event->peer_id); +} + +non_null() +static bool tox_event_group_peer_join_unpack_into( + Tox_Event_Group_Peer_Join *event, Bin_Unpack *bu) +{ + assert(event != nullptr); + if (!bin_unpack_array_fixed(bu, 2, nullptr)) { + return false; + } + + return bin_unpack_u32(bu, &event->group_number) + && bin_unpack_u32(bu, &event->peer_id); +} + + +/***************************************************** + * + * :: new/free/add/get/size/unpack + * + *****************************************************/ + +const Tox_Event_Group_Peer_Join *tox_event_get_group_peer_join(const Tox_Event *event) +{ + return event->type == TOX_EVENT_GROUP_PEER_JOIN ? event->data.group_peer_join : nullptr; +} + +Tox_Event_Group_Peer_Join *tox_event_group_peer_join_new(const Memory *mem) +{ + Tox_Event_Group_Peer_Join *const group_peer_join = + (Tox_Event_Group_Peer_Join *)mem_alloc(mem, sizeof(Tox_Event_Group_Peer_Join)); + + if (group_peer_join == nullptr) { + return nullptr; + } + + tox_event_group_peer_join_construct(group_peer_join); + return group_peer_join; +} + +void tox_event_group_peer_join_free(Tox_Event_Group_Peer_Join *group_peer_join, const Memory *mem) +{ + if (group_peer_join != nullptr) { + tox_event_group_peer_join_destruct(group_peer_join, mem); + } + mem_delete(mem, group_peer_join); +} + +non_null() +static Tox_Event_Group_Peer_Join *tox_events_add_group_peer_join(Tox_Events *events, const Memory *mem) +{ + Tox_Event_Group_Peer_Join *const group_peer_join = tox_event_group_peer_join_new(mem); + + if (group_peer_join == nullptr) { + return nullptr; + } + + Tox_Event event; + event.type = TOX_EVENT_GROUP_PEER_JOIN; + event.data.group_peer_join = group_peer_join; + + tox_events_add(events, &event); + return group_peer_join; +} + +bool tox_event_group_peer_join_unpack( + Tox_Event_Group_Peer_Join **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_peer_join_new(mem); + + if (*event == nullptr) { + return false; + } + + return tox_event_group_peer_join_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Peer_Join *tox_event_group_peer_join_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_Peer_Join *group_peer_join = tox_events_add_group_peer_join(state->events, state->mem); + + if (group_peer_join == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_peer_join; +} + + +/***************************************************** + * + * :: event handler + * + *****************************************************/ + + +void tox_events_handle_group_peer_join(Tox *tox, uint32_t group_number, uint32_t peer_id, + void *user_data) +{ + Tox_Event_Group_Peer_Join *group_peer_join = tox_event_group_peer_join_alloc(user_data); + + if (group_peer_join == nullptr) { + return; + } + + tox_event_group_peer_join_set_group_number(group_peer_join, group_number); + tox_event_group_peer_join_set_peer_id(group_peer_join, peer_id); +} diff --git a/toxcore/events/group_peer_limit.c b/toxcore/events/group_peer_limit.c new file mode 100644 index 0000000000..7e37cef3dd --- /dev/null +++ b/toxcore/events/group_peer_limit.c @@ -0,0 +1,194 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2023-2024 The TokTok team. + */ + +#include "events_alloc.h" + +#include + +#include "../bin_pack.h" +#include "../bin_unpack.h" +#include "../ccompat.h" +#include "../mem.h" +#include "../tox.h" +#include "../tox_events.h" + + +/***************************************************** + * + * :: struct and accessors + * + *****************************************************/ + + +struct Tox_Event_Group_Peer_Limit { + uint32_t group_number; + uint32_t peer_limit; +}; + +non_null() +static void tox_event_group_peer_limit_set_group_number(Tox_Event_Group_Peer_Limit *group_peer_limit, + uint32_t group_number) +{ + assert(group_peer_limit != nullptr); + group_peer_limit->group_number = group_number; +} +uint32_t tox_event_group_peer_limit_get_group_number(const Tox_Event_Group_Peer_Limit *group_peer_limit) +{ + assert(group_peer_limit != nullptr); + return group_peer_limit->group_number; +} + +non_null() +static void tox_event_group_peer_limit_set_peer_limit(Tox_Event_Group_Peer_Limit *group_peer_limit, + uint32_t peer_limit) +{ + assert(group_peer_limit != nullptr); + group_peer_limit->peer_limit = peer_limit; +} +uint32_t tox_event_group_peer_limit_get_peer_limit(const Tox_Event_Group_Peer_Limit *group_peer_limit) +{ + assert(group_peer_limit != nullptr); + return group_peer_limit->peer_limit; +} + +non_null() +static void tox_event_group_peer_limit_construct(Tox_Event_Group_Peer_Limit *group_peer_limit) +{ + *group_peer_limit = (Tox_Event_Group_Peer_Limit) { + 0 + }; +} +non_null() +static void tox_event_group_peer_limit_destruct(Tox_Event_Group_Peer_Limit *group_peer_limit, const Memory *mem) +{ + return; +} + +bool tox_event_group_peer_limit_pack( + const Tox_Event_Group_Peer_Limit *event, Bin_Pack *bp) +{ + assert(event != nullptr); + return bin_pack_array(bp, 2) + && bin_pack_u32(bp, TOX_EVENT_GROUP_PEER_LIMIT) + && bin_pack_array(bp, 2) + && bin_pack_u32(bp, event->group_number) + && bin_pack_u32(bp, event->peer_limit); +} + +non_null() +static bool tox_event_group_peer_limit_unpack_into( + Tox_Event_Group_Peer_Limit *event, Bin_Unpack *bu) +{ + assert(event != nullptr); + if (!bin_unpack_array_fixed(bu, 2, nullptr)) { + return false; + } + + return bin_unpack_u32(bu, &event->group_number) + && bin_unpack_u32(bu, &event->peer_limit); +} + + +/***************************************************** + * + * :: new/free/add/get/size/unpack + * + *****************************************************/ + +const Tox_Event_Group_Peer_Limit *tox_event_get_group_peer_limit(const Tox_Event *event) +{ + return event->type == TOX_EVENT_GROUP_PEER_LIMIT ? event->data.group_peer_limit : nullptr; +} + +Tox_Event_Group_Peer_Limit *tox_event_group_peer_limit_new(const Memory *mem) +{ + Tox_Event_Group_Peer_Limit *const group_peer_limit = + (Tox_Event_Group_Peer_Limit *)mem_alloc(mem, sizeof(Tox_Event_Group_Peer_Limit)); + + if (group_peer_limit == nullptr) { + return nullptr; + } + + tox_event_group_peer_limit_construct(group_peer_limit); + return group_peer_limit; +} + +void tox_event_group_peer_limit_free(Tox_Event_Group_Peer_Limit *group_peer_limit, const Memory *mem) +{ + if (group_peer_limit != nullptr) { + tox_event_group_peer_limit_destruct(group_peer_limit, mem); + } + mem_delete(mem, group_peer_limit); +} + +non_null() +static Tox_Event_Group_Peer_Limit *tox_events_add_group_peer_limit(Tox_Events *events, const Memory *mem) +{ + Tox_Event_Group_Peer_Limit *const group_peer_limit = tox_event_group_peer_limit_new(mem); + + if (group_peer_limit == nullptr) { + return nullptr; + } + + Tox_Event event; + event.type = TOX_EVENT_GROUP_PEER_LIMIT; + event.data.group_peer_limit = group_peer_limit; + + tox_events_add(events, &event); + return group_peer_limit; +} + +bool tox_event_group_peer_limit_unpack( + Tox_Event_Group_Peer_Limit **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_peer_limit_new(mem); + + if (*event == nullptr) { + return false; + } + + return tox_event_group_peer_limit_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Peer_Limit *tox_event_group_peer_limit_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_Peer_Limit *group_peer_limit = tox_events_add_group_peer_limit(state->events, state->mem); + + if (group_peer_limit == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_peer_limit; +} + + +/***************************************************** + * + * :: event handler + * + *****************************************************/ + + +void tox_events_handle_group_peer_limit(Tox *tox, uint32_t group_number, uint32_t peer_limit, + void *user_data) +{ + Tox_Event_Group_Peer_Limit *group_peer_limit = tox_event_group_peer_limit_alloc(user_data); + + if (group_peer_limit == nullptr) { + return; + } + + tox_event_group_peer_limit_set_group_number(group_peer_limit, group_number); + tox_event_group_peer_limit_set_peer_limit(group_peer_limit, peer_limit); +} diff --git a/toxcore/events/group_peer_name.c b/toxcore/events/group_peer_name.c new file mode 100644 index 0000000000..e81ee04ccd --- /dev/null +++ b/toxcore/events/group_peer_name.c @@ -0,0 +1,235 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2023-2024 The TokTok team. + */ + +#include "events_alloc.h" + +#include +#include +#include + +#include "../bin_pack.h" +#include "../bin_unpack.h" +#include "../ccompat.h" +#include "../mem.h" +#include "../tox.h" +#include "../tox_events.h" + + +/***************************************************** + * + * :: struct and accessors + * + *****************************************************/ + + +struct Tox_Event_Group_Peer_Name { + uint32_t group_number; + uint32_t peer_id; + uint8_t *name; + uint32_t name_length; +}; + +non_null() +static void tox_event_group_peer_name_set_group_number(Tox_Event_Group_Peer_Name *group_peer_name, + uint32_t group_number) +{ + assert(group_peer_name != nullptr); + group_peer_name->group_number = group_number; +} +uint32_t tox_event_group_peer_name_get_group_number(const Tox_Event_Group_Peer_Name *group_peer_name) +{ + assert(group_peer_name != nullptr); + return group_peer_name->group_number; +} + +non_null() +static void tox_event_group_peer_name_set_peer_id(Tox_Event_Group_Peer_Name *group_peer_name, + uint32_t peer_id) +{ + assert(group_peer_name != nullptr); + group_peer_name->peer_id = peer_id; +} +uint32_t tox_event_group_peer_name_get_peer_id(const Tox_Event_Group_Peer_Name *group_peer_name) +{ + assert(group_peer_name != nullptr); + return group_peer_name->peer_id; +} + +non_null() +static bool tox_event_group_peer_name_set_name(Tox_Event_Group_Peer_Name *group_peer_name, + const uint8_t *name, uint32_t name_length) +{ + assert(group_peer_name != nullptr); + + if (group_peer_name->name != nullptr) { + free(group_peer_name->name); + group_peer_name->name = nullptr; + group_peer_name->name_length = 0; + } + + uint8_t *name_copy = (uint8_t *)malloc(name_length); + + if (name_copy == nullptr) { + return false; + } + + memcpy(name_copy, name, name_length); + group_peer_name->name = name_copy; + group_peer_name->name_length = name_length; + return true; +} +uint32_t tox_event_group_peer_name_get_name_length(const Tox_Event_Group_Peer_Name *group_peer_name) +{ + assert(group_peer_name != nullptr); + return group_peer_name->name_length; +} +const uint8_t *tox_event_group_peer_name_get_name(const Tox_Event_Group_Peer_Name *group_peer_name) +{ + assert(group_peer_name != nullptr); + return group_peer_name->name; +} + +non_null() +static void tox_event_group_peer_name_construct(Tox_Event_Group_Peer_Name *group_peer_name) +{ + *group_peer_name = (Tox_Event_Group_Peer_Name) { + 0 + }; +} +non_null() +static void tox_event_group_peer_name_destruct(Tox_Event_Group_Peer_Name *group_peer_name, const Memory *mem) +{ + free(group_peer_name->name); +} + +bool tox_event_group_peer_name_pack( + const Tox_Event_Group_Peer_Name *event, Bin_Pack *bp) +{ + assert(event != nullptr); + return bin_pack_array(bp, 2) + && bin_pack_u32(bp, TOX_EVENT_GROUP_PEER_NAME) + && bin_pack_array(bp, 3) + && bin_pack_u32(bp, event->group_number) + && bin_pack_u32(bp, event->peer_id) + && bin_pack_bin(bp, event->name, event->name_length); +} + +non_null() +static bool tox_event_group_peer_name_unpack_into( + Tox_Event_Group_Peer_Name *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->name, &event->name_length); +} + + +/***************************************************** + * + * :: new/free/add/get/size/unpack + * + *****************************************************/ + +const Tox_Event_Group_Peer_Name *tox_event_get_group_peer_name(const Tox_Event *event) +{ + return event->type == TOX_EVENT_GROUP_PEER_NAME ? event->data.group_peer_name : nullptr; +} + +Tox_Event_Group_Peer_Name *tox_event_group_peer_name_new(const Memory *mem) +{ + Tox_Event_Group_Peer_Name *const group_peer_name = + (Tox_Event_Group_Peer_Name *)mem_alloc(mem, sizeof(Tox_Event_Group_Peer_Name)); + + if (group_peer_name == nullptr) { + return nullptr; + } + + tox_event_group_peer_name_construct(group_peer_name); + return group_peer_name; +} + +void tox_event_group_peer_name_free(Tox_Event_Group_Peer_Name *group_peer_name, const Memory *mem) +{ + if (group_peer_name != nullptr) { + tox_event_group_peer_name_destruct(group_peer_name, mem); + } + mem_delete(mem, group_peer_name); +} + +non_null() +static Tox_Event_Group_Peer_Name *tox_events_add_group_peer_name(Tox_Events *events, const Memory *mem) +{ + Tox_Event_Group_Peer_Name *const group_peer_name = tox_event_group_peer_name_new(mem); + + if (group_peer_name == nullptr) { + return nullptr; + } + + Tox_Event event; + event.type = TOX_EVENT_GROUP_PEER_NAME; + event.data.group_peer_name = group_peer_name; + + tox_events_add(events, &event); + return group_peer_name; +} + +bool tox_event_group_peer_name_unpack( + Tox_Event_Group_Peer_Name **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_peer_name_new(mem); + + if (*event == nullptr) { + return false; + } + + return tox_event_group_peer_name_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Peer_Name *tox_event_group_peer_name_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_Peer_Name *group_peer_name = tox_events_add_group_peer_name(state->events, state->mem); + + if (group_peer_name == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_peer_name; +} + + +/***************************************************** + * + * :: event handler + * + *****************************************************/ + + +void tox_events_handle_group_peer_name(Tox *tox, uint32_t group_number, uint32_t peer_id, const uint8_t *name, size_t length, + void *user_data) +{ + Tox_Event_Group_Peer_Name *group_peer_name = tox_event_group_peer_name_alloc(user_data); + + if (group_peer_name == nullptr) { + return; + } + + tox_event_group_peer_name_set_group_number(group_peer_name, group_number); + tox_event_group_peer_name_set_peer_id(group_peer_name, peer_id); + tox_event_group_peer_name_set_name(group_peer_name, name, length); +} diff --git a/toxcore/events/group_peer_status.c b/toxcore/events/group_peer_status.c new file mode 100644 index 0000000000..8bf456f1d1 --- /dev/null +++ b/toxcore/events/group_peer_status.c @@ -0,0 +1,212 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2023-2024 The TokTok team. + */ + +#include "events_alloc.h" + +#include + +#include "../bin_pack.h" +#include "../bin_unpack.h" +#include "../ccompat.h" +#include "../mem.h" +#include "../tox.h" +#include "../tox_events.h" +#include "../tox_unpack.h" + + +/***************************************************** + * + * :: struct and accessors + * + *****************************************************/ + + +struct Tox_Event_Group_Peer_Status { + uint32_t group_number; + uint32_t peer_id; + Tox_User_Status status; +}; + +non_null() +static void tox_event_group_peer_status_set_group_number(Tox_Event_Group_Peer_Status *group_peer_status, + uint32_t group_number) +{ + assert(group_peer_status != nullptr); + group_peer_status->group_number = group_number; +} +uint32_t tox_event_group_peer_status_get_group_number(const Tox_Event_Group_Peer_Status *group_peer_status) +{ + assert(group_peer_status != nullptr); + return group_peer_status->group_number; +} + +non_null() +static void tox_event_group_peer_status_set_peer_id(Tox_Event_Group_Peer_Status *group_peer_status, + uint32_t peer_id) +{ + assert(group_peer_status != nullptr); + group_peer_status->peer_id = peer_id; +} +uint32_t tox_event_group_peer_status_get_peer_id(const Tox_Event_Group_Peer_Status *group_peer_status) +{ + assert(group_peer_status != nullptr); + return group_peer_status->peer_id; +} + +non_null() +static void tox_event_group_peer_status_set_status(Tox_Event_Group_Peer_Status *group_peer_status, + Tox_User_Status status) +{ + assert(group_peer_status != nullptr); + group_peer_status->status = status; +} +Tox_User_Status tox_event_group_peer_status_get_status(const Tox_Event_Group_Peer_Status *group_peer_status) +{ + assert(group_peer_status != nullptr); + return group_peer_status->status; +} + +non_null() +static void tox_event_group_peer_status_construct(Tox_Event_Group_Peer_Status *group_peer_status) +{ + *group_peer_status = (Tox_Event_Group_Peer_Status) { + 0 + }; +} +non_null() +static void tox_event_group_peer_status_destruct(Tox_Event_Group_Peer_Status *group_peer_status, const Memory *mem) +{ + return; +} + +bool tox_event_group_peer_status_pack( + const Tox_Event_Group_Peer_Status *event, Bin_Pack *bp) +{ + assert(event != nullptr); + return bin_pack_array(bp, 2) + && bin_pack_u32(bp, TOX_EVENT_GROUP_PEER_STATUS) + && bin_pack_array(bp, 3) + && bin_pack_u32(bp, event->group_number) + && bin_pack_u32(bp, event->peer_id) + && bin_pack_u32(bp, event->status); +} + +non_null() +static bool tox_event_group_peer_status_unpack_into( + Tox_Event_Group_Peer_Status *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) + && tox_user_status_unpack(bu, &event->status); +} + + +/***************************************************** + * + * :: new/free/add/get/size/unpack + * + *****************************************************/ + +const Tox_Event_Group_Peer_Status *tox_event_get_group_peer_status(const Tox_Event *event) +{ + return event->type == TOX_EVENT_GROUP_PEER_STATUS ? event->data.group_peer_status : nullptr; +} + +Tox_Event_Group_Peer_Status *tox_event_group_peer_status_new(const Memory *mem) +{ + Tox_Event_Group_Peer_Status *const group_peer_status = + (Tox_Event_Group_Peer_Status *)mem_alloc(mem, sizeof(Tox_Event_Group_Peer_Status)); + + if (group_peer_status == nullptr) { + return nullptr; + } + + tox_event_group_peer_status_construct(group_peer_status); + return group_peer_status; +} + +void tox_event_group_peer_status_free(Tox_Event_Group_Peer_Status *group_peer_status, const Memory *mem) +{ + if (group_peer_status != nullptr) { + tox_event_group_peer_status_destruct(group_peer_status, mem); + } + mem_delete(mem, group_peer_status); +} + +non_null() +static Tox_Event_Group_Peer_Status *tox_events_add_group_peer_status(Tox_Events *events, const Memory *mem) +{ + Tox_Event_Group_Peer_Status *const group_peer_status = tox_event_group_peer_status_new(mem); + + if (group_peer_status == nullptr) { + return nullptr; + } + + Tox_Event event; + event.type = TOX_EVENT_GROUP_PEER_STATUS; + event.data.group_peer_status = group_peer_status; + + tox_events_add(events, &event); + return group_peer_status; +} + +bool tox_event_group_peer_status_unpack( + Tox_Event_Group_Peer_Status **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_peer_status_new(mem); + + if (*event == nullptr) { + return false; + } + + return tox_event_group_peer_status_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Peer_Status *tox_event_group_peer_status_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_Peer_Status *group_peer_status = tox_events_add_group_peer_status(state->events, state->mem); + + if (group_peer_status == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_peer_status; +} + + +/***************************************************** + * + * :: event handler + * + *****************************************************/ + + +void tox_events_handle_group_peer_status(Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_User_Status status, + void *user_data) +{ + Tox_Event_Group_Peer_Status *group_peer_status = tox_event_group_peer_status_alloc(user_data); + + if (group_peer_status == nullptr) { + return; + } + + tox_event_group_peer_status_set_group_number(group_peer_status, group_number); + tox_event_group_peer_status_set_peer_id(group_peer_status, peer_id); + tox_event_group_peer_status_set_status(group_peer_status, status); +} diff --git a/toxcore/events/group_privacy_state.c b/toxcore/events/group_privacy_state.c new file mode 100644 index 0000000000..e82e42382c --- /dev/null +++ b/toxcore/events/group_privacy_state.c @@ -0,0 +1,195 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2023-2024 The TokTok team. + */ + +#include "events_alloc.h" + +#include + +#include "../bin_pack.h" +#include "../bin_unpack.h" +#include "../ccompat.h" +#include "../mem.h" +#include "../tox.h" +#include "../tox_events.h" +#include "../tox_unpack.h" + + +/***************************************************** + * + * :: struct and accessors + * + *****************************************************/ + + +struct Tox_Event_Group_Privacy_State { + uint32_t group_number; + Tox_Group_Privacy_State privacy_state; +}; + +non_null() +static void tox_event_group_privacy_state_set_group_number(Tox_Event_Group_Privacy_State *group_privacy_state, + uint32_t group_number) +{ + assert(group_privacy_state != nullptr); + group_privacy_state->group_number = group_number; +} +uint32_t tox_event_group_privacy_state_get_group_number(const Tox_Event_Group_Privacy_State *group_privacy_state) +{ + assert(group_privacy_state != nullptr); + return group_privacy_state->group_number; +} + +non_null() +static void tox_event_group_privacy_state_set_privacy_state(Tox_Event_Group_Privacy_State *group_privacy_state, + Tox_Group_Privacy_State privacy_state) +{ + assert(group_privacy_state != nullptr); + group_privacy_state->privacy_state = privacy_state; +} +Tox_Group_Privacy_State tox_event_group_privacy_state_get_privacy_state(const Tox_Event_Group_Privacy_State *group_privacy_state) +{ + assert(group_privacy_state != nullptr); + return group_privacy_state->privacy_state; +} + +non_null() +static void tox_event_group_privacy_state_construct(Tox_Event_Group_Privacy_State *group_privacy_state) +{ + *group_privacy_state = (Tox_Event_Group_Privacy_State) { + 0 + }; +} +non_null() +static void tox_event_group_privacy_state_destruct(Tox_Event_Group_Privacy_State *group_privacy_state, const Memory *mem) +{ + return; +} + +bool tox_event_group_privacy_state_pack( + const Tox_Event_Group_Privacy_State *event, Bin_Pack *bp) +{ + assert(event != nullptr); + return bin_pack_array(bp, 2) + && bin_pack_u32(bp, TOX_EVENT_GROUP_PRIVACY_STATE) + && bin_pack_array(bp, 2) + && bin_pack_u32(bp, event->group_number) + && bin_pack_u32(bp, event->privacy_state); +} + +non_null() +static bool tox_event_group_privacy_state_unpack_into( + Tox_Event_Group_Privacy_State *event, Bin_Unpack *bu) +{ + assert(event != nullptr); + if (!bin_unpack_array_fixed(bu, 2, nullptr)) { + return false; + } + + return bin_unpack_u32(bu, &event->group_number) + && tox_group_privacy_state_unpack(bu, &event->privacy_state); +} + + +/***************************************************** + * + * :: new/free/add/get/size/unpack + * + *****************************************************/ + +const Tox_Event_Group_Privacy_State *tox_event_get_group_privacy_state(const Tox_Event *event) +{ + return event->type == TOX_EVENT_GROUP_PRIVACY_STATE ? event->data.group_privacy_state : nullptr; +} + +Tox_Event_Group_Privacy_State *tox_event_group_privacy_state_new(const Memory *mem) +{ + Tox_Event_Group_Privacy_State *const group_privacy_state = + (Tox_Event_Group_Privacy_State *)mem_alloc(mem, sizeof(Tox_Event_Group_Privacy_State)); + + if (group_privacy_state == nullptr) { + return nullptr; + } + + tox_event_group_privacy_state_construct(group_privacy_state); + return group_privacy_state; +} + +void tox_event_group_privacy_state_free(Tox_Event_Group_Privacy_State *group_privacy_state, const Memory *mem) +{ + if (group_privacy_state != nullptr) { + tox_event_group_privacy_state_destruct(group_privacy_state, mem); + } + mem_delete(mem, group_privacy_state); +} + +non_null() +static Tox_Event_Group_Privacy_State *tox_events_add_group_privacy_state(Tox_Events *events, const Memory *mem) +{ + Tox_Event_Group_Privacy_State *const group_privacy_state = tox_event_group_privacy_state_new(mem); + + if (group_privacy_state == nullptr) { + return nullptr; + } + + Tox_Event event; + event.type = TOX_EVENT_GROUP_PRIVACY_STATE; + event.data.group_privacy_state = group_privacy_state; + + tox_events_add(events, &event); + return group_privacy_state; +} + +bool tox_event_group_privacy_state_unpack( + Tox_Event_Group_Privacy_State **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_privacy_state_new(mem); + + if (*event == nullptr) { + return false; + } + + return tox_event_group_privacy_state_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Privacy_State *tox_event_group_privacy_state_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_Privacy_State *group_privacy_state = tox_events_add_group_privacy_state(state->events, state->mem); + + if (group_privacy_state == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_privacy_state; +} + + +/***************************************************** + * + * :: event handler + * + *****************************************************/ + + +void tox_events_handle_group_privacy_state(Tox *tox, uint32_t group_number, Tox_Group_Privacy_State privacy_state, + void *user_data) +{ + Tox_Event_Group_Privacy_State *group_privacy_state = tox_event_group_privacy_state_alloc(user_data); + + if (group_privacy_state == nullptr) { + return; + } + + tox_event_group_privacy_state_set_group_number(group_privacy_state, group_number); + tox_event_group_privacy_state_set_privacy_state(group_privacy_state, privacy_state); +} diff --git a/toxcore/events/group_private_message.c b/toxcore/events/group_private_message.c new file mode 100644 index 0000000000..efa62fbcba --- /dev/null +++ b/toxcore/events/group_private_message.c @@ -0,0 +1,253 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2023-2024 The TokTok team. + */ + +#include "events_alloc.h" + +#include +#include +#include + +#include "../bin_pack.h" +#include "../bin_unpack.h" +#include "../ccompat.h" +#include "../mem.h" +#include "../tox.h" +#include "../tox_events.h" +#include "../tox_unpack.h" + + +/***************************************************** + * + * :: struct and accessors + * + *****************************************************/ + + +struct Tox_Event_Group_Private_Message { + uint32_t group_number; + uint32_t peer_id; + Tox_Message_Type type; + uint8_t *message; + uint32_t message_length; +}; + +non_null() +static void tox_event_group_private_message_set_group_number(Tox_Event_Group_Private_Message *group_private_message, + uint32_t group_number) +{ + assert(group_private_message != nullptr); + group_private_message->group_number = group_number; +} +uint32_t tox_event_group_private_message_get_group_number(const Tox_Event_Group_Private_Message *group_private_message) +{ + assert(group_private_message != nullptr); + return group_private_message->group_number; +} + +non_null() +static void tox_event_group_private_message_set_peer_id(Tox_Event_Group_Private_Message *group_private_message, + uint32_t peer_id) +{ + assert(group_private_message != nullptr); + group_private_message->peer_id = peer_id; +} +uint32_t tox_event_group_private_message_get_peer_id(const Tox_Event_Group_Private_Message *group_private_message) +{ + assert(group_private_message != nullptr); + return group_private_message->peer_id; +} + +non_null() +static void tox_event_group_private_message_set_type(Tox_Event_Group_Private_Message *group_private_message, + Tox_Message_Type type) +{ + assert(group_private_message != nullptr); + group_private_message->type = type; +} +Tox_Message_Type tox_event_group_private_message_get_type(const Tox_Event_Group_Private_Message *group_private_message) +{ + assert(group_private_message != nullptr); + return group_private_message->type; +} + +non_null() +static bool tox_event_group_private_message_set_message(Tox_Event_Group_Private_Message *group_private_message, + const uint8_t *message, uint32_t message_length) +{ + assert(group_private_message != nullptr); + + if (group_private_message->message != nullptr) { + free(group_private_message->message); + group_private_message->message = nullptr; + group_private_message->message_length = 0; + } + + uint8_t *message_copy = (uint8_t *)malloc(message_length); + + if (message_copy == nullptr) { + return false; + } + + memcpy(message_copy, message, message_length); + group_private_message->message = message_copy; + group_private_message->message_length = message_length; + return true; +} +uint32_t tox_event_group_private_message_get_message_length(const Tox_Event_Group_Private_Message *group_private_message) +{ + assert(group_private_message != nullptr); + return group_private_message->message_length; +} +const uint8_t *tox_event_group_private_message_get_message(const Tox_Event_Group_Private_Message *group_private_message) +{ + assert(group_private_message != nullptr); + return group_private_message->message; +} + +non_null() +static void tox_event_group_private_message_construct(Tox_Event_Group_Private_Message *group_private_message) +{ + *group_private_message = (Tox_Event_Group_Private_Message) { + 0 + }; +} +non_null() +static void tox_event_group_private_message_destruct(Tox_Event_Group_Private_Message *group_private_message, const Memory *mem) +{ + free(group_private_message->message); +} + +bool tox_event_group_private_message_pack( + const Tox_Event_Group_Private_Message *event, Bin_Pack *bp) +{ + assert(event != nullptr); + return bin_pack_array(bp, 2) + && bin_pack_u32(bp, TOX_EVENT_GROUP_PRIVATE_MESSAGE) + && bin_pack_array(bp, 4) + && bin_pack_u32(bp, event->group_number) + && bin_pack_u32(bp, event->peer_id) + && bin_pack_u32(bp, event->type) + && bin_pack_bin(bp, event->message, event->message_length); +} + +non_null() +static bool tox_event_group_private_message_unpack_into( + Tox_Event_Group_Private_Message *event, Bin_Unpack *bu) +{ + assert(event != nullptr); + if (!bin_unpack_array_fixed(bu, 4, nullptr)) { + return false; + } + + return bin_unpack_u32(bu, &event->group_number) + && bin_unpack_u32(bu, &event->peer_id) + && tox_message_type_unpack(bu, &event->type) + && bin_unpack_bin(bu, &event->message, &event->message_length); +} + + +/***************************************************** + * + * :: new/free/add/get/size/unpack + * + *****************************************************/ + +const Tox_Event_Group_Private_Message *tox_event_get_group_private_message(const Tox_Event *event) +{ + return event->type == TOX_EVENT_GROUP_PRIVATE_MESSAGE ? event->data.group_private_message : nullptr; +} + +Tox_Event_Group_Private_Message *tox_event_group_private_message_new(const Memory *mem) +{ + Tox_Event_Group_Private_Message *const group_private_message = + (Tox_Event_Group_Private_Message *)mem_alloc(mem, sizeof(Tox_Event_Group_Private_Message)); + + if (group_private_message == nullptr) { + return nullptr; + } + + tox_event_group_private_message_construct(group_private_message); + return group_private_message; +} + +void tox_event_group_private_message_free(Tox_Event_Group_Private_Message *group_private_message, const Memory *mem) +{ + if (group_private_message != nullptr) { + tox_event_group_private_message_destruct(group_private_message, mem); + } + mem_delete(mem, group_private_message); +} + +non_null() +static Tox_Event_Group_Private_Message *tox_events_add_group_private_message(Tox_Events *events, const Memory *mem) +{ + Tox_Event_Group_Private_Message *const group_private_message = tox_event_group_private_message_new(mem); + + if (group_private_message == nullptr) { + return nullptr; + } + + Tox_Event event; + event.type = TOX_EVENT_GROUP_PRIVATE_MESSAGE; + event.data.group_private_message = group_private_message; + + tox_events_add(events, &event); + return group_private_message; +} + +bool tox_event_group_private_message_unpack( + Tox_Event_Group_Private_Message **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_private_message_new(mem); + + if (*event == nullptr) { + return false; + } + + return tox_event_group_private_message_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Private_Message *tox_event_group_private_message_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_Private_Message *group_private_message = tox_events_add_group_private_message(state->events, state->mem); + + if (group_private_message == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_private_message; +} + + +/***************************************************** + * + * :: event handler + * + *****************************************************/ + + +void tox_events_handle_group_private_message(Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Message_Type type, const uint8_t *message, size_t length, + void *user_data) +{ + Tox_Event_Group_Private_Message *group_private_message = tox_event_group_private_message_alloc(user_data); + + if (group_private_message == nullptr) { + return; + } + + tox_event_group_private_message_set_group_number(group_private_message, group_number); + tox_event_group_private_message_set_peer_id(group_private_message, peer_id); + tox_event_group_private_message_set_type(group_private_message, type); + tox_event_group_private_message_set_message(group_private_message, message, length); +} diff --git a/toxcore/events/group_self_join.c b/toxcore/events/group_self_join.c new file mode 100644 index 0000000000..ceca661ff3 --- /dev/null +++ b/toxcore/events/group_self_join.c @@ -0,0 +1,172 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2023-2024 The TokTok team. + */ + +#include "events_alloc.h" + +#include + +#include "../bin_pack.h" +#include "../bin_unpack.h" +#include "../ccompat.h" +#include "../mem.h" +#include "../tox.h" +#include "../tox_events.h" + + +/***************************************************** + * + * :: struct and accessors + * + *****************************************************/ + + +struct Tox_Event_Group_Self_Join { + uint32_t group_number; +}; + +non_null() +static void tox_event_group_self_join_set_group_number(Tox_Event_Group_Self_Join *group_self_join, + uint32_t group_number) +{ + assert(group_self_join != nullptr); + group_self_join->group_number = group_number; +} +uint32_t tox_event_group_self_join_get_group_number(const Tox_Event_Group_Self_Join *group_self_join) +{ + assert(group_self_join != nullptr); + return group_self_join->group_number; +} + +non_null() +static void tox_event_group_self_join_construct(Tox_Event_Group_Self_Join *group_self_join) +{ + *group_self_join = (Tox_Event_Group_Self_Join) { + 0 + }; +} +non_null() +static void tox_event_group_self_join_destruct(Tox_Event_Group_Self_Join *group_self_join, const Memory *mem) +{ + return; +} + +bool tox_event_group_self_join_pack( + const Tox_Event_Group_Self_Join *event, Bin_Pack *bp) +{ + assert(event != nullptr); + return bin_pack_array(bp, 2) + && bin_pack_u32(bp, TOX_EVENT_GROUP_SELF_JOIN) + && bin_pack_u32(bp, event->group_number); +} + +non_null() +static bool tox_event_group_self_join_unpack_into( + Tox_Event_Group_Self_Join *event, Bin_Unpack *bu) +{ + assert(event != nullptr); + return bin_unpack_u32(bu, &event->group_number); +} + + +/***************************************************** + * + * :: new/free/add/get/size/unpack + * + *****************************************************/ + +const Tox_Event_Group_Self_Join *tox_event_get_group_self_join(const Tox_Event *event) +{ + return event->type == TOX_EVENT_GROUP_SELF_JOIN ? event->data.group_self_join : nullptr; +} + +Tox_Event_Group_Self_Join *tox_event_group_self_join_new(const Memory *mem) +{ + Tox_Event_Group_Self_Join *const group_self_join = + (Tox_Event_Group_Self_Join *)mem_alloc(mem, sizeof(Tox_Event_Group_Self_Join)); + + if (group_self_join == nullptr) { + return nullptr; + } + + tox_event_group_self_join_construct(group_self_join); + return group_self_join; +} + +void tox_event_group_self_join_free(Tox_Event_Group_Self_Join *group_self_join, const Memory *mem) +{ + if (group_self_join != nullptr) { + tox_event_group_self_join_destruct(group_self_join, mem); + } + mem_delete(mem, group_self_join); +} + +non_null() +static Tox_Event_Group_Self_Join *tox_events_add_group_self_join(Tox_Events *events, const Memory *mem) +{ + Tox_Event_Group_Self_Join *const group_self_join = tox_event_group_self_join_new(mem); + + if (group_self_join == nullptr) { + return nullptr; + } + + Tox_Event event; + event.type = TOX_EVENT_GROUP_SELF_JOIN; + event.data.group_self_join = group_self_join; + + tox_events_add(events, &event); + return group_self_join; +} + +bool tox_event_group_self_join_unpack( + Tox_Event_Group_Self_Join **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_self_join_new(mem); + + if (*event == nullptr) { + return false; + } + + return tox_event_group_self_join_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Self_Join *tox_event_group_self_join_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_Self_Join *group_self_join = tox_events_add_group_self_join(state->events, state->mem); + + if (group_self_join == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_self_join; +} + + +/***************************************************** + * + * :: event handler + * + *****************************************************/ + + +void tox_events_handle_group_self_join(Tox *tox, uint32_t group_number, + void *user_data) +{ + Tox_Event_Group_Self_Join *group_self_join = tox_event_group_self_join_alloc(user_data); + + if (group_self_join == nullptr) { + return; + } + + tox_event_group_self_join_set_group_number(group_self_join, group_number); +} diff --git a/toxcore/events/group_topic.c b/toxcore/events/group_topic.c new file mode 100644 index 0000000000..48f5ed7c26 --- /dev/null +++ b/toxcore/events/group_topic.c @@ -0,0 +1,235 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2023-2024 The TokTok team. + */ + +#include "events_alloc.h" + +#include +#include +#include + +#include "../bin_pack.h" +#include "../bin_unpack.h" +#include "../ccompat.h" +#include "../mem.h" +#include "../tox.h" +#include "../tox_events.h" + + +/***************************************************** + * + * :: struct and accessors + * + *****************************************************/ + + +struct Tox_Event_Group_Topic { + uint32_t group_number; + uint32_t peer_id; + uint8_t *topic; + uint32_t topic_length; +}; + +non_null() +static void tox_event_group_topic_set_group_number(Tox_Event_Group_Topic *group_topic, + uint32_t group_number) +{ + assert(group_topic != nullptr); + group_topic->group_number = group_number; +} +uint32_t tox_event_group_topic_get_group_number(const Tox_Event_Group_Topic *group_topic) +{ + assert(group_topic != nullptr); + return group_topic->group_number; +} + +non_null() +static void tox_event_group_topic_set_peer_id(Tox_Event_Group_Topic *group_topic, + uint32_t peer_id) +{ + assert(group_topic != nullptr); + group_topic->peer_id = peer_id; +} +uint32_t tox_event_group_topic_get_peer_id(const Tox_Event_Group_Topic *group_topic) +{ + assert(group_topic != nullptr); + return group_topic->peer_id; +} + +non_null() +static bool tox_event_group_topic_set_topic(Tox_Event_Group_Topic *group_topic, + const uint8_t *topic, uint32_t topic_length) +{ + assert(group_topic != nullptr); + + if (group_topic->topic != nullptr) { + free(group_topic->topic); + group_topic->topic = nullptr; + group_topic->topic_length = 0; + } + + uint8_t *topic_copy = (uint8_t *)malloc(topic_length); + + if (topic_copy == nullptr) { + return false; + } + + memcpy(topic_copy, topic, topic_length); + group_topic->topic = topic_copy; + group_topic->topic_length = topic_length; + return true; +} +uint32_t tox_event_group_topic_get_topic_length(const Tox_Event_Group_Topic *group_topic) +{ + assert(group_topic != nullptr); + return group_topic->topic_length; +} +const uint8_t *tox_event_group_topic_get_topic(const Tox_Event_Group_Topic *group_topic) +{ + assert(group_topic != nullptr); + return group_topic->topic; +} + +non_null() +static void tox_event_group_topic_construct(Tox_Event_Group_Topic *group_topic) +{ + *group_topic = (Tox_Event_Group_Topic) { + 0 + }; +} +non_null() +static void tox_event_group_topic_destruct(Tox_Event_Group_Topic *group_topic, const Memory *mem) +{ + free(group_topic->topic); +} + +bool tox_event_group_topic_pack( + const Tox_Event_Group_Topic *event, Bin_Pack *bp) +{ + assert(event != nullptr); + return bin_pack_array(bp, 2) + && bin_pack_u32(bp, TOX_EVENT_GROUP_TOPIC) + && bin_pack_array(bp, 3) + && bin_pack_u32(bp, event->group_number) + && bin_pack_u32(bp, event->peer_id) + && bin_pack_bin(bp, event->topic, event->topic_length); +} + +non_null() +static bool tox_event_group_topic_unpack_into( + Tox_Event_Group_Topic *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->topic, &event->topic_length); +} + + +/***************************************************** + * + * :: new/free/add/get/size/unpack + * + *****************************************************/ + +const Tox_Event_Group_Topic *tox_event_get_group_topic(const Tox_Event *event) +{ + return event->type == TOX_EVENT_GROUP_TOPIC ? event->data.group_topic : nullptr; +} + +Tox_Event_Group_Topic *tox_event_group_topic_new(const Memory *mem) +{ + Tox_Event_Group_Topic *const group_topic = + (Tox_Event_Group_Topic *)mem_alloc(mem, sizeof(Tox_Event_Group_Topic)); + + if (group_topic == nullptr) { + return nullptr; + } + + tox_event_group_topic_construct(group_topic); + return group_topic; +} + +void tox_event_group_topic_free(Tox_Event_Group_Topic *group_topic, const Memory *mem) +{ + if (group_topic != nullptr) { + tox_event_group_topic_destruct(group_topic, mem); + } + mem_delete(mem, group_topic); +} + +non_null() +static Tox_Event_Group_Topic *tox_events_add_group_topic(Tox_Events *events, const Memory *mem) +{ + Tox_Event_Group_Topic *const group_topic = tox_event_group_topic_new(mem); + + if (group_topic == nullptr) { + return nullptr; + } + + Tox_Event event; + event.type = TOX_EVENT_GROUP_TOPIC; + event.data.group_topic = group_topic; + + tox_events_add(events, &event); + return group_topic; +} + +bool tox_event_group_topic_unpack( + Tox_Event_Group_Topic **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_topic_new(mem); + + if (*event == nullptr) { + return false; + } + + return tox_event_group_topic_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Topic *tox_event_group_topic_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_Topic *group_topic = tox_events_add_group_topic(state->events, state->mem); + + if (group_topic == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_topic; +} + + +/***************************************************** + * + * :: event handler + * + *****************************************************/ + + +void tox_events_handle_group_topic(Tox *tox, uint32_t group_number, uint32_t peer_id, const uint8_t *topic, size_t length, + void *user_data) +{ + Tox_Event_Group_Topic *group_topic = tox_event_group_topic_alloc(user_data); + + if (group_topic == nullptr) { + return; + } + + tox_event_group_topic_set_group_number(group_topic, group_number); + tox_event_group_topic_set_peer_id(group_topic, peer_id); + tox_event_group_topic_set_topic(group_topic, topic, length); +} diff --git a/toxcore/events/group_topic_lock.c b/toxcore/events/group_topic_lock.c new file mode 100644 index 0000000000..17d71028e8 --- /dev/null +++ b/toxcore/events/group_topic_lock.c @@ -0,0 +1,195 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2023-2024 The TokTok team. + */ + +#include "events_alloc.h" + +#include + +#include "../bin_pack.h" +#include "../bin_unpack.h" +#include "../ccompat.h" +#include "../mem.h" +#include "../tox.h" +#include "../tox_events.h" +#include "../tox_unpack.h" + + +/***************************************************** + * + * :: struct and accessors + * + *****************************************************/ + + +struct Tox_Event_Group_Topic_Lock { + uint32_t group_number; + Tox_Group_Topic_Lock topic_lock; +}; + +non_null() +static void tox_event_group_topic_lock_set_group_number(Tox_Event_Group_Topic_Lock *group_topic_lock, + uint32_t group_number) +{ + assert(group_topic_lock != nullptr); + group_topic_lock->group_number = group_number; +} +uint32_t tox_event_group_topic_lock_get_group_number(const Tox_Event_Group_Topic_Lock *group_topic_lock) +{ + assert(group_topic_lock != nullptr); + return group_topic_lock->group_number; +} + +non_null() +static void tox_event_group_topic_lock_set_topic_lock(Tox_Event_Group_Topic_Lock *group_topic_lock, + Tox_Group_Topic_Lock topic_lock) +{ + assert(group_topic_lock != nullptr); + group_topic_lock->topic_lock = topic_lock; +} +Tox_Group_Topic_Lock tox_event_group_topic_lock_get_topic_lock(const Tox_Event_Group_Topic_Lock *group_topic_lock) +{ + assert(group_topic_lock != nullptr); + return group_topic_lock->topic_lock; +} + +non_null() +static void tox_event_group_topic_lock_construct(Tox_Event_Group_Topic_Lock *group_topic_lock) +{ + *group_topic_lock = (Tox_Event_Group_Topic_Lock) { + 0 + }; +} +non_null() +static void tox_event_group_topic_lock_destruct(Tox_Event_Group_Topic_Lock *group_topic_lock, const Memory *mem) +{ + return; +} + +bool tox_event_group_topic_lock_pack( + const Tox_Event_Group_Topic_Lock *event, Bin_Pack *bp) +{ + assert(event != nullptr); + return bin_pack_array(bp, 2) + && bin_pack_u32(bp, TOX_EVENT_GROUP_TOPIC_LOCK) + && bin_pack_array(bp, 2) + && bin_pack_u32(bp, event->group_number) + && bin_pack_u32(bp, event->topic_lock); +} + +non_null() +static bool tox_event_group_topic_lock_unpack_into( + Tox_Event_Group_Topic_Lock *event, Bin_Unpack *bu) +{ + assert(event != nullptr); + if (!bin_unpack_array_fixed(bu, 2, nullptr)) { + return false; + } + + return bin_unpack_u32(bu, &event->group_number) + && tox_group_topic_lock_unpack(bu, &event->topic_lock); +} + + +/***************************************************** + * + * :: new/free/add/get/size/unpack + * + *****************************************************/ + +const Tox_Event_Group_Topic_Lock *tox_event_get_group_topic_lock(const Tox_Event *event) +{ + return event->type == TOX_EVENT_GROUP_TOPIC_LOCK ? event->data.group_topic_lock : nullptr; +} + +Tox_Event_Group_Topic_Lock *tox_event_group_topic_lock_new(const Memory *mem) +{ + Tox_Event_Group_Topic_Lock *const group_topic_lock = + (Tox_Event_Group_Topic_Lock *)mem_alloc(mem, sizeof(Tox_Event_Group_Topic_Lock)); + + if (group_topic_lock == nullptr) { + return nullptr; + } + + tox_event_group_topic_lock_construct(group_topic_lock); + return group_topic_lock; +} + +void tox_event_group_topic_lock_free(Tox_Event_Group_Topic_Lock *group_topic_lock, const Memory *mem) +{ + if (group_topic_lock != nullptr) { + tox_event_group_topic_lock_destruct(group_topic_lock, mem); + } + mem_delete(mem, group_topic_lock); +} + +non_null() +static Tox_Event_Group_Topic_Lock *tox_events_add_group_topic_lock(Tox_Events *events, const Memory *mem) +{ + Tox_Event_Group_Topic_Lock *const group_topic_lock = tox_event_group_topic_lock_new(mem); + + if (group_topic_lock == nullptr) { + return nullptr; + } + + Tox_Event event; + event.type = TOX_EVENT_GROUP_TOPIC_LOCK; + event.data.group_topic_lock = group_topic_lock; + + tox_events_add(events, &event); + return group_topic_lock; +} + +bool tox_event_group_topic_lock_unpack( + Tox_Event_Group_Topic_Lock **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_topic_lock_new(mem); + + if (*event == nullptr) { + return false; + } + + return tox_event_group_topic_lock_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Topic_Lock *tox_event_group_topic_lock_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_Topic_Lock *group_topic_lock = tox_events_add_group_topic_lock(state->events, state->mem); + + if (group_topic_lock == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_topic_lock; +} + + +/***************************************************** + * + * :: event handler + * + *****************************************************/ + + +void tox_events_handle_group_topic_lock(Tox *tox, uint32_t group_number, Tox_Group_Topic_Lock topic_lock, + void *user_data) +{ + Tox_Event_Group_Topic_Lock *group_topic_lock = tox_event_group_topic_lock_alloc(user_data); + + if (group_topic_lock == nullptr) { + return; + } + + tox_event_group_topic_lock_set_group_number(group_topic_lock, group_number); + tox_event_group_topic_lock_set_topic_lock(group_topic_lock, topic_lock); +} diff --git a/toxcore/events/group_voice_state.c b/toxcore/events/group_voice_state.c new file mode 100644 index 0000000000..053c9ed89f --- /dev/null +++ b/toxcore/events/group_voice_state.c @@ -0,0 +1,195 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2023-2024 The TokTok team. + */ + +#include "events_alloc.h" + +#include + +#include "../bin_pack.h" +#include "../bin_unpack.h" +#include "../ccompat.h" +#include "../mem.h" +#include "../tox.h" +#include "../tox_events.h" +#include "../tox_unpack.h" + + +/***************************************************** + * + * :: struct and accessors + * + *****************************************************/ + + +struct Tox_Event_Group_Voice_State { + uint32_t group_number; + Tox_Group_Voice_State voice_state; +}; + +non_null() +static void tox_event_group_voice_state_set_group_number(Tox_Event_Group_Voice_State *group_voice_state, + uint32_t group_number) +{ + assert(group_voice_state != nullptr); + group_voice_state->group_number = group_number; +} +uint32_t tox_event_group_voice_state_get_group_number(const Tox_Event_Group_Voice_State *group_voice_state) +{ + assert(group_voice_state != nullptr); + return group_voice_state->group_number; +} + +non_null() +static void tox_event_group_voice_state_set_voice_state(Tox_Event_Group_Voice_State *group_voice_state, + Tox_Group_Voice_State voice_state) +{ + assert(group_voice_state != nullptr); + group_voice_state->voice_state = voice_state; +} +Tox_Group_Voice_State tox_event_group_voice_state_get_voice_state(const Tox_Event_Group_Voice_State *group_voice_state) +{ + assert(group_voice_state != nullptr); + return group_voice_state->voice_state; +} + +non_null() +static void tox_event_group_voice_state_construct(Tox_Event_Group_Voice_State *group_voice_state) +{ + *group_voice_state = (Tox_Event_Group_Voice_State) { + 0 + }; +} +non_null() +static void tox_event_group_voice_state_destruct(Tox_Event_Group_Voice_State *group_voice_state, const Memory *mem) +{ + return; +} + +bool tox_event_group_voice_state_pack( + const Tox_Event_Group_Voice_State *event, Bin_Pack *bp) +{ + assert(event != nullptr); + return bin_pack_array(bp, 2) + && bin_pack_u32(bp, TOX_EVENT_GROUP_VOICE_STATE) + && bin_pack_array(bp, 2) + && bin_pack_u32(bp, event->group_number) + && bin_pack_u32(bp, event->voice_state); +} + +non_null() +static bool tox_event_group_voice_state_unpack_into( + Tox_Event_Group_Voice_State *event, Bin_Unpack *bu) +{ + assert(event != nullptr); + if (!bin_unpack_array_fixed(bu, 2, nullptr)) { + return false; + } + + return bin_unpack_u32(bu, &event->group_number) + && tox_group_voice_state_unpack(bu, &event->voice_state); +} + + +/***************************************************** + * + * :: new/free/add/get/size/unpack + * + *****************************************************/ + +const Tox_Event_Group_Voice_State *tox_event_get_group_voice_state(const Tox_Event *event) +{ + return event->type == TOX_EVENT_GROUP_VOICE_STATE ? event->data.group_voice_state : nullptr; +} + +Tox_Event_Group_Voice_State *tox_event_group_voice_state_new(const Memory *mem) +{ + Tox_Event_Group_Voice_State *const group_voice_state = + (Tox_Event_Group_Voice_State *)mem_alloc(mem, sizeof(Tox_Event_Group_Voice_State)); + + if (group_voice_state == nullptr) { + return nullptr; + } + + tox_event_group_voice_state_construct(group_voice_state); + return group_voice_state; +} + +void tox_event_group_voice_state_free(Tox_Event_Group_Voice_State *group_voice_state, const Memory *mem) +{ + if (group_voice_state != nullptr) { + tox_event_group_voice_state_destruct(group_voice_state, mem); + } + mem_delete(mem, group_voice_state); +} + +non_null() +static Tox_Event_Group_Voice_State *tox_events_add_group_voice_state(Tox_Events *events, const Memory *mem) +{ + Tox_Event_Group_Voice_State *const group_voice_state = tox_event_group_voice_state_new(mem); + + if (group_voice_state == nullptr) { + return nullptr; + } + + Tox_Event event; + event.type = TOX_EVENT_GROUP_VOICE_STATE; + event.data.group_voice_state = group_voice_state; + + tox_events_add(events, &event); + return group_voice_state; +} + +bool tox_event_group_voice_state_unpack( + Tox_Event_Group_Voice_State **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_voice_state_new(mem); + + if (*event == nullptr) { + return false; + } + + return tox_event_group_voice_state_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Voice_State *tox_event_group_voice_state_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_Voice_State *group_voice_state = tox_events_add_group_voice_state(state->events, state->mem); + + if (group_voice_state == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_voice_state; +} + + +/***************************************************** + * + * :: event handler + * + *****************************************************/ + + +void tox_events_handle_group_voice_state(Tox *tox, uint32_t group_number, Tox_Group_Voice_State voice_state, + void *user_data) +{ + Tox_Event_Group_Voice_State *group_voice_state = tox_event_group_voice_state_alloc(user_data); + + if (group_voice_state == nullptr) { + return; + } + + tox_event_group_voice_state_set_group_number(group_voice_state, group_number); + tox_event_group_voice_state_set_voice_state(group_voice_state, voice_state); +} diff --git a/toxcore/events/self_connection_status.c b/toxcore/events/self_connection_status.c index 097319e93a..945f4bb6a2 100644 --- a/toxcore/events/self_connection_status.c +++ b/toxcore/events/self_connection_status.c @@ -1,15 +1,15 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" @@ -27,34 +27,32 @@ struct Tox_Event_Self_Connection_Status { }; non_null() -static void tox_event_self_connection_status_construct(Tox_Event_Self_Connection_Status *self_connection_status) +static void tox_event_self_connection_status_set_connection_status(Tox_Event_Self_Connection_Status *self_connection_status, + Tox_Connection connection_status) { - *self_connection_status = (Tox_Event_Self_Connection_Status) { - TOX_CONNECTION_NONE - }; + assert(self_connection_status != nullptr); + self_connection_status->connection_status = connection_status; } -non_null() -static void tox_event_self_connection_status_destruct(Tox_Event_Self_Connection_Status *self_connection_status) +Tox_Connection tox_event_self_connection_status_get_connection_status(const Tox_Event_Self_Connection_Status *self_connection_status) { - return; + assert(self_connection_status != nullptr); + return self_connection_status->connection_status; } non_null() -static void tox_event_self_connection_status_set_connection_status(Tox_Event_Self_Connection_Status - *self_connection_status, Tox_Connection connection_status) +static void tox_event_self_connection_status_construct(Tox_Event_Self_Connection_Status *self_connection_status) { - assert(self_connection_status != nullptr); - self_connection_status->connection_status = connection_status; + *self_connection_status = (Tox_Event_Self_Connection_Status) { + TOX_CONNECTION_NONE + }; } -Tox_Connection tox_event_self_connection_status_get_connection_status(const Tox_Event_Self_Connection_Status - *self_connection_status) +non_null() +static void tox_event_self_connection_status_destruct(Tox_Event_Self_Connection_Status *self_connection_status, const Memory *mem) { - assert(self_connection_status != nullptr); - return self_connection_status->connection_status; + return; } -non_null() -static bool tox_event_self_connection_status_pack( +bool tox_event_self_connection_status_pack( const Tox_Event_Self_Connection_Status *event, Bin_Pack *bp) { assert(event != nullptr); @@ -64,7 +62,7 @@ static bool tox_event_self_connection_status_pack( } non_null() -static bool tox_event_self_connection_status_unpack( +static bool tox_event_self_connection_status_unpack_into( Tox_Event_Self_Connection_Status *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -74,91 +72,84 @@ static bool tox_event_self_connection_status_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Self_Connection_Status *tox_events_add_self_connection_status(Tox_Events *events) +const Tox_Event_Self_Connection_Status *tox_event_get_self_connection_status(const Tox_Event *event) { - if (events->self_connection_status_size == UINT32_MAX) { - return nullptr; - } - - if (events->self_connection_status_size == events->self_connection_status_capacity) { - const uint32_t new_self_connection_status_capacity = events->self_connection_status_capacity * 2 + 1; - Tox_Event_Self_Connection_Status *new_self_connection_status = (Tox_Event_Self_Connection_Status *)realloc( - events->self_connection_status, new_self_connection_status_capacity * sizeof(Tox_Event_Self_Connection_Status)); + return event->type == TOX_EVENT_SELF_CONNECTION_STATUS ? event->data.self_connection_status : nullptr; +} - if (new_self_connection_status == nullptr) { - return nullptr; - } +Tox_Event_Self_Connection_Status *tox_event_self_connection_status_new(const Memory *mem) +{ + Tox_Event_Self_Connection_Status *const self_connection_status = + (Tox_Event_Self_Connection_Status *)mem_alloc(mem, sizeof(Tox_Event_Self_Connection_Status)); - events->self_connection_status = new_self_connection_status; - events->self_connection_status_capacity = new_self_connection_status_capacity; + if (self_connection_status == nullptr) { + return nullptr; } - Tox_Event_Self_Connection_Status *const self_connection_status = - &events->self_connection_status[events->self_connection_status_size]; tox_event_self_connection_status_construct(self_connection_status); - ++events->self_connection_status_size; return self_connection_status; } -void tox_events_clear_self_connection_status(Tox_Events *events) +void tox_event_self_connection_status_free(Tox_Event_Self_Connection_Status *self_connection_status, const Memory *mem) { - if (events == nullptr) { - return; + if (self_connection_status != nullptr) { + tox_event_self_connection_status_destruct(self_connection_status, mem); } - - for (uint32_t i = 0; i < events->self_connection_status_size; ++i) { - tox_event_self_connection_status_destruct(&events->self_connection_status[i]); - } - - free(events->self_connection_status); - events->self_connection_status = nullptr; - events->self_connection_status_size = 0; - events->self_connection_status_capacity = 0; + mem_delete(mem, self_connection_status); } -uint32_t tox_events_get_self_connection_status_size(const Tox_Events *events) +non_null() +static Tox_Event_Self_Connection_Status *tox_events_add_self_connection_status(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Self_Connection_Status *const self_connection_status = tox_event_self_connection_status_new(mem); + + if (self_connection_status == nullptr) { + return nullptr; } - return events->self_connection_status_size; -} + Tox_Event event; + event.type = TOX_EVENT_SELF_CONNECTION_STATUS; + event.data.self_connection_status = self_connection_status; -const Tox_Event_Self_Connection_Status *tox_events_get_self_connection_status(const Tox_Events *events, uint32_t index) -{ - assert(index < events->self_connection_status_size); - assert(events->self_connection_status != nullptr); - return &events->self_connection_status[index]; + tox_events_add(events, &event); + return self_connection_status; } -bool tox_events_pack_self_connection_status(const Tox_Events *events, Bin_Pack *bp) +bool tox_event_self_connection_status_unpack( + Tox_Event_Self_Connection_Status **event, Bin_Unpack *bu, const Memory *mem) { - const uint32_t size = tox_events_get_self_connection_status_size(events); + assert(event != nullptr); + *event = tox_event_self_connection_status_new(mem); - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_self_connection_status_pack(tox_events_get_self_connection_status(events, i), bp)) { - return false; - } + if (*event == nullptr) { + return false; } - return true; + + return tox_event_self_connection_status_unpack_into(*event, bu); } -bool tox_events_unpack_self_connection_status(Tox_Events *events, Bin_Unpack *bu) +non_null() +static Tox_Event_Self_Connection_Status *tox_event_self_connection_status_alloc(void *user_data) { - Tox_Event_Self_Connection_Status *event = tox_events_add_self_connection_status(events); + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); - if (event == nullptr) { - return false; + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Self_Connection_Status *self_connection_status = tox_events_add_self_connection_status(state->events, state->mem); + + if (self_connection_status == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; } - return tox_event_self_connection_status_unpack(event, bu); + return self_connection_status; } @@ -169,19 +160,12 @@ bool tox_events_unpack_self_connection_status(Tox_Events *events, Bin_Unpack *bu *****************************************************/ -void tox_events_handle_self_connection_status(Tox *tox, Tox_Connection connection_status, void *user_data) +void tox_events_handle_self_connection_status(Tox *tox, Tox_Connection connection_status, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Self_Connection_Status *self_connection_status = tox_events_add_self_connection_status(state->events); + Tox_Event_Self_Connection_Status *self_connection_status = tox_event_self_connection_status_alloc(user_data); if (self_connection_status == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/tox_dispatch.c b/toxcore/tox_dispatch.c index 25dafdcb36..bbf09493c5 100644 --- a/toxcore/tox_dispatch.c +++ b/toxcore/tox_dispatch.c @@ -7,7 +7,9 @@ #include #include "ccompat.h" +#include "events/events_alloc.h" // IWYU pragma: keep #include "tox.h" +#include "tox_event.h" #include "tox_events.h" struct Tox_Dispatch { @@ -32,6 +34,24 @@ struct Tox_Dispatch { tox_events_friend_status_message_cb *friend_status_message_callback; tox_events_friend_typing_cb *friend_typing_callback; tox_events_self_connection_status_cb *self_connection_status_callback; + tox_events_group_peer_name_cb *group_peer_name_callback; + tox_events_group_peer_status_cb *group_peer_status_callback; + tox_events_group_topic_cb *group_topic_callback; + tox_events_group_privacy_state_cb *group_privacy_state_callback; + tox_events_group_voice_state_cb *group_voice_state_callback; + tox_events_group_topic_lock_cb *group_topic_lock_callback; + tox_events_group_peer_limit_cb *group_peer_limit_callback; + tox_events_group_password_cb *group_password_callback; + tox_events_group_message_cb *group_message_callback; + tox_events_group_private_message_cb *group_private_message_callback; + tox_events_group_custom_packet_cb *group_custom_packet_callback; + tox_events_group_custom_private_packet_cb *group_custom_private_packet_callback; + tox_events_group_invite_cb *group_invite_callback; + tox_events_group_peer_join_cb *group_peer_join_callback; + tox_events_group_peer_exit_cb *group_peer_exit_callback; + tox_events_group_self_join_cb *group_self_join_callback; + tox_events_group_join_fail_cb *group_join_fail_callback; + tox_events_group_moderation_cb *group_moderation_callback; }; Tox_Dispatch *tox_dispatch_new(Tox_Err_Dispatch_New *error) @@ -167,322 +187,424 @@ void tox_events_callback_self_connection_status( { dispatch->self_connection_status_callback = callback; } +void tox_events_callback_group_peer_name( + Tox_Dispatch *dispatch, tox_events_group_peer_name_cb *callback) +{ + dispatch->group_peer_name_callback = callback; +} +void tox_events_callback_group_peer_status( + Tox_Dispatch *dispatch, tox_events_group_peer_status_cb *callback) +{ + dispatch->group_peer_status_callback = callback; +} +void tox_events_callback_group_topic( + Tox_Dispatch *dispatch, tox_events_group_topic_cb *callback) +{ + dispatch->group_topic_callback = callback; +} +void tox_events_callback_group_privacy_state( + Tox_Dispatch *dispatch, tox_events_group_privacy_state_cb *callback) +{ + dispatch->group_privacy_state_callback = callback; +} +void tox_events_callback_group_voice_state( + Tox_Dispatch *dispatch, tox_events_group_voice_state_cb *callback) +{ + dispatch->group_voice_state_callback = callback; +} +void tox_events_callback_group_topic_lock( + Tox_Dispatch *dispatch, tox_events_group_topic_lock_cb *callback) +{ + dispatch->group_topic_lock_callback = callback; +} +void tox_events_callback_group_peer_limit( + Tox_Dispatch *dispatch, tox_events_group_peer_limit_cb *callback) +{ + dispatch->group_peer_limit_callback = callback; +} +void tox_events_callback_group_password( + Tox_Dispatch *dispatch, tox_events_group_password_cb *callback) +{ + dispatch->group_password_callback = callback; +} +void tox_events_callback_group_message( + Tox_Dispatch *dispatch, tox_events_group_message_cb *callback) +{ + dispatch->group_message_callback = callback; +} +void tox_events_callback_group_private_message( + Tox_Dispatch *dispatch, tox_events_group_private_message_cb *callback) +{ + dispatch->group_private_message_callback = callback; +} +void tox_events_callback_group_custom_packet( + Tox_Dispatch *dispatch, tox_events_group_custom_packet_cb *callback) +{ + dispatch->group_custom_packet_callback = callback; +} +void tox_events_callback_group_custom_private_packet( + Tox_Dispatch *dispatch, tox_events_group_custom_private_packet_cb *callback) +{ + dispatch->group_custom_private_packet_callback = callback; +} +void tox_events_callback_group_invite( + Tox_Dispatch *dispatch, tox_events_group_invite_cb *callback) +{ + dispatch->group_invite_callback = callback; +} +void tox_events_callback_group_peer_join( + Tox_Dispatch *dispatch, tox_events_group_peer_join_cb *callback) +{ + dispatch->group_peer_join_callback = callback; +} +void tox_events_callback_group_peer_exit( + Tox_Dispatch *dispatch, tox_events_group_peer_exit_cb *callback) +{ + dispatch->group_peer_exit_callback = callback; +} +void tox_events_callback_group_self_join( + Tox_Dispatch *dispatch, tox_events_group_self_join_cb *callback) +{ + dispatch->group_self_join_callback = callback; +} +void tox_events_callback_group_join_fail( + Tox_Dispatch *dispatch, tox_events_group_join_fail_cb *callback) +{ + dispatch->group_join_fail_callback = callback; +} +void tox_events_callback_group_moderation( + Tox_Dispatch *dispatch, tox_events_group_moderation_cb *callback) +{ + dispatch->group_moderation_callback = callback; +} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_conference_connected( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) +non_null(1, 2) nullable(3, 4) +static void tox_dispatch_invoke_event(const Tox_Dispatch *dispatch, const Tox_Event *event, Tox *tox, void *user_data) { - const uint32_t size = tox_events_get_conference_connected_size(events); + switch (event->type) { + case TOX_EVENT_CONFERENCE_CONNECTED: { + if (dispatch->conference_connected_callback != nullptr) { + dispatch->conference_connected_callback(tox, event->data.conference_connected, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->conference_connected_callback != nullptr) { - dispatch->conference_connected_callback( - tox, tox_events_get_conference_connected(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_conference_invite( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_conference_invite_size(events); + case TOX_EVENT_CONFERENCE_INVITE: { + if (dispatch->conference_invite_callback != nullptr) { + dispatch->conference_invite_callback(tox, event->data.conference_invite, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->conference_invite_callback != nullptr) { - dispatch->conference_invite_callback( - tox, tox_events_get_conference_invite(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_conference_message( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_conference_message_size(events); + case TOX_EVENT_CONFERENCE_MESSAGE: { + if (dispatch->conference_message_callback != nullptr) { + dispatch->conference_message_callback(tox, event->data.conference_message, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->conference_message_callback != nullptr) { - dispatch->conference_message_callback( - tox, tox_events_get_conference_message(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_conference_peer_list_changed( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_conference_peer_list_changed_size(events); + case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED: { + if (dispatch->conference_peer_list_changed_callback != nullptr) { + dispatch->conference_peer_list_changed_callback(tox, event->data.conference_peer_list_changed, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->conference_peer_list_changed_callback != nullptr) { - dispatch->conference_peer_list_changed_callback( - tox, tox_events_get_conference_peer_list_changed(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_conference_peer_name( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_conference_peer_name_size(events); + case TOX_EVENT_CONFERENCE_PEER_NAME: { + if (dispatch->conference_peer_name_callback != nullptr) { + dispatch->conference_peer_name_callback(tox, event->data.conference_peer_name, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->conference_peer_name_callback != nullptr) { - dispatch->conference_peer_name_callback( - tox, tox_events_get_conference_peer_name(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_conference_title( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_conference_title_size(events); + case TOX_EVENT_CONFERENCE_TITLE: { + if (dispatch->conference_title_callback != nullptr) { + dispatch->conference_title_callback(tox, event->data.conference_title, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->conference_title_callback != nullptr) { - dispatch->conference_title_callback( - tox, tox_events_get_conference_title(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_file_chunk_request( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_file_chunk_request_size(events); + case TOX_EVENT_FILE_CHUNK_REQUEST: { + if (dispatch->file_chunk_request_callback != nullptr) { + dispatch->file_chunk_request_callback(tox, event->data.file_chunk_request, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->file_chunk_request_callback != nullptr) { - dispatch->file_chunk_request_callback( - tox, tox_events_get_file_chunk_request(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_file_recv( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_file_recv_size(events); + case TOX_EVENT_FILE_RECV_CHUNK: { + if (dispatch->file_recv_chunk_callback != nullptr) { + dispatch->file_recv_chunk_callback(tox, event->data.file_recv_chunk, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->file_recv_callback != nullptr) { - dispatch->file_recv_callback( - tox, tox_events_get_file_recv(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_file_recv_chunk( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_file_recv_chunk_size(events); + case TOX_EVENT_FILE_RECV_CONTROL: { + if (dispatch->file_recv_control_callback != nullptr) { + dispatch->file_recv_control_callback(tox, event->data.file_recv_control, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->file_recv_chunk_callback != nullptr) { - dispatch->file_recv_chunk_callback( - tox, tox_events_get_file_recv_chunk(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_file_recv_control( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_file_recv_control_size(events); + case TOX_EVENT_FILE_RECV: { + if (dispatch->file_recv_callback != nullptr) { + dispatch->file_recv_callback(tox, event->data.file_recv, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->file_recv_control_callback != nullptr) { - dispatch->file_recv_control_callback( - tox, tox_events_get_file_recv_control(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_connection_status( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_connection_status_size(events); + case TOX_EVENT_FRIEND_CONNECTION_STATUS: { + if (dispatch->friend_connection_status_callback != nullptr) { + dispatch->friend_connection_status_callback(tox, event->data.friend_connection_status, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_connection_status_callback != nullptr) { - dispatch->friend_connection_status_callback( - tox, tox_events_get_friend_connection_status(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_lossless_packet( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_lossless_packet_size(events); + case TOX_EVENT_FRIEND_LOSSLESS_PACKET: { + if (dispatch->friend_lossless_packet_callback != nullptr) { + dispatch->friend_lossless_packet_callback(tox, event->data.friend_lossless_packet, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_lossless_packet_callback != nullptr) { - dispatch->friend_lossless_packet_callback( - tox, tox_events_get_friend_lossless_packet(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_lossy_packet( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_lossy_packet_size(events); + case TOX_EVENT_FRIEND_LOSSY_PACKET: { + if (dispatch->friend_lossy_packet_callback != nullptr) { + dispatch->friend_lossy_packet_callback(tox, event->data.friend_lossy_packet, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_lossy_packet_callback != nullptr) { - dispatch->friend_lossy_packet_callback( - tox, tox_events_get_friend_lossy_packet(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_message( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_message_size(events); + case TOX_EVENT_FRIEND_MESSAGE: { + if (dispatch->friend_message_callback != nullptr) { + dispatch->friend_message_callback(tox, event->data.friend_message, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_message_callback != nullptr) { - dispatch->friend_message_callback( - tox, tox_events_get_friend_message(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_name( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_name_size(events); + case TOX_EVENT_FRIEND_NAME: { + if (dispatch->friend_name_callback != nullptr) { + dispatch->friend_name_callback(tox, event->data.friend_name, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_name_callback != nullptr) { - dispatch->friend_name_callback( - tox, tox_events_get_friend_name(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_read_receipt( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_read_receipt_size(events); + case TOX_EVENT_FRIEND_READ_RECEIPT: { + if (dispatch->friend_read_receipt_callback != nullptr) { + dispatch->friend_read_receipt_callback(tox, event->data.friend_read_receipt, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_read_receipt_callback != nullptr) { - dispatch->friend_read_receipt_callback( - tox, tox_events_get_friend_read_receipt(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_request( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_request_size(events); + case TOX_EVENT_FRIEND_REQUEST: { + if (dispatch->friend_request_callback != nullptr) { + dispatch->friend_request_callback(tox, event->data.friend_request, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_request_callback != nullptr) { - dispatch->friend_request_callback( - tox, tox_events_get_friend_request(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_status( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_status_size(events); + case TOX_EVENT_FRIEND_STATUS: { + if (dispatch->friend_status_callback != nullptr) { + dispatch->friend_status_callback(tox, event->data.friend_status, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_status_callback != nullptr) { - dispatch->friend_status_callback( - tox, tox_events_get_friend_status(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_status_message( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_status_message_size(events); + case TOX_EVENT_FRIEND_STATUS_MESSAGE: { + if (dispatch->friend_status_message_callback != nullptr) { + dispatch->friend_status_message_callback(tox, event->data.friend_status_message, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_status_message_callback != nullptr) { - dispatch->friend_status_message_callback( - tox, tox_events_get_friend_status_message(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_typing( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_typing_size(events); + case TOX_EVENT_FRIEND_TYPING: { + if (dispatch->friend_typing_callback != nullptr) { + dispatch->friend_typing_callback(tox, event->data.friend_typing, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_typing_callback != nullptr) { - dispatch->friend_typing_callback( - tox, tox_events_get_friend_typing(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_self_connection_status( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_self_connection_status_size(events); + case TOX_EVENT_SELF_CONNECTION_STATUS: { + if (dispatch->self_connection_status_callback != nullptr) { + dispatch->self_connection_status_callback(tox, event->data.self_connection_status, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->self_connection_status_callback != nullptr) { - dispatch->self_connection_status_callback( - tox, tox_events_get_self_connection_status(events, i), user_data); + break; + } + + case TOX_EVENT_GROUP_PEER_NAME: { + if (dispatch->group_peer_name_callback != nullptr) { + dispatch->group_peer_name_callback(tox, event->data.group_peer_name, user_data); + } + + break; + } + + case TOX_EVENT_GROUP_PEER_STATUS: { + if (dispatch->group_peer_status_callback != nullptr) { + dispatch->group_peer_status_callback(tox, event->data.group_peer_status, user_data); + } + + break; + } + + case TOX_EVENT_GROUP_TOPIC: { + if (dispatch->group_topic_callback != nullptr) { + dispatch->group_topic_callback(tox, event->data.group_topic, user_data); + } + + break; + } + + case TOX_EVENT_GROUP_PRIVACY_STATE: { + if (dispatch->group_privacy_state_callback != nullptr) { + dispatch->group_privacy_state_callback(tox, event->data.group_privacy_state, user_data); + } + + break; + } + + case TOX_EVENT_GROUP_VOICE_STATE: { + if (dispatch->group_voice_state_callback != nullptr) { + dispatch->group_voice_state_callback(tox, event->data.group_voice_state, user_data); + } + + break; + } + + case TOX_EVENT_GROUP_TOPIC_LOCK: { + if (dispatch->group_topic_lock_callback != nullptr) { + dispatch->group_topic_lock_callback(tox, event->data.group_topic_lock, user_data); + } + + break; + } + + case TOX_EVENT_GROUP_PEER_LIMIT: { + if (dispatch->group_peer_limit_callback != nullptr) { + dispatch->group_peer_limit_callback(tox, event->data.group_peer_limit, user_data); + } + + break; + } + + case TOX_EVENT_GROUP_PASSWORD: { + if (dispatch->group_password_callback != nullptr) { + dispatch->group_password_callback(tox, event->data.group_password, user_data); + } + + break; + } + + case TOX_EVENT_GROUP_MESSAGE: { + if (dispatch->group_message_callback != nullptr) { + dispatch->group_message_callback(tox, event->data.group_message, user_data); + } + + break; + } + + case TOX_EVENT_GROUP_PRIVATE_MESSAGE: { + if (dispatch->group_private_message_callback != nullptr) { + dispatch->group_private_message_callback(tox, event->data.group_private_message, user_data); + } + + break; + } + + case TOX_EVENT_GROUP_CUSTOM_PACKET: { + if (dispatch->group_custom_packet_callback != nullptr) { + dispatch->group_custom_packet_callback(tox, event->data.group_custom_packet, user_data); + } + + break; + } + + case TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET: { + if (dispatch->group_custom_private_packet_callback != nullptr) { + dispatch->group_custom_private_packet_callback(tox, event->data.group_custom_private_packet, user_data); + } + + break; + } + + case TOX_EVENT_GROUP_INVITE: { + if (dispatch->group_invite_callback != nullptr) { + dispatch->group_invite_callback(tox, event->data.group_invite, user_data); + } + + break; + } + + case TOX_EVENT_GROUP_PEER_JOIN: { + if (dispatch->group_peer_join_callback != nullptr) { + dispatch->group_peer_join_callback(tox, event->data.group_peer_join, user_data); + } + + break; + } + + case TOX_EVENT_GROUP_PEER_EXIT: { + if (dispatch->group_peer_exit_callback != nullptr) { + dispatch->group_peer_exit_callback(tox, event->data.group_peer_exit, user_data); + } + + break; + } + + case TOX_EVENT_GROUP_SELF_JOIN: { + if (dispatch->group_self_join_callback != nullptr) { + dispatch->group_self_join_callback(tox, event->data.group_self_join, user_data); + } + + break; + } + + case TOX_EVENT_GROUP_JOIN_FAIL: { + if (dispatch->group_join_fail_callback != nullptr) { + dispatch->group_join_fail_callback(tox, event->data.group_join_fail, user_data); + } + + break; + } + + case TOX_EVENT_GROUP_MODERATION: { + if (dispatch->group_moderation_callback != nullptr) { + dispatch->group_moderation_callback(tox, event->data.group_moderation, user_data); + } + + break; + } + + case TOX_EVENT_INVALID: { + break; } } } void tox_dispatch_invoke(const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) { - tox_dispatch_invoke_conference_connected(dispatch, events, tox, user_data); - tox_dispatch_invoke_conference_invite(dispatch, events, tox, user_data); - tox_dispatch_invoke_conference_message(dispatch, events, tox, user_data); - tox_dispatch_invoke_conference_peer_list_changed(dispatch, events, tox, user_data); - tox_dispatch_invoke_conference_peer_name(dispatch, events, tox, user_data); - tox_dispatch_invoke_conference_title(dispatch, events, tox, user_data); - tox_dispatch_invoke_file_chunk_request(dispatch, events, tox, user_data); - tox_dispatch_invoke_file_recv(dispatch, events, tox, user_data); - tox_dispatch_invoke_file_recv_chunk(dispatch, events, tox, user_data); - tox_dispatch_invoke_file_recv_control(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_connection_status(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_lossless_packet(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_lossy_packet(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_message(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_name(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_read_receipt(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_request(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_status(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_status_message(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_typing(dispatch, events, tox, user_data); - tox_dispatch_invoke_self_connection_status(dispatch, events, tox, user_data); + const uint32_t size = tox_events_get_size(events); + for (uint32_t i = 0; i < size; ++i) { + const Tox_Event *event = &events->events[i]; + tox_dispatch_invoke_event(dispatch, event, tox, user_data); + } } diff --git a/toxcore/tox_dispatch.h b/toxcore/tox_dispatch.h index 7e8fbba376..bf2df13994 100644 --- a/toxcore/tox_dispatch.h +++ b/toxcore/tox_dispatch.h @@ -93,6 +93,42 @@ typedef void tox_events_friend_typing_cb( Tox *tox, const Tox_Event_Friend_Typing *event, void *user_data); typedef void tox_events_self_connection_status_cb( Tox *tox, const Tox_Event_Self_Connection_Status *event, void *user_data); +typedef void tox_events_group_peer_name_cb( + Tox *tox, const Tox_Event_Group_Peer_Name *event, void *user_data); +typedef void tox_events_group_peer_status_cb( + Tox *tox, const Tox_Event_Group_Peer_Status *event, void *user_data); +typedef void tox_events_group_topic_cb( + Tox *tox, const Tox_Event_Group_Topic *event, void *user_data); +typedef void tox_events_group_privacy_state_cb( + Tox *tox, const Tox_Event_Group_Privacy_State *event, void *user_data); +typedef void tox_events_group_voice_state_cb( + Tox *tox, const Tox_Event_Group_Voice_State *event, void *user_data); +typedef void tox_events_group_topic_lock_cb( + Tox *tox, const Tox_Event_Group_Topic_Lock *event, void *user_data); +typedef void tox_events_group_peer_limit_cb( + Tox *tox, const Tox_Event_Group_Peer_Limit *event, void *user_data); +typedef void tox_events_group_password_cb( + Tox *tox, const Tox_Event_Group_Password *event, void *user_data); +typedef void tox_events_group_message_cb( + Tox *tox, const Tox_Event_Group_Message *event, void *user_data); +typedef void tox_events_group_private_message_cb( + Tox *tox, const Tox_Event_Group_Private_Message *event, void *user_data); +typedef void tox_events_group_custom_packet_cb( + Tox *tox, const Tox_Event_Group_Custom_Packet *event, void *user_data); +typedef void tox_events_group_custom_private_packet_cb( + Tox *tox, const Tox_Event_Group_Custom_Private_Packet *event, void *user_data); +typedef void tox_events_group_invite_cb( + Tox *tox, const Tox_Event_Group_Invite *event, void *user_data); +typedef void tox_events_group_peer_join_cb( + Tox *tox, const Tox_Event_Group_Peer_Join *event, void *user_data); +typedef void tox_events_group_peer_exit_cb( + Tox *tox, const Tox_Event_Group_Peer_Exit *event, void *user_data); +typedef void tox_events_group_self_join_cb( + Tox *tox, const Tox_Event_Group_Self_Join *event, void *user_data); +typedef void tox_events_group_join_fail_cb( + Tox *tox, const Tox_Event_Group_Join_Fail *event, void *user_data); +typedef void tox_events_group_moderation_cb( + Tox *tox, const Tox_Event_Group_Moderation *event, void *user_data); void tox_events_callback_conference_connected( Tox_Dispatch *dispatch, tox_events_conference_connected_cb *callback); @@ -136,6 +172,42 @@ void tox_events_callback_friend_typing( Tox_Dispatch *dispatch, tox_events_friend_typing_cb *callback); void tox_events_callback_self_connection_status( Tox_Dispatch *dispatch, tox_events_self_connection_status_cb *callback); +void tox_events_callback_group_peer_name( + Tox_Dispatch *dispatch, tox_events_group_peer_name_cb *callback); +void tox_events_callback_group_peer_status( + Tox_Dispatch *dispatch, tox_events_group_peer_status_cb *callback); +void tox_events_callback_group_topic( + Tox_Dispatch *dispatch, tox_events_group_topic_cb *callback); +void tox_events_callback_group_privacy_state( + Tox_Dispatch *dispatch, tox_events_group_privacy_state_cb *callback); +void tox_events_callback_group_voice_state( + Tox_Dispatch *dispatch, tox_events_group_voice_state_cb *callback); +void tox_events_callback_group_topic_lock( + Tox_Dispatch *dispatch, tox_events_group_topic_lock_cb *callback); +void tox_events_callback_group_peer_limit( + Tox_Dispatch *dispatch, tox_events_group_peer_limit_cb *callback); +void tox_events_callback_group_password( + Tox_Dispatch *dispatch, tox_events_group_password_cb *callback); +void tox_events_callback_group_message( + Tox_Dispatch *dispatch, tox_events_group_message_cb *callback); +void tox_events_callback_group_private_message( + Tox_Dispatch *dispatch, tox_events_group_private_message_cb *callback); +void tox_events_callback_group_custom_packet( + Tox_Dispatch *dispatch, tox_events_group_custom_packet_cb *callback); +void tox_events_callback_group_custom_private_packet( + Tox_Dispatch *dispatch, tox_events_group_custom_private_packet_cb *callback); +void tox_events_callback_group_invite( + Tox_Dispatch *dispatch, tox_events_group_invite_cb *callback); +void tox_events_callback_group_peer_join( + Tox_Dispatch *dispatch, tox_events_group_peer_join_cb *callback); +void tox_events_callback_group_peer_exit( + Tox_Dispatch *dispatch, tox_events_group_peer_exit_cb *callback); +void tox_events_callback_group_self_join( + Tox_Dispatch *dispatch, tox_events_group_self_join_cb *callback); +void tox_events_callback_group_join_fail( + Tox_Dispatch *dispatch, tox_events_group_join_fail_cb *callback); +void tox_events_callback_group_moderation( + Tox_Dispatch *dispatch, tox_events_group_moderation_cb *callback); #ifdef __cplusplus } diff --git a/toxcore/tox_event.c b/toxcore/tox_event.c new file mode 100644 index 0000000000..d7ac2a90b5 --- /dev/null +++ b/toxcore/tox_event.c @@ -0,0 +1,1056 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2022 The TokTok team. + */ + +#include "tox_event.h" + +#include + +#include "bin_pack.h" +#include "bin_unpack.h" +#include "ccompat.h" +#include "mem.h" +#include "tox_events.h" + +const char *tox_event_type_to_string(Tox_Event_Type type) +{ + switch (type) { + case TOX_EVENT_SELF_CONNECTION_STATUS: + return "TOX_EVENT_SELF_CONNECTION_STATUS"; + + case TOX_EVENT_FRIEND_REQUEST: + return "TOX_EVENT_FRIEND_REQUEST"; + + case TOX_EVENT_FRIEND_CONNECTION_STATUS: + return "TOX_EVENT_FRIEND_CONNECTION_STATUS"; + + case TOX_EVENT_FRIEND_LOSSY_PACKET: + return "TOX_EVENT_FRIEND_LOSSY_PACKET"; + + case TOX_EVENT_FRIEND_LOSSLESS_PACKET: + return "TOX_EVENT_FRIEND_LOSSLESS_PACKET"; + + case TOX_EVENT_FRIEND_NAME: + return "TOX_EVENT_FRIEND_NAME"; + + case TOX_EVENT_FRIEND_STATUS: + return "TOX_EVENT_FRIEND_STATUS"; + + case TOX_EVENT_FRIEND_STATUS_MESSAGE: + return "TOX_EVENT_FRIEND_STATUS_MESSAGE"; + + case TOX_EVENT_FRIEND_MESSAGE: + return "TOX_EVENT_FRIEND_MESSAGE"; + + case TOX_EVENT_FRIEND_READ_RECEIPT: + return "TOX_EVENT_FRIEND_READ_RECEIPT"; + + case TOX_EVENT_FRIEND_TYPING: + return "TOX_EVENT_FRIEND_TYPING"; + + case TOX_EVENT_FILE_CHUNK_REQUEST: + return "TOX_EVENT_FILE_CHUNK_REQUEST"; + + case TOX_EVENT_FILE_RECV: + return "TOX_EVENT_FILE_RECV"; + + case TOX_EVENT_FILE_RECV_CHUNK: + return "TOX_EVENT_FILE_RECV_CHUNK"; + + case TOX_EVENT_FILE_RECV_CONTROL: + return "TOX_EVENT_FILE_RECV_CONTROL"; + + case TOX_EVENT_CONFERENCE_INVITE: + return "TOX_EVENT_CONFERENCE_INVITE"; + + case TOX_EVENT_CONFERENCE_CONNECTED: + return "TOX_EVENT_CONFERENCE_CONNECTED"; + + case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED: + return "TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED"; + + case TOX_EVENT_CONFERENCE_PEER_NAME: + return "TOX_EVENT_CONFERENCE_PEER_NAME"; + + case TOX_EVENT_CONFERENCE_TITLE: + return "TOX_EVENT_CONFERENCE_TITLE"; + + case TOX_EVENT_CONFERENCE_MESSAGE: + return "TOX_EVENT_CONFERENCE_MESSAGE"; + + case TOX_EVENT_GROUP_PEER_NAME: + return "TOX_EVENT_GROUP_PEER_NAME"; + + case TOX_EVENT_GROUP_PEER_STATUS: + return "TOX_EVENT_GROUP_PEER_STATUS"; + + case TOX_EVENT_GROUP_TOPIC: + return "TOX_EVENT_GROUP_TOPIC"; + + case TOX_EVENT_GROUP_PRIVACY_STATE: + return "TOX_EVENT_GROUP_PRIVACY_STATE"; + + case TOX_EVENT_GROUP_VOICE_STATE: + return "TOX_EVENT_GROUP_VOICE_STATE"; + + case TOX_EVENT_GROUP_TOPIC_LOCK: + return "TOX_EVENT_GROUP_TOPIC_LOCK"; + + case TOX_EVENT_GROUP_PEER_LIMIT: + return "TOX_EVENT_GROUP_PEER_LIMIT"; + + case TOX_EVENT_GROUP_PASSWORD: + return "TOX_EVENT_GROUP_PASSWORD"; + + case TOX_EVENT_GROUP_MESSAGE: + return "TOX_EVENT_GROUP_MESSAGE"; + + case TOX_EVENT_GROUP_PRIVATE_MESSAGE: + return "TOX_EVENT_GROUP_PRIVATE_MESSAGE"; + + case TOX_EVENT_GROUP_CUSTOM_PACKET: + return "TOX_EVENT_GROUP_CUSTOM_PACKET"; + + case TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET: + return "TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET"; + + case TOX_EVENT_GROUP_INVITE: + return "TOX_EVENT_GROUP_INVITE"; + + case TOX_EVENT_GROUP_PEER_JOIN: + return "TOX_EVENT_GROUP_PEER_JOIN"; + + case TOX_EVENT_GROUP_PEER_EXIT: + return "TOX_EVENT_GROUP_PEER_EXIT"; + + case TOX_EVENT_GROUP_SELF_JOIN: + return "TOX_EVENT_GROUP_SELF_JOIN"; + + case TOX_EVENT_GROUP_JOIN_FAIL: + return "TOX_EVENT_GROUP_JOIN_FAIL"; + + case TOX_EVENT_GROUP_MODERATION: + return "TOX_EVENT_GROUP_MODERATION"; + + case TOX_EVENT_INVALID: + return "TOX_EVENT_INVALID"; + } + + return ""; +} + +Tox_Event_Type tox_event_get_type(const Tox_Event *event) +{ + assert(event != nullptr); + return event->type; +} + +bool tox_event_construct(Tox_Event *event, Tox_Event_Type type, const Memory *mem) +{ + event->type = type; + event->data.value = nullptr; + + switch (type) { + case TOX_EVENT_CONFERENCE_CONNECTED: { + event->data.conference_connected = tox_event_conference_connected_new(mem); + break; + } + + case TOX_EVENT_CONFERENCE_INVITE: { + event->data.conference_invite = tox_event_conference_invite_new(mem); + break; + } + + case TOX_EVENT_CONFERENCE_MESSAGE: { + event->data.conference_message = tox_event_conference_message_new(mem); + break; + } + + case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED: { + event->data.conference_peer_list_changed = tox_event_conference_peer_list_changed_new(mem); + break; + } + + case TOX_EVENT_CONFERENCE_PEER_NAME: { + event->data.conference_peer_name = tox_event_conference_peer_name_new(mem); + break; + } + + case TOX_EVENT_CONFERENCE_TITLE: { + event->data.conference_title = tox_event_conference_title_new(mem); + break; + } + + case TOX_EVENT_FILE_CHUNK_REQUEST: { + event->data.file_chunk_request = tox_event_file_chunk_request_new(mem); + break; + } + + case TOX_EVENT_FILE_RECV_CHUNK: { + event->data.file_recv_chunk = tox_event_file_recv_chunk_new(mem); + break; + } + + case TOX_EVENT_FILE_RECV_CONTROL: { + event->data.file_recv_control = tox_event_file_recv_control_new(mem); + break; + } + + case TOX_EVENT_FILE_RECV: { + event->data.file_recv = tox_event_file_recv_new(mem); + break; + } + + case TOX_EVENT_FRIEND_CONNECTION_STATUS: { + event->data.friend_connection_status = tox_event_friend_connection_status_new(mem); + break; + } + + case TOX_EVENT_FRIEND_LOSSLESS_PACKET: { + event->data.friend_lossless_packet = tox_event_friend_lossless_packet_new(mem); + break; + } + + case TOX_EVENT_FRIEND_LOSSY_PACKET: { + event->data.friend_lossy_packet = tox_event_friend_lossy_packet_new(mem); + break; + } + + case TOX_EVENT_FRIEND_MESSAGE: { + event->data.friend_message = tox_event_friend_message_new(mem); + break; + } + + case TOX_EVENT_FRIEND_NAME: { + event->data.friend_name = tox_event_friend_name_new(mem); + break; + } + + case TOX_EVENT_FRIEND_READ_RECEIPT: { + event->data.friend_read_receipt = tox_event_friend_read_receipt_new(mem); + break; + } + + case TOX_EVENT_FRIEND_REQUEST: { + event->data.friend_request = tox_event_friend_request_new(mem); + break; + } + + case TOX_EVENT_FRIEND_STATUS: { + event->data.friend_status = tox_event_friend_status_new(mem); + break; + } + + case TOX_EVENT_FRIEND_STATUS_MESSAGE: { + event->data.friend_status_message = tox_event_friend_status_message_new(mem); + break; + } + + case TOX_EVENT_FRIEND_TYPING: { + event->data.friend_typing = tox_event_friend_typing_new(mem); + break; + } + + case TOX_EVENT_SELF_CONNECTION_STATUS: { + event->data.self_connection_status = tox_event_self_connection_status_new(mem); + break; + } + + case TOX_EVENT_GROUP_PEER_NAME: { + event->data.group_peer_name = tox_event_group_peer_name_new(mem); + break; + } + + case TOX_EVENT_GROUP_PEER_STATUS: { + event->data.group_peer_status = tox_event_group_peer_status_new(mem); + break; + } + + case TOX_EVENT_GROUP_TOPIC: { + event->data.group_topic = tox_event_group_topic_new(mem); + break; + } + + case TOX_EVENT_GROUP_PRIVACY_STATE: { + event->data.group_privacy_state = tox_event_group_privacy_state_new(mem); + break; + } + + case TOX_EVENT_GROUP_VOICE_STATE: { + event->data.group_voice_state = tox_event_group_voice_state_new(mem); + break; + } + + case TOX_EVENT_GROUP_TOPIC_LOCK: { + event->data.group_topic_lock = tox_event_group_topic_lock_new(mem); + break; + } + + case TOX_EVENT_GROUP_PEER_LIMIT: { + event->data.group_peer_limit = tox_event_group_peer_limit_new(mem); + break; + } + + case TOX_EVENT_GROUP_PASSWORD: { + event->data.group_password = tox_event_group_password_new(mem); + break; + } + + case TOX_EVENT_GROUP_MESSAGE: { + event->data.group_message = tox_event_group_message_new(mem); + break; + } + + case TOX_EVENT_GROUP_PRIVATE_MESSAGE: { + event->data.group_private_message = tox_event_group_private_message_new(mem); + break; + } + + case TOX_EVENT_GROUP_CUSTOM_PACKET: { + event->data.group_custom_packet = tox_event_group_custom_packet_new(mem); + break; + } + + case TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET: { + event->data.group_custom_private_packet = tox_event_group_custom_private_packet_new(mem); + break; + } + + case TOX_EVENT_GROUP_INVITE: { + event->data.group_invite = tox_event_group_invite_new(mem); + break; + } + + case TOX_EVENT_GROUP_PEER_JOIN: { + event->data.group_peer_join = tox_event_group_peer_join_new(mem); + break; + } + + case TOX_EVENT_GROUP_PEER_EXIT: { + event->data.group_peer_exit = tox_event_group_peer_exit_new(mem); + break; + } + + case TOX_EVENT_GROUP_SELF_JOIN: { + event->data.group_self_join = tox_event_group_self_join_new(mem); + break; + } + + case TOX_EVENT_GROUP_JOIN_FAIL: { + event->data.group_join_fail = tox_event_group_join_fail_new(mem); + break; + } + + case TOX_EVENT_GROUP_MODERATION: { + event->data.group_moderation = tox_event_group_moderation_new(mem); + break; + } + + case TOX_EVENT_INVALID: { + return false; + } + } + + return event->data.value != nullptr; +} + +void tox_event_destruct(Tox_Event *event, const Memory *mem) +{ + if (event == nullptr) { + return; + } + + switch (event->type) { + case TOX_EVENT_CONFERENCE_CONNECTED: { + tox_event_conference_connected_free(event->data.conference_connected, mem); + break; + } + + case TOX_EVENT_CONFERENCE_INVITE: { + tox_event_conference_invite_free(event->data.conference_invite, mem); + break; + } + + case TOX_EVENT_CONFERENCE_MESSAGE: { + tox_event_conference_message_free(event->data.conference_message, mem); + break; + } + + case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED: { + tox_event_conference_peer_list_changed_free(event->data.conference_peer_list_changed, mem); + break; + } + + case TOX_EVENT_CONFERENCE_PEER_NAME: { + tox_event_conference_peer_name_free(event->data.conference_peer_name, mem); + break; + } + + case TOX_EVENT_CONFERENCE_TITLE: { + tox_event_conference_title_free(event->data.conference_title, mem); + break; + } + + case TOX_EVENT_FILE_CHUNK_REQUEST: { + tox_event_file_chunk_request_free(event->data.file_chunk_request, mem); + break; + } + + case TOX_EVENT_FILE_RECV_CHUNK: { + tox_event_file_recv_chunk_free(event->data.file_recv_chunk, mem); + break; + } + + case TOX_EVENT_FILE_RECV_CONTROL: { + tox_event_file_recv_control_free(event->data.file_recv_control, mem); + break; + } + + case TOX_EVENT_FILE_RECV: { + tox_event_file_recv_free(event->data.file_recv, mem); + break; + } + + case TOX_EVENT_FRIEND_CONNECTION_STATUS: { + tox_event_friend_connection_status_free(event->data.friend_connection_status, mem); + break; + } + + case TOX_EVENT_FRIEND_LOSSLESS_PACKET: { + tox_event_friend_lossless_packet_free(event->data.friend_lossless_packet, mem); + break; + } + + case TOX_EVENT_FRIEND_LOSSY_PACKET: { + tox_event_friend_lossy_packet_free(event->data.friend_lossy_packet, mem); + break; + } + + case TOX_EVENT_FRIEND_MESSAGE: { + tox_event_friend_message_free(event->data.friend_message, mem); + break; + } + + case TOX_EVENT_FRIEND_NAME: { + tox_event_friend_name_free(event->data.friend_name, mem); + break; + } + + case TOX_EVENT_FRIEND_READ_RECEIPT: { + tox_event_friend_read_receipt_free(event->data.friend_read_receipt, mem); + break; + } + + case TOX_EVENT_FRIEND_REQUEST: { + tox_event_friend_request_free(event->data.friend_request, mem); + break; + } + + case TOX_EVENT_FRIEND_STATUS: { + tox_event_friend_status_free(event->data.friend_status, mem); + break; + } + + case TOX_EVENT_FRIEND_STATUS_MESSAGE: { + tox_event_friend_status_message_free(event->data.friend_status_message, mem); + break; + } + + case TOX_EVENT_FRIEND_TYPING: { + tox_event_friend_typing_free(event->data.friend_typing, mem); + break; + } + + case TOX_EVENT_SELF_CONNECTION_STATUS: { + tox_event_self_connection_status_free(event->data.self_connection_status, mem); + break; + } + + case TOX_EVENT_GROUP_PEER_NAME: { + tox_event_group_peer_name_free(event->data.group_peer_name, mem); + break; + } + + case TOX_EVENT_GROUP_PEER_STATUS: { + tox_event_group_peer_status_free(event->data.group_peer_status, mem); + break; + } + + case TOX_EVENT_GROUP_TOPIC: { + tox_event_group_topic_free(event->data.group_topic, mem); + break; + } + + case TOX_EVENT_GROUP_PRIVACY_STATE: { + tox_event_group_privacy_state_free(event->data.group_privacy_state, mem); + break; + } + + case TOX_EVENT_GROUP_VOICE_STATE: { + tox_event_group_voice_state_free(event->data.group_voice_state, mem); + break; + } + + case TOX_EVENT_GROUP_TOPIC_LOCK: { + tox_event_group_topic_lock_free(event->data.group_topic_lock, mem); + break; + } + + case TOX_EVENT_GROUP_PEER_LIMIT: { + tox_event_group_peer_limit_free(event->data.group_peer_limit, mem); + break; + } + + case TOX_EVENT_GROUP_PASSWORD: { + tox_event_group_password_free(event->data.group_password, mem); + break; + } + + case TOX_EVENT_GROUP_MESSAGE: { + tox_event_group_message_free(event->data.group_message, mem); + break; + } + + case TOX_EVENT_GROUP_PRIVATE_MESSAGE: { + tox_event_group_private_message_free(event->data.group_private_message, mem); + break; + } + + case TOX_EVENT_GROUP_CUSTOM_PACKET: { + tox_event_group_custom_packet_free(event->data.group_custom_packet, mem); + break; + } + + case TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET: { + tox_event_group_custom_private_packet_free(event->data.group_custom_private_packet, mem); + break; + } + + case TOX_EVENT_GROUP_INVITE: { + tox_event_group_invite_free(event->data.group_invite, mem); + break; + } + + case TOX_EVENT_GROUP_PEER_JOIN: { + tox_event_group_peer_join_free(event->data.group_peer_join, mem); + break; + } + + case TOX_EVENT_GROUP_PEER_EXIT: { + tox_event_group_peer_exit_free(event->data.group_peer_exit, mem); + break; + } + + case TOX_EVENT_GROUP_SELF_JOIN: { + tox_event_group_self_join_free(event->data.group_self_join, mem); + break; + } + + case TOX_EVENT_GROUP_JOIN_FAIL: { + tox_event_group_join_fail_free(event->data.group_join_fail, mem); + break; + } + + case TOX_EVENT_GROUP_MODERATION: { + tox_event_group_moderation_free(event->data.group_moderation, mem); + break; + } + + case TOX_EVENT_INVALID: { + break; + } + } + + event->data.value = nullptr; +} + +bool tox_event_pack(const Tox_Event *event, Bin_Pack *bp) +{ + assert(event->type != TOX_EVENT_INVALID); + + switch (event->type) { + case TOX_EVENT_CONFERENCE_CONNECTED: + return tox_event_conference_connected_pack(event->data.conference_connected, bp); + + case TOX_EVENT_CONFERENCE_INVITE: + return tox_event_conference_invite_pack(event->data.conference_invite, bp); + + case TOX_EVENT_CONFERENCE_MESSAGE: + return tox_event_conference_message_pack(event->data.conference_message, bp); + + case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED: + return tox_event_conference_peer_list_changed_pack(event->data.conference_peer_list_changed, bp); + + case TOX_EVENT_CONFERENCE_PEER_NAME: + return tox_event_conference_peer_name_pack(event->data.conference_peer_name, bp); + + case TOX_EVENT_CONFERENCE_TITLE: + return tox_event_conference_title_pack(event->data.conference_title, bp); + + case TOX_EVENT_FILE_CHUNK_REQUEST: + return tox_event_file_chunk_request_pack(event->data.file_chunk_request, bp); + + case TOX_EVENT_FILE_RECV_CHUNK: + return tox_event_file_recv_chunk_pack(event->data.file_recv_chunk, bp); + + case TOX_EVENT_FILE_RECV_CONTROL: + return tox_event_file_recv_control_pack(event->data.file_recv_control, bp); + + case TOX_EVENT_FILE_RECV: + return tox_event_file_recv_pack(event->data.file_recv, bp); + + case TOX_EVENT_FRIEND_CONNECTION_STATUS: + return tox_event_friend_connection_status_pack(event->data.friend_connection_status, bp); + + case TOX_EVENT_FRIEND_LOSSLESS_PACKET: + return tox_event_friend_lossless_packet_pack(event->data.friend_lossless_packet, bp); + + case TOX_EVENT_FRIEND_LOSSY_PACKET: + return tox_event_friend_lossy_packet_pack(event->data.friend_lossy_packet, bp); + + case TOX_EVENT_FRIEND_MESSAGE: + return tox_event_friend_message_pack(event->data.friend_message, bp); + + case TOX_EVENT_FRIEND_NAME: + return tox_event_friend_name_pack(event->data.friend_name, bp); + + case TOX_EVENT_FRIEND_READ_RECEIPT: + return tox_event_friend_read_receipt_pack(event->data.friend_read_receipt, bp); + + case TOX_EVENT_FRIEND_REQUEST: + return tox_event_friend_request_pack(event->data.friend_request, bp); + + case TOX_EVENT_FRIEND_STATUS: + return tox_event_friend_status_pack(event->data.friend_status, bp); + + case TOX_EVENT_FRIEND_STATUS_MESSAGE: + return tox_event_friend_status_message_pack(event->data.friend_status_message, bp); + + case TOX_EVENT_FRIEND_TYPING: + return tox_event_friend_typing_pack(event->data.friend_typing, bp); + + case TOX_EVENT_SELF_CONNECTION_STATUS: + return tox_event_self_connection_status_pack(event->data.self_connection_status, bp); + + case TOX_EVENT_GROUP_PEER_NAME: + return tox_event_group_peer_name_pack(event->data.group_peer_name, bp); + + case TOX_EVENT_GROUP_PEER_STATUS: + return tox_event_group_peer_status_pack(event->data.group_peer_status, bp); + + case TOX_EVENT_GROUP_TOPIC: + return tox_event_group_topic_pack(event->data.group_topic, bp); + + case TOX_EVENT_GROUP_PRIVACY_STATE: + return tox_event_group_privacy_state_pack(event->data.group_privacy_state, bp); + + case TOX_EVENT_GROUP_VOICE_STATE: + return tox_event_group_voice_state_pack(event->data.group_voice_state, bp); + + case TOX_EVENT_GROUP_TOPIC_LOCK: + return tox_event_group_topic_lock_pack(event->data.group_topic_lock, bp); + + case TOX_EVENT_GROUP_PEER_LIMIT: + return tox_event_group_peer_limit_pack(event->data.group_peer_limit, bp); + + case TOX_EVENT_GROUP_PASSWORD: + return tox_event_group_password_pack(event->data.group_password, bp); + + case TOX_EVENT_GROUP_MESSAGE: + return tox_event_group_message_pack(event->data.group_message, bp); + + case TOX_EVENT_GROUP_PRIVATE_MESSAGE: + return tox_event_group_private_message_pack(event->data.group_private_message, bp); + + case TOX_EVENT_GROUP_CUSTOM_PACKET: + return tox_event_group_custom_packet_pack(event->data.group_custom_packet, bp); + + case TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET: + return tox_event_group_custom_private_packet_pack(event->data.group_custom_private_packet, bp); + + case TOX_EVENT_GROUP_INVITE: + return tox_event_group_invite_pack(event->data.group_invite, bp); + + case TOX_EVENT_GROUP_PEER_JOIN: + return tox_event_group_peer_join_pack(event->data.group_peer_join, bp); + + case TOX_EVENT_GROUP_PEER_EXIT: + return tox_event_group_peer_exit_pack(event->data.group_peer_exit, bp); + + case TOX_EVENT_GROUP_SELF_JOIN: + return tox_event_group_self_join_pack(event->data.group_self_join, bp); + + case TOX_EVENT_GROUP_JOIN_FAIL: + return tox_event_group_join_fail_pack(event->data.group_join_fail, bp); + + case TOX_EVENT_GROUP_MODERATION: + return tox_event_group_moderation_pack(event->data.group_moderation, bp); + + case TOX_EVENT_INVALID: + return false; + } + + return false; +} + +non_null() +static bool tox_event_type_from_int(uint32_t value, Tox_Event_Type *out) +{ + switch (value) { + case TOX_EVENT_SELF_CONNECTION_STATUS: { + *out = TOX_EVENT_SELF_CONNECTION_STATUS; + return true; + } + + case TOX_EVENT_FRIEND_REQUEST: { + *out = TOX_EVENT_FRIEND_REQUEST; + return true; + } + + case TOX_EVENT_FRIEND_CONNECTION_STATUS: { + *out = TOX_EVENT_FRIEND_CONNECTION_STATUS; + return true; + } + + case TOX_EVENT_FRIEND_LOSSY_PACKET: { + *out = TOX_EVENT_FRIEND_LOSSY_PACKET; + return true; + } + + case TOX_EVENT_FRIEND_LOSSLESS_PACKET: { + *out = TOX_EVENT_FRIEND_LOSSLESS_PACKET; + return true; + } + + case TOX_EVENT_FRIEND_NAME: { + *out = TOX_EVENT_FRIEND_NAME; + return true; + } + + case TOX_EVENT_FRIEND_STATUS: { + *out = TOX_EVENT_FRIEND_STATUS; + return true; + } + + case TOX_EVENT_FRIEND_STATUS_MESSAGE: { + *out = TOX_EVENT_FRIEND_STATUS_MESSAGE; + return true; + } + + case TOX_EVENT_FRIEND_MESSAGE: { + *out = TOX_EVENT_FRIEND_MESSAGE; + return true; + } + + case TOX_EVENT_FRIEND_READ_RECEIPT: { + *out = TOX_EVENT_FRIEND_READ_RECEIPT; + return true; + } + + case TOX_EVENT_FRIEND_TYPING: { + *out = TOX_EVENT_FRIEND_TYPING; + return true; + } + + case TOX_EVENT_FILE_CHUNK_REQUEST: { + *out = TOX_EVENT_FILE_CHUNK_REQUEST; + return true; + } + + case TOX_EVENT_FILE_RECV: { + *out = TOX_EVENT_FILE_RECV; + return true; + } + + case TOX_EVENT_FILE_RECV_CHUNK: { + *out = TOX_EVENT_FILE_RECV_CHUNK; + return true; + } + + case TOX_EVENT_FILE_RECV_CONTROL: { + *out = TOX_EVENT_FILE_RECV_CONTROL; + return true; + } + + case TOX_EVENT_CONFERENCE_INVITE: { + *out = TOX_EVENT_CONFERENCE_INVITE; + return true; + } + + case TOX_EVENT_CONFERENCE_CONNECTED: { + *out = TOX_EVENT_CONFERENCE_CONNECTED; + return true; + } + + case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED: { + *out = TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED; + return true; + } + + case TOX_EVENT_CONFERENCE_PEER_NAME: { + *out = TOX_EVENT_CONFERENCE_PEER_NAME; + return true; + } + + case TOX_EVENT_CONFERENCE_TITLE: { + *out = TOX_EVENT_CONFERENCE_TITLE; + return true; + } + + case TOX_EVENT_CONFERENCE_MESSAGE: { + *out = TOX_EVENT_CONFERENCE_MESSAGE; + return true; + } + + case TOX_EVENT_GROUP_PEER_NAME: { + *out = TOX_EVENT_GROUP_PEER_NAME; + return true; + } + + case TOX_EVENT_GROUP_PEER_STATUS: { + *out = TOX_EVENT_GROUP_PEER_STATUS; + return true; + } + + case TOX_EVENT_GROUP_TOPIC: { + *out = TOX_EVENT_GROUP_TOPIC; + return true; + } + + case TOX_EVENT_GROUP_PRIVACY_STATE: { + *out = TOX_EVENT_GROUP_PRIVACY_STATE; + return true; + } + + case TOX_EVENT_GROUP_VOICE_STATE: { + *out = TOX_EVENT_GROUP_VOICE_STATE; + return true; + } + + case TOX_EVENT_GROUP_TOPIC_LOCK: { + *out = TOX_EVENT_GROUP_TOPIC_LOCK; + return true; + } + + case TOX_EVENT_GROUP_PEER_LIMIT: { + *out = TOX_EVENT_GROUP_PEER_LIMIT; + return true; + } + + case TOX_EVENT_GROUP_PASSWORD: { + *out = TOX_EVENT_GROUP_PASSWORD; + return true; + } + + case TOX_EVENT_GROUP_MESSAGE: { + *out = TOX_EVENT_GROUP_MESSAGE; + return true; + } + + case TOX_EVENT_GROUP_PRIVATE_MESSAGE: { + *out = TOX_EVENT_GROUP_PRIVATE_MESSAGE; + return true; + } + + case TOX_EVENT_GROUP_CUSTOM_PACKET: { + *out = TOX_EVENT_GROUP_CUSTOM_PACKET; + return true; + } + + case TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET: { + *out = TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET; + return true; + } + + case TOX_EVENT_GROUP_INVITE: { + *out = TOX_EVENT_GROUP_INVITE; + return true; + } + + case TOX_EVENT_GROUP_PEER_JOIN: { + *out = TOX_EVENT_GROUP_PEER_JOIN; + return true; + } + + case TOX_EVENT_GROUP_PEER_EXIT: { + *out = TOX_EVENT_GROUP_PEER_EXIT; + return true; + } + + case TOX_EVENT_GROUP_SELF_JOIN: { + *out = TOX_EVENT_GROUP_SELF_JOIN; + return true; + } + + case TOX_EVENT_GROUP_JOIN_FAIL: { + *out = TOX_EVENT_GROUP_JOIN_FAIL; + return true; + } + + case TOX_EVENT_GROUP_MODERATION: { + *out = TOX_EVENT_GROUP_MODERATION; + return true; + } + + case TOX_EVENT_INVALID: { + *out = TOX_EVENT_INVALID; + return true; + } + + default: { + *out = TOX_EVENT_INVALID; + return false; + } + } +} + +non_null() +static bool tox_event_type_unpack(Bin_Unpack *bu, Tox_Event_Type *val) +{ + uint32_t u32; + return bin_unpack_u32(bu, &u32) + && tox_event_type_from_int(u32, val); +} + +bool tox_event_unpack_into(Tox_Event *event, Bin_Unpack *bu, const Memory *mem) +{ + uint32_t size; + if (!bin_unpack_array(bu, &size)) { + return false; + } + + if (size != 2) { + return false; + } + + Tox_Event_Type type; + if (!tox_event_type_unpack(bu, &type)) { + return false; + } + + event->type = type; + + switch (type) { + case TOX_EVENT_CONFERENCE_CONNECTED: + return tox_event_conference_connected_unpack(&event->data.conference_connected, bu, mem); + + case TOX_EVENT_CONFERENCE_INVITE: + return tox_event_conference_invite_unpack(&event->data.conference_invite, bu, mem); + + case TOX_EVENT_CONFERENCE_MESSAGE: + return tox_event_conference_message_unpack(&event->data.conference_message, bu, mem); + + case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED: + return tox_event_conference_peer_list_changed_unpack(&event->data.conference_peer_list_changed, bu, mem); + + case TOX_EVENT_CONFERENCE_PEER_NAME: + return tox_event_conference_peer_name_unpack(&event->data.conference_peer_name, bu, mem); + + case TOX_EVENT_CONFERENCE_TITLE: + return tox_event_conference_title_unpack(&event->data.conference_title, bu, mem); + + case TOX_EVENT_FILE_CHUNK_REQUEST: + return tox_event_file_chunk_request_unpack(&event->data.file_chunk_request, bu, mem); + + case TOX_EVENT_FILE_RECV_CHUNK: + return tox_event_file_recv_chunk_unpack(&event->data.file_recv_chunk, bu, mem); + + case TOX_EVENT_FILE_RECV_CONTROL: + return tox_event_file_recv_control_unpack(&event->data.file_recv_control, bu, mem); + + case TOX_EVENT_FILE_RECV: + return tox_event_file_recv_unpack(&event->data.file_recv, bu, mem); + + case TOX_EVENT_FRIEND_CONNECTION_STATUS: + return tox_event_friend_connection_status_unpack(&event->data.friend_connection_status, bu, mem); + + case TOX_EVENT_FRIEND_LOSSLESS_PACKET: + return tox_event_friend_lossless_packet_unpack(&event->data.friend_lossless_packet, bu, mem); + + case TOX_EVENT_FRIEND_LOSSY_PACKET: + return tox_event_friend_lossy_packet_unpack(&event->data.friend_lossy_packet, bu, mem); + + case TOX_EVENT_FRIEND_MESSAGE: + return tox_event_friend_message_unpack(&event->data.friend_message, bu, mem); + + case TOX_EVENT_FRIEND_NAME: + return tox_event_friend_name_unpack(&event->data.friend_name, bu, mem); + + case TOX_EVENT_FRIEND_READ_RECEIPT: + return tox_event_friend_read_receipt_unpack(&event->data.friend_read_receipt, bu, mem); + + case TOX_EVENT_FRIEND_REQUEST: + return tox_event_friend_request_unpack(&event->data.friend_request, bu, mem); + + case TOX_EVENT_FRIEND_STATUS_MESSAGE: + return tox_event_friend_status_message_unpack(&event->data.friend_status_message, bu, mem); + + case TOX_EVENT_FRIEND_STATUS: + return tox_event_friend_status_unpack(&event->data.friend_status, bu, mem); + + case TOX_EVENT_FRIEND_TYPING: + return tox_event_friend_typing_unpack(&event->data.friend_typing, bu, mem); + + case TOX_EVENT_SELF_CONNECTION_STATUS: + return tox_event_self_connection_status_unpack(&event->data.self_connection_status, bu, mem); + + case TOX_EVENT_GROUP_PEER_NAME: + return tox_event_group_peer_name_unpack(&event->data.group_peer_name, bu, mem); + + case TOX_EVENT_GROUP_PEER_STATUS: + return tox_event_group_peer_status_unpack(&event->data.group_peer_status, bu, mem); + + case TOX_EVENT_GROUP_TOPIC: + return tox_event_group_topic_unpack(&event->data.group_topic, bu, mem); + + case TOX_EVENT_GROUP_PRIVACY_STATE: + return tox_event_group_privacy_state_unpack(&event->data.group_privacy_state, bu, mem); + + case TOX_EVENT_GROUP_VOICE_STATE: + return tox_event_group_voice_state_unpack(&event->data.group_voice_state, bu, mem); + + case TOX_EVENT_GROUP_TOPIC_LOCK: + return tox_event_group_topic_lock_unpack(&event->data.group_topic_lock, bu, mem); + + case TOX_EVENT_GROUP_PEER_LIMIT: + return tox_event_group_peer_limit_unpack(&event->data.group_peer_limit, bu, mem); + + case TOX_EVENT_GROUP_PASSWORD: + return tox_event_group_password_unpack(&event->data.group_password, bu, mem); + + case TOX_EVENT_GROUP_MESSAGE: + return tox_event_group_message_unpack(&event->data.group_message, bu, mem); + + case TOX_EVENT_GROUP_PRIVATE_MESSAGE: + return tox_event_group_private_message_unpack(&event->data.group_private_message, bu, mem); + + case TOX_EVENT_GROUP_CUSTOM_PACKET: + return tox_event_group_custom_packet_unpack(&event->data.group_custom_packet, bu, mem); + + case TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET: + return tox_event_group_custom_private_packet_unpack(&event->data.group_custom_private_packet, bu, mem); + + case TOX_EVENT_GROUP_INVITE: + return tox_event_group_invite_unpack(&event->data.group_invite, bu, mem); + + case TOX_EVENT_GROUP_PEER_JOIN: + return tox_event_group_peer_join_unpack(&event->data.group_peer_join, bu, mem); + + case TOX_EVENT_GROUP_PEER_EXIT: + return tox_event_group_peer_exit_unpack(&event->data.group_peer_exit, bu, mem); + + case TOX_EVENT_GROUP_SELF_JOIN: + return tox_event_group_self_join_unpack(&event->data.group_self_join, bu, mem); + + case TOX_EVENT_GROUP_JOIN_FAIL: + return tox_event_group_join_fail_unpack(&event->data.group_join_fail, bu, mem); + + case TOX_EVENT_GROUP_MODERATION: + return tox_event_group_moderation_unpack(&event->data.group_moderation, bu, mem); + + case TOX_EVENT_INVALID: + return false; + } + + return false; +} diff --git a/toxcore/tox_event.h b/toxcore/tox_event.h new file mode 100644 index 0000000000..c28ba79bfc --- /dev/null +++ b/toxcore/tox_event.h @@ -0,0 +1,254 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2022 The TokTok team. + */ + +#ifndef C_TOXCORE_TOXCORE_TOX_EVENT_H +#define C_TOXCORE_TOXCORE_TOX_EVENT_H + +#include "attributes.h" +#include "bin_pack.h" +#include "bin_unpack.h" +#include "mem.h" +#include "tox_events.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef union Tox_Event_Data { + /** + * Opaque pointer just to check whether any value is set. + */ + void *value; + + Tox_Event_Conference_Connected *conference_connected; + Tox_Event_Conference_Invite *conference_invite; + Tox_Event_Conference_Message *conference_message; + Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed; + Tox_Event_Conference_Peer_Name *conference_peer_name; + Tox_Event_Conference_Title *conference_title; + Tox_Event_File_Chunk_Request *file_chunk_request; + Tox_Event_File_Recv *file_recv; + Tox_Event_File_Recv_Chunk *file_recv_chunk; + Tox_Event_File_Recv_Control *file_recv_control; + Tox_Event_Friend_Connection_Status *friend_connection_status; + Tox_Event_Friend_Lossless_Packet *friend_lossless_packet; + Tox_Event_Friend_Lossy_Packet *friend_lossy_packet; + Tox_Event_Friend_Message *friend_message; + Tox_Event_Friend_Name *friend_name; + Tox_Event_Friend_Read_Receipt *friend_read_receipt; + Tox_Event_Friend_Request *friend_request; + Tox_Event_Friend_Status *friend_status; + Tox_Event_Friend_Status_Message *friend_status_message; + Tox_Event_Friend_Typing *friend_typing; + Tox_Event_Self_Connection_Status *self_connection_status; + Tox_Event_Group_Peer_Name *group_peer_name; + Tox_Event_Group_Peer_Status *group_peer_status; + Tox_Event_Group_Topic *group_topic; + Tox_Event_Group_Privacy_State *group_privacy_state; + Tox_Event_Group_Voice_State *group_voice_state; + Tox_Event_Group_Topic_Lock *group_topic_lock; + Tox_Event_Group_Peer_Limit *group_peer_limit; + Tox_Event_Group_Password *group_password; + Tox_Event_Group_Message *group_message; + Tox_Event_Group_Private_Message *group_private_message; + Tox_Event_Group_Custom_Packet *group_custom_packet; + Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet; + Tox_Event_Group_Invite *group_invite; + Tox_Event_Group_Peer_Join *group_peer_join; + Tox_Event_Group_Peer_Exit *group_peer_exit; + Tox_Event_Group_Self_Join *group_self_join; + Tox_Event_Group_Join_Fail *group_join_fail; + Tox_Event_Group_Moderation *group_moderation; +} Tox_Event_Data; + +struct Tox_Event { + Tox_Event_Type type; + Tox_Event_Data data; +}; + +/** + * Constructor. + */ +non_null() bool tox_event_construct(Tox_Event *event, Tox_Event_Type type, const Memory *mem); + +non_null() Tox_Event_Conference_Connected *tox_event_conference_connected_new(const Memory *mem); +non_null() Tox_Event_Conference_Invite *tox_event_conference_invite_new(const Memory *mem); +non_null() Tox_Event_Conference_Message *tox_event_conference_message_new(const Memory *mem); +non_null() Tox_Event_Conference_Peer_List_Changed *tox_event_conference_peer_list_changed_new(const Memory *mem); +non_null() Tox_Event_Conference_Peer_Name *tox_event_conference_peer_name_new(const Memory *mem); +non_null() Tox_Event_Conference_Title *tox_event_conference_title_new(const Memory *mem); +non_null() Tox_Event_File_Chunk_Request *tox_event_file_chunk_request_new(const Memory *mem); +non_null() Tox_Event_File_Recv_Chunk *tox_event_file_recv_chunk_new(const Memory *mem); +non_null() Tox_Event_File_Recv_Control *tox_event_file_recv_control_new(const Memory *mem); +non_null() Tox_Event_File_Recv *tox_event_file_recv_new(const Memory *mem); +non_null() Tox_Event_Friend_Connection_Status *tox_event_friend_connection_status_new(const Memory *mem); +non_null() Tox_Event_Friend_Lossless_Packet *tox_event_friend_lossless_packet_new(const Memory *mem); +non_null() Tox_Event_Friend_Lossy_Packet *tox_event_friend_lossy_packet_new(const Memory *mem); +non_null() Tox_Event_Friend_Message *tox_event_friend_message_new(const Memory *mem); +non_null() Tox_Event_Friend_Name *tox_event_friend_name_new(const Memory *mem); +non_null() Tox_Event_Friend_Read_Receipt *tox_event_friend_read_receipt_new(const Memory *mem); +non_null() Tox_Event_Friend_Request *tox_event_friend_request_new(const Memory *mem); +non_null() Tox_Event_Friend_Status_Message *tox_event_friend_status_message_new(const Memory *mem); +non_null() Tox_Event_Friend_Status *tox_event_friend_status_new(const Memory *mem); +non_null() Tox_Event_Friend_Typing *tox_event_friend_typing_new(const Memory *mem); +non_null() Tox_Event_Self_Connection_Status *tox_event_self_connection_status_new(const Memory *mem); +non_null() Tox_Event_Group_Peer_Name *tox_event_group_peer_name_new(const Memory *mem); +non_null() Tox_Event_Group_Peer_Status *tox_event_group_peer_status_new(const Memory *mem); +non_null() Tox_Event_Group_Topic *tox_event_group_topic_new(const Memory *mem); +non_null() Tox_Event_Group_Privacy_State *tox_event_group_privacy_state_new(const Memory *mem); +non_null() Tox_Event_Group_Voice_State *tox_event_group_voice_state_new(const Memory *mem); +non_null() Tox_Event_Group_Topic_Lock *tox_event_group_topic_lock_new(const Memory *mem); +non_null() Tox_Event_Group_Peer_Limit *tox_event_group_peer_limit_new(const Memory *mem); +non_null() Tox_Event_Group_Password *tox_event_group_password_new(const Memory *mem); +non_null() Tox_Event_Group_Message *tox_event_group_message_new(const Memory *mem); +non_null() Tox_Event_Group_Private_Message *tox_event_group_private_message_new(const Memory *mem); +non_null() Tox_Event_Group_Custom_Packet *tox_event_group_custom_packet_new(const Memory *mem); +non_null() Tox_Event_Group_Custom_Private_Packet *tox_event_group_custom_private_packet_new(const Memory *mem); +non_null() Tox_Event_Group_Invite *tox_event_group_invite_new(const Memory *mem); +non_null() Tox_Event_Group_Peer_Join *tox_event_group_peer_join_new(const Memory *mem); +non_null() Tox_Event_Group_Peer_Exit *tox_event_group_peer_exit_new(const Memory *mem); +non_null() Tox_Event_Group_Self_Join *tox_event_group_self_join_new(const Memory *mem); +non_null() Tox_Event_Group_Join_Fail *tox_event_group_join_fail_new(const Memory *mem); +non_null() Tox_Event_Group_Moderation *tox_event_group_moderation_new(const Memory *mem); + +/** + * Destructor. + */ +non_null(2) nullable(1) void tox_event_destruct(Tox_Event *event, const Memory *mem); + +non_null(2) nullable(1) void tox_event_conference_connected_free(Tox_Event_Conference_Connected *conference_connected, const Memory *mem); +non_null(2) nullable(1) void tox_event_conference_invite_free(Tox_Event_Conference_Invite *conference_invite, const Memory *mem); +non_null(2) nullable(1) void tox_event_conference_message_free(Tox_Event_Conference_Message *conference_message, const Memory *mem); +non_null(2) nullable(1) void tox_event_conference_peer_list_changed_free(Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed, const Memory *mem); +non_null(2) nullable(1) void tox_event_conference_peer_name_free(Tox_Event_Conference_Peer_Name *conference_peer_name, const Memory *mem); +non_null(2) nullable(1) void tox_event_conference_title_free(Tox_Event_Conference_Title *conference_title, const Memory *mem); +non_null(2) nullable(1) void tox_event_file_chunk_request_free(Tox_Event_File_Chunk_Request *file_chunk_request, const Memory *mem); +non_null(2) nullable(1) void tox_event_file_recv_chunk_free(Tox_Event_File_Recv_Chunk *file_recv_chunk, const Memory *mem); +non_null(2) nullable(1) void tox_event_file_recv_control_free(Tox_Event_File_Recv_Control *file_recv_control, const Memory *mem); +non_null(2) nullable(1) void tox_event_file_recv_free(Tox_Event_File_Recv *file_recv, const Memory *mem); +non_null(2) nullable(1) void tox_event_friend_connection_status_free(Tox_Event_Friend_Connection_Status *friend_connection_status, const Memory *mem); +non_null(2) nullable(1) void tox_event_friend_lossless_packet_free(Tox_Event_Friend_Lossless_Packet *friend_lossless_packet, const Memory *mem); +non_null(2) nullable(1) void tox_event_friend_lossy_packet_free(Tox_Event_Friend_Lossy_Packet *friend_lossy_packet, const Memory *mem); +non_null(2) nullable(1) void tox_event_friend_message_free(Tox_Event_Friend_Message *friend_message, const Memory *mem); +non_null(2) nullable(1) void tox_event_friend_name_free(Tox_Event_Friend_Name *friend_name, const Memory *mem); +non_null(2) nullable(1) void tox_event_friend_read_receipt_free(Tox_Event_Friend_Read_Receipt *friend_read_receipt, const Memory *mem); +non_null(2) nullable(1) void tox_event_friend_request_free(Tox_Event_Friend_Request *friend_request, const Memory *mem); +non_null(2) nullable(1) void tox_event_friend_status_message_free(Tox_Event_Friend_Status_Message *friend_status_message, const Memory *mem); +non_null(2) nullable(1) void tox_event_friend_status_free(Tox_Event_Friend_Status *friend_status, const Memory *mem); +non_null(2) nullable(1) void tox_event_friend_typing_free(Tox_Event_Friend_Typing *friend_typing, const Memory *mem); +non_null(2) nullable(1) void tox_event_self_connection_status_free(Tox_Event_Self_Connection_Status *self_connection_status, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_peer_name_free(Tox_Event_Group_Peer_Name *group_peer_name, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_peer_status_free(Tox_Event_Group_Peer_Status *group_peer_status, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_topic_free(Tox_Event_Group_Topic *group_topic, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_privacy_state_free(Tox_Event_Group_Privacy_State *group_privacy_state, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_voice_state_free(Tox_Event_Group_Voice_State *group_voice_state, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_topic_lock_free(Tox_Event_Group_Topic_Lock *group_topic_lock, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_peer_limit_free(Tox_Event_Group_Peer_Limit *group_peer_limit, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_password_free(Tox_Event_Group_Password *group_password, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_message_free(Tox_Event_Group_Message *group_message, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_private_message_free(Tox_Event_Group_Private_Message *group_private_message, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_custom_packet_free(Tox_Event_Group_Custom_Packet *group_custom_packet, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_custom_private_packet_free(Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_invite_free(Tox_Event_Group_Invite *group_invite, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_peer_join_free(Tox_Event_Group_Peer_Join *group_peer_join, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_peer_exit_free(Tox_Event_Group_Peer_Exit *group_peer_exit, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_self_join_free(Tox_Event_Group_Self_Join *group_self_join, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_join_fail_free(Tox_Event_Group_Join_Fail *group_join_fail, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_moderation_free(Tox_Event_Group_Moderation *group_moderation, const Memory *mem); + +/** + * Pack into msgpack. + */ +non_null() bool tox_event_pack(const Tox_Event *event, Bin_Pack *bp); + +non_null() bool tox_event_conference_connected_pack(const Tox_Event_Conference_Connected *event, Bin_Pack *bp); +non_null() bool tox_event_conference_invite_pack(const Tox_Event_Conference_Invite *event, Bin_Pack *bp); +non_null() bool tox_event_conference_message_pack(const Tox_Event_Conference_Message *event, Bin_Pack *bp); +non_null() bool tox_event_conference_peer_list_changed_pack(const Tox_Event_Conference_Peer_List_Changed *event, Bin_Pack *bp); +non_null() bool tox_event_conference_peer_name_pack(const Tox_Event_Conference_Peer_Name *event, Bin_Pack *bp); +non_null() bool tox_event_conference_title_pack(const Tox_Event_Conference_Title *event, Bin_Pack *bp); +non_null() bool tox_event_file_chunk_request_pack(const Tox_Event_File_Chunk_Request *event, Bin_Pack *bp); +non_null() bool tox_event_file_recv_chunk_pack(const Tox_Event_File_Recv_Chunk *event, Bin_Pack *bp); +non_null() bool tox_event_file_recv_control_pack(const Tox_Event_File_Recv_Control *event, Bin_Pack *bp); +non_null() bool tox_event_file_recv_pack(const Tox_Event_File_Recv *event, Bin_Pack *bp); +non_null() bool tox_event_friend_connection_status_pack(const Tox_Event_Friend_Connection_Status *event, Bin_Pack *bp); +non_null() bool tox_event_friend_lossless_packet_pack(const Tox_Event_Friend_Lossless_Packet *event, Bin_Pack *bp); +non_null() bool tox_event_friend_lossy_packet_pack(const Tox_Event_Friend_Lossy_Packet *event, Bin_Pack *bp); +non_null() bool tox_event_friend_message_pack(const Tox_Event_Friend_Message *event, Bin_Pack *bp); +non_null() bool tox_event_friend_name_pack(const Tox_Event_Friend_Name *event, Bin_Pack *bp); +non_null() bool tox_event_friend_read_receipt_pack(const Tox_Event_Friend_Read_Receipt *event, Bin_Pack *bp); +non_null() bool tox_event_friend_request_pack(const Tox_Event_Friend_Request *event, Bin_Pack *bp); +non_null() bool tox_event_friend_status_message_pack(const Tox_Event_Friend_Status_Message *event, Bin_Pack *bp); +non_null() bool tox_event_friend_status_pack(const Tox_Event_Friend_Status *event, Bin_Pack *bp); +non_null() bool tox_event_friend_typing_pack(const Tox_Event_Friend_Typing *event, Bin_Pack *bp); +non_null() bool tox_event_self_connection_status_pack(const Tox_Event_Self_Connection_Status *event, Bin_Pack *bp); +non_null() bool tox_event_group_peer_name_pack(const Tox_Event_Group_Peer_Name *event, Bin_Pack *bp); +non_null() bool tox_event_group_peer_status_pack(const Tox_Event_Group_Peer_Status *event, Bin_Pack *bp); +non_null() bool tox_event_group_topic_pack(const Tox_Event_Group_Topic *event, Bin_Pack *bp); +non_null() bool tox_event_group_privacy_state_pack(const Tox_Event_Group_Privacy_State *event, Bin_Pack *bp); +non_null() bool tox_event_group_voice_state_pack(const Tox_Event_Group_Voice_State *event, Bin_Pack *bp); +non_null() bool tox_event_group_topic_lock_pack(const Tox_Event_Group_Topic_Lock *event, Bin_Pack *bp); +non_null() bool tox_event_group_peer_limit_pack(const Tox_Event_Group_Peer_Limit *event, Bin_Pack *bp); +non_null() bool tox_event_group_password_pack(const Tox_Event_Group_Password *event, Bin_Pack *bp); +non_null() bool tox_event_group_message_pack(const Tox_Event_Group_Message *event, Bin_Pack *bp); +non_null() bool tox_event_group_private_message_pack(const Tox_Event_Group_Private_Message *event, Bin_Pack *bp); +non_null() bool tox_event_group_custom_packet_pack(const Tox_Event_Group_Custom_Packet *event, Bin_Pack *bp); +non_null() bool tox_event_group_custom_private_packet_pack(const Tox_Event_Group_Custom_Private_Packet *event, Bin_Pack *bp); +non_null() bool tox_event_group_invite_pack(const Tox_Event_Group_Invite *event, Bin_Pack *bp); +non_null() bool tox_event_group_peer_join_pack(const Tox_Event_Group_Peer_Join *event, Bin_Pack *bp); +non_null() bool tox_event_group_peer_exit_pack(const Tox_Event_Group_Peer_Exit *event, Bin_Pack *bp); +non_null() bool tox_event_group_self_join_pack(const Tox_Event_Group_Self_Join *event, Bin_Pack *bp); +non_null() bool tox_event_group_join_fail_pack(const Tox_Event_Group_Join_Fail *event, Bin_Pack *bp); +non_null() bool tox_event_group_moderation_pack(const Tox_Event_Group_Moderation *event, Bin_Pack *bp); + +/** + * Unpack from msgpack. + */ +non_null() bool tox_event_unpack_into(Tox_Event *event, Bin_Unpack *bu, const Memory *mem); + +non_null() bool tox_event_conference_connected_unpack(Tox_Event_Conference_Connected **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_conference_invite_unpack(Tox_Event_Conference_Invite **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_conference_message_unpack(Tox_Event_Conference_Message **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_conference_peer_list_changed_unpack(Tox_Event_Conference_Peer_List_Changed **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_conference_peer_name_unpack(Tox_Event_Conference_Peer_Name **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_conference_title_unpack(Tox_Event_Conference_Title **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_file_chunk_request_unpack(Tox_Event_File_Chunk_Request **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_file_recv_chunk_unpack(Tox_Event_File_Recv_Chunk **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_file_recv_control_unpack(Tox_Event_File_Recv_Control **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_file_recv_unpack(Tox_Event_File_Recv **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_connection_status_unpack(Tox_Event_Friend_Connection_Status **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_lossless_packet_unpack(Tox_Event_Friend_Lossless_Packet **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_lossy_packet_unpack(Tox_Event_Friend_Lossy_Packet **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_message_unpack(Tox_Event_Friend_Message **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_name_unpack(Tox_Event_Friend_Name **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_read_receipt_unpack(Tox_Event_Friend_Read_Receipt **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_request_unpack(Tox_Event_Friend_Request **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_status_message_unpack(Tox_Event_Friend_Status_Message **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_status_unpack(Tox_Event_Friend_Status **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_typing_unpack(Tox_Event_Friend_Typing **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_self_connection_status_unpack(Tox_Event_Self_Connection_Status **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_peer_name_unpack(Tox_Event_Group_Peer_Name **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_peer_status_unpack(Tox_Event_Group_Peer_Status **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_topic_unpack(Tox_Event_Group_Topic **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_privacy_state_unpack(Tox_Event_Group_Privacy_State **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_voice_state_unpack(Tox_Event_Group_Voice_State **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_topic_lock_unpack(Tox_Event_Group_Topic_Lock **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_peer_limit_unpack(Tox_Event_Group_Peer_Limit **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_password_unpack(Tox_Event_Group_Password **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_message_unpack(Tox_Event_Group_Message **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_private_message_unpack(Tox_Event_Group_Private_Message **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_custom_packet_unpack(Tox_Event_Group_Custom_Packet **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_custom_private_packet_unpack(Tox_Event_Group_Custom_Private_Packet **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_invite_unpack(Tox_Event_Group_Invite **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_peer_join_unpack(Tox_Event_Group_Peer_Join **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_peer_exit_unpack(Tox_Event_Group_Peer_Exit **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_self_join_unpack(Tox_Event_Group_Self_Join **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_join_fail_unpack(Tox_Event_Group_Join_Fail **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_moderation_unpack(Tox_Event_Group_Moderation **event, Bin_Unpack *bu, const Memory *mem); + +#ifdef __cplusplus +} +#endif + +#endif // C_TOXCORE_TOXCORE_TOX_EVENT_H diff --git a/toxcore/tox_events.c b/toxcore/tox_events.c index c5f2d6016f..fea4adc5f4 100644 --- a/toxcore/tox_events.c +++ b/toxcore/tox_events.c @@ -14,6 +14,8 @@ #include "logger.h" #include "mem.h" #include "tox.h" +#include "tox_event.h" +#include "tox_private.h" #include "tox_struct.h" @@ -47,11 +49,44 @@ void tox_events_init(Tox *tox) tox_callback_friend_status(tox, tox_events_handle_friend_status); tox_callback_friend_typing(tox, tox_events_handle_friend_typing); tox_callback_self_connection_status(tox, tox_events_handle_self_connection_status); + tox_callback_group_peer_name(tox, tox_events_handle_group_peer_name); + tox_callback_group_peer_status(tox, tox_events_handle_group_peer_status); + tox_callback_group_topic(tox, tox_events_handle_group_topic); + tox_callback_group_privacy_state(tox, tox_events_handle_group_privacy_state); + tox_callback_group_voice_state(tox, tox_events_handle_group_voice_state); + tox_callback_group_topic_lock(tox, tox_events_handle_group_topic_lock); + tox_callback_group_peer_limit(tox, tox_events_handle_group_peer_limit); + tox_callback_group_password(tox, tox_events_handle_group_password); + tox_callback_group_message(tox, tox_events_handle_group_message); + tox_callback_group_private_message(tox, tox_events_handle_group_private_message); + tox_callback_group_custom_packet(tox, tox_events_handle_group_custom_packet); + tox_callback_group_custom_private_packet(tox, tox_events_handle_group_custom_private_packet); + tox_callback_group_invite(tox, tox_events_handle_group_invite); + tox_callback_group_peer_join(tox, tox_events_handle_group_peer_join); + tox_callback_group_peer_exit(tox, tox_events_handle_group_peer_exit); + tox_callback_group_self_join(tox, tox_events_handle_group_self_join); + tox_callback_group_join_fail(tox, tox_events_handle_group_join_fail); + tox_callback_group_moderation(tox, tox_events_handle_group_moderation); +} + +uint32_t tox_events_get_size(const Tox_Events *events) +{ + return events == nullptr ? 0 : events->events_size; +} + +const Tox_Event *tox_events_get(const Tox_Events *events, uint32_t index) +{ + if (index >= tox_events_get_size(events)) { + return nullptr; + } + + return &events->events[index]; } Tox_Events *tox_events_iterate(Tox *tox, bool fail_hard, Tox_Err_Events_Iterate *error) { - Tox_Events_State state = {TOX_ERR_EVENTS_ITERATE_OK}; + const Tox_System *sys = tox_get_system(tox); + Tox_Events_State state = {TOX_ERR_EVENTS_ITERATE_OK, sys->mem}; tox_iterate(tox, &state); if (error != nullptr) { @@ -68,141 +103,21 @@ Tox_Events *tox_events_iterate(Tox *tox, bool fail_hard, Tox_Err_Events_Iterate bool tox_events_pack(const Tox_Events *events, Bin_Pack *bp) { - const uint32_t count = tox_events_get_conference_connected_size(events) - + tox_events_get_conference_invite_size(events) - + tox_events_get_conference_message_size(events) - + tox_events_get_conference_peer_list_changed_size(events) - + tox_events_get_conference_peer_name_size(events) - + tox_events_get_conference_title_size(events) - + tox_events_get_file_chunk_request_size(events) - + tox_events_get_file_recv_chunk_size(events) - + tox_events_get_file_recv_control_size(events) - + tox_events_get_file_recv_size(events) - + tox_events_get_friend_connection_status_size(events) - + tox_events_get_friend_lossless_packet_size(events) - + tox_events_get_friend_lossy_packet_size(events) - + tox_events_get_friend_message_size(events) - + tox_events_get_friend_name_size(events) - + tox_events_get_friend_read_receipt_size(events) - + tox_events_get_friend_request_size(events) - + tox_events_get_friend_status_message_size(events) - + tox_events_get_friend_status_size(events) - + tox_events_get_friend_typing_size(events) - + tox_events_get_self_connection_status_size(events); - - return bin_pack_array(bp, count) - && tox_events_pack_conference_connected(events, bp) - && tox_events_pack_conference_invite(events, bp) - && tox_events_pack_conference_message(events, bp) - && tox_events_pack_conference_peer_list_changed(events, bp) - && tox_events_pack_conference_peer_name(events, bp) - && tox_events_pack_conference_title(events, bp) - && tox_events_pack_file_chunk_request(events, bp) - && tox_events_pack_file_recv_chunk(events, bp) - && tox_events_pack_file_recv_control(events, bp) - && tox_events_pack_file_recv(events, bp) - && tox_events_pack_friend_connection_status(events, bp) - && tox_events_pack_friend_lossless_packet(events, bp) - && tox_events_pack_friend_lossy_packet(events, bp) - && tox_events_pack_friend_message(events, bp) - && tox_events_pack_friend_name(events, bp) - && tox_events_pack_friend_read_receipt(events, bp) - && tox_events_pack_friend_request(events, bp) - && tox_events_pack_friend_status_message(events, bp) - && tox_events_pack_friend_status(events, bp) - && tox_events_pack_friend_typing(events, bp) - && tox_events_pack_self_connection_status(events, bp); -} - -non_null() -static bool tox_events_unpack_event(Tox_Events *events, Bin_Unpack *bu) -{ - uint32_t size; - if (!bin_unpack_array(bu, &size)) { - return false; - } - - if (size != 2) { + const uint32_t size = tox_events_get_size(events); + if (!bin_pack_array(bp, size)) { return false; } - uint8_t type; - if (!bin_unpack_u08(bu, &type)) { - return false; - } - - switch (type) { - case TOX_EVENT_CONFERENCE_CONNECTED: - return tox_events_unpack_conference_connected(events, bu); - - case TOX_EVENT_CONFERENCE_INVITE: - return tox_events_unpack_conference_invite(events, bu); - - case TOX_EVENT_CONFERENCE_MESSAGE: - return tox_events_unpack_conference_message(events, bu); - - case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED: - return tox_events_unpack_conference_peer_list_changed(events, bu); - - case TOX_EVENT_CONFERENCE_PEER_NAME: - return tox_events_unpack_conference_peer_name(events, bu); - - case TOX_EVENT_CONFERENCE_TITLE: - return tox_events_unpack_conference_title(events, bu); - - case TOX_EVENT_FILE_CHUNK_REQUEST: - return tox_events_unpack_file_chunk_request(events, bu); - - case TOX_EVENT_FILE_RECV_CHUNK: - return tox_events_unpack_file_recv_chunk(events, bu); - - case TOX_EVENT_FILE_RECV_CONTROL: - return tox_events_unpack_file_recv_control(events, bu); - - case TOX_EVENT_FILE_RECV: - return tox_events_unpack_file_recv(events, bu); - - case TOX_EVENT_FRIEND_CONNECTION_STATUS: - return tox_events_unpack_friend_connection_status(events, bu); - - case TOX_EVENT_FRIEND_LOSSLESS_PACKET: - return tox_events_unpack_friend_lossless_packet(events, bu); - - case TOX_EVENT_FRIEND_LOSSY_PACKET: - return tox_events_unpack_friend_lossy_packet(events, bu); - - case TOX_EVENT_FRIEND_MESSAGE: - return tox_events_unpack_friend_message(events, bu); - - case TOX_EVENT_FRIEND_NAME: - return tox_events_unpack_friend_name(events, bu); - - case TOX_EVENT_FRIEND_READ_RECEIPT: - return tox_events_unpack_friend_read_receipt(events, bu); - - case TOX_EVENT_FRIEND_REQUEST: - return tox_events_unpack_friend_request(events, bu); - - case TOX_EVENT_FRIEND_STATUS_MESSAGE: - return tox_events_unpack_friend_status_message(events, bu); - - case TOX_EVENT_FRIEND_STATUS: - return tox_events_unpack_friend_status(events, bu); - - case TOX_EVENT_FRIEND_TYPING: - return tox_events_unpack_friend_typing(events, bu); - - case TOX_EVENT_SELF_CONNECTION_STATUS: - return tox_events_unpack_self_connection_status(events, bu); - - default: + for (uint32_t i = 0; i < size; ++i) { + if (!tox_event_pack(&events->events[i], bp)) { return false; + } } return true; } -bool tox_events_unpack(Tox_Events *events, Bin_Unpack *bu) +bool tox_events_unpack(Tox_Events *events, Bin_Unpack *bu, const Memory *mem) { uint32_t size; if (!bin_unpack_array(bu, &size)) { @@ -210,11 +125,20 @@ bool tox_events_unpack(Tox_Events *events, Bin_Unpack *bu) } for (uint32_t i = 0; i < size; ++i) { - if (!tox_events_unpack_event(events, bu)) { + Tox_Event event = {TOX_EVENT_INVALID}; + if (!tox_event_unpack_into(&event, bu, mem)) { + tox_event_destruct(&event, mem); + return false; + } + + if (!tox_events_add(events, &event)) { + tox_event_destruct(&event, mem); return false; } } + // Invariant: if all adds worked, the events size must be the input array size. + assert(tox_events_get_size(events) == size); return true; } @@ -230,9 +154,9 @@ uint32_t tox_events_bytes_size(const Tox_Events *events) return bin_pack_obj_size(tox_events_bin_pack_handler, nullptr, events); } -void tox_events_get_bytes(const Tox_Events *events, uint8_t *bytes) +bool tox_events_get_bytes(const Tox_Events *events, uint8_t *bytes) { - bin_pack_obj(tox_events_bin_pack_handler, nullptr, events, bytes, UINT32_MAX); + return bin_pack_obj(tox_events_bin_pack_handler, nullptr, events, bytes, UINT32_MAX); } Tox_Events *tox_events_load(const Tox_System *sys, const uint8_t *bytes, uint32_t bytes_size) @@ -253,8 +177,9 @@ Tox_Events *tox_events_load(const Tox_System *sys, const uint8_t *bytes, uint32_ *events = (Tox_Events) { nullptr }; + events->mem = sys->mem; - if (!tox_events_unpack(events, bu)) { + if (!tox_events_unpack(events, bu, sys->mem)) { tox_events_free(events); bin_unpack_free(bu); return nullptr; diff --git a/toxcore/tox_events.h b/toxcore/tox_events.h index e868d89098..05edd4f4bb 100644 --- a/toxcore/tox_events.h +++ b/toxcore/tox_events.h @@ -88,7 +88,7 @@ uint32_t tox_event_file_recv_get_kind( typedef struct Tox_Event_File_Recv_Chunk Tox_Event_File_Recv_Chunk; const uint8_t *tox_event_file_recv_chunk_get_data( const Tox_Event_File_Recv_Chunk *file_recv_chunk); -uint32_t tox_event_file_recv_chunk_get_length( +uint32_t tox_event_file_recv_chunk_get_data_length( const Tox_Event_File_Recv_Chunk *file_recv_chunk); uint32_t tox_event_file_recv_chunk_get_file_number( const Tox_Event_File_Recv_Chunk *file_recv_chunk); @@ -183,8 +183,167 @@ typedef struct Tox_Event_Self_Connection_Status Tox_Event_Self_Connection_Status Tox_Connection tox_event_self_connection_status_get_connection_status( const Tox_Event_Self_Connection_Status *self_connection_status); - -typedef enum Tox_Event { +typedef struct Tox_Event_Group_Peer_Name Tox_Event_Group_Peer_Name; +uint32_t tox_event_group_peer_name_get_group_number( + const Tox_Event_Group_Peer_Name *group_peer_name); +uint32_t tox_event_group_peer_name_get_peer_id( + const Tox_Event_Group_Peer_Name *group_peer_name); +const uint8_t *tox_event_group_peer_name_get_name( + const Tox_Event_Group_Peer_Name *group_peer_name); +uint32_t tox_event_group_peer_name_get_name_length( + const Tox_Event_Group_Peer_Name *group_peer_name); + +typedef struct Tox_Event_Group_Peer_Status Tox_Event_Group_Peer_Status; +uint32_t tox_event_group_peer_status_get_group_number( + const Tox_Event_Group_Peer_Status *group_peer_status); +uint32_t tox_event_group_peer_status_get_peer_id( + const Tox_Event_Group_Peer_Status *group_peer_status); +Tox_User_Status tox_event_group_peer_status_get_status( + const Tox_Event_Group_Peer_Status *group_peer_status); + +typedef struct Tox_Event_Group_Topic Tox_Event_Group_Topic; +uint32_t tox_event_group_topic_get_group_number( + const Tox_Event_Group_Topic *group_topic); +uint32_t tox_event_group_topic_get_peer_id( + const Tox_Event_Group_Topic *group_topic); +const uint8_t *tox_event_group_topic_get_topic( + const Tox_Event_Group_Topic *group_topic); +uint32_t tox_event_group_topic_get_topic_length( + const Tox_Event_Group_Topic *group_topic); + +typedef struct Tox_Event_Group_Privacy_State Tox_Event_Group_Privacy_State; +uint32_t tox_event_group_privacy_state_get_group_number( + const Tox_Event_Group_Privacy_State *group_privacy_state); +Tox_Group_Privacy_State tox_event_group_privacy_state_get_privacy_state( + const Tox_Event_Group_Privacy_State *group_privacy_state); + +typedef struct Tox_Event_Group_Voice_State Tox_Event_Group_Voice_State; +uint32_t tox_event_group_voice_state_get_group_number( + const Tox_Event_Group_Voice_State *group_voice_state); +Tox_Group_Voice_State tox_event_group_voice_state_get_voice_state( + const Tox_Event_Group_Voice_State *group_voice_state); + +typedef struct Tox_Event_Group_Topic_Lock Tox_Event_Group_Topic_Lock; +uint32_t tox_event_group_topic_lock_get_group_number( + const Tox_Event_Group_Topic_Lock *group_topic_lock); +Tox_Group_Topic_Lock tox_event_group_topic_lock_get_topic_lock( + const Tox_Event_Group_Topic_Lock *group_topic_lock); + +typedef struct Tox_Event_Group_Peer_Limit Tox_Event_Group_Peer_Limit; +uint32_t tox_event_group_peer_limit_get_group_number( + const Tox_Event_Group_Peer_Limit *group_peer_limit); +uint32_t tox_event_group_peer_limit_get_peer_limit( + const Tox_Event_Group_Peer_Limit *group_peer_limit); + +typedef struct Tox_Event_Group_Password Tox_Event_Group_Password; +uint32_t tox_event_group_password_get_group_number( + const Tox_Event_Group_Password *group_password); +const uint8_t *tox_event_group_password_get_password( + const Tox_Event_Group_Password *group_password); +uint32_t tox_event_group_password_get_password_length( + const Tox_Event_Group_Password *group_password); + +typedef struct Tox_Event_Group_Message Tox_Event_Group_Message; +uint32_t tox_event_group_message_get_group_number( + const Tox_Event_Group_Message *group_message); +uint32_t tox_event_group_message_get_peer_id( + const Tox_Event_Group_Message *group_message); +Tox_Message_Type tox_event_group_message_get_type( + const Tox_Event_Group_Message *group_message); +const uint8_t *tox_event_group_message_get_message( + const Tox_Event_Group_Message *group_message); +uint32_t tox_event_group_message_get_message_length( + const Tox_Event_Group_Message *group_message); +uint32_t tox_event_group_message_get_message_id( + const Tox_Event_Group_Message *group_message); + +typedef struct Tox_Event_Group_Private_Message Tox_Event_Group_Private_Message; +uint32_t tox_event_group_private_message_get_group_number( + const Tox_Event_Group_Private_Message *group_private_message); +uint32_t tox_event_group_private_message_get_peer_id( + const Tox_Event_Group_Private_Message *group_private_message); +Tox_Message_Type tox_event_group_private_message_get_type( + const Tox_Event_Group_Private_Message *group_private_message); +const uint8_t *tox_event_group_private_message_get_message( + const Tox_Event_Group_Private_Message *group_private_message); +uint32_t tox_event_group_private_message_get_message_length( + const Tox_Event_Group_Private_Message *group_private_message); + +typedef struct Tox_Event_Group_Custom_Packet Tox_Event_Group_Custom_Packet; +uint32_t tox_event_group_custom_packet_get_group_number( + const Tox_Event_Group_Custom_Packet *group_custom_packet); +uint32_t tox_event_group_custom_packet_get_peer_id( + const Tox_Event_Group_Custom_Packet *group_custom_packet); +const uint8_t *tox_event_group_custom_packet_get_data( + const Tox_Event_Group_Custom_Packet *group_custom_packet); +uint32_t tox_event_group_custom_packet_get_data_length( + const Tox_Event_Group_Custom_Packet *group_custom_packet); + +typedef struct Tox_Event_Group_Custom_Private_Packet Tox_Event_Group_Custom_Private_Packet; +uint32_t tox_event_group_custom_private_packet_get_group_number( + const Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet); +uint32_t tox_event_group_custom_private_packet_get_peer_id( + const Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet); +const uint8_t *tox_event_group_custom_private_packet_get_data( + const Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet); +uint32_t tox_event_group_custom_private_packet_get_data_length( + const Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet); + +typedef struct Tox_Event_Group_Invite Tox_Event_Group_Invite; +uint32_t tox_event_group_invite_get_friend_number( + const Tox_Event_Group_Invite *group_invite); +const uint8_t *tox_event_group_invite_get_invite_data( + const Tox_Event_Group_Invite *group_invite); +uint32_t tox_event_group_invite_get_invite_data_length( + const Tox_Event_Group_Invite *group_invite); +const uint8_t *tox_event_group_invite_get_group_name( + const Tox_Event_Group_Invite *group_invite); +uint32_t tox_event_group_invite_get_group_name_length( + const Tox_Event_Group_Invite *group_invite); + +typedef struct Tox_Event_Group_Peer_Join Tox_Event_Group_Peer_Join; +uint32_t tox_event_group_peer_join_get_group_number( + const Tox_Event_Group_Peer_Join *group_peer_join); +uint32_t tox_event_group_peer_join_get_peer_id( + const Tox_Event_Group_Peer_Join *group_peer_join); + +typedef struct Tox_Event_Group_Peer_Exit Tox_Event_Group_Peer_Exit; +uint32_t tox_event_group_peer_exit_get_group_number( + const Tox_Event_Group_Peer_Exit *group_peer_exit); +uint32_t tox_event_group_peer_exit_get_peer_id( + const Tox_Event_Group_Peer_Exit *group_peer_exit); +Tox_Group_Exit_Type tox_event_group_peer_exit_get_exit_type( + const Tox_Event_Group_Peer_Exit *group_peer_exit); +const uint8_t *tox_event_group_peer_exit_get_name( + const Tox_Event_Group_Peer_Exit *group_peer_exit); +uint32_t tox_event_group_peer_exit_get_name_length( + const Tox_Event_Group_Peer_Exit *group_peer_exit); +const uint8_t *tox_event_group_peer_exit_get_part_message( + const Tox_Event_Group_Peer_Exit *group_peer_exit); +uint32_t tox_event_group_peer_exit_get_part_message_length( + const Tox_Event_Group_Peer_Exit *group_peer_exit); + +typedef struct Tox_Event_Group_Self_Join Tox_Event_Group_Self_Join; +uint32_t tox_event_group_self_join_get_group_number( + const Tox_Event_Group_Self_Join *group_self_join); + +typedef struct Tox_Event_Group_Join_Fail Tox_Event_Group_Join_Fail; +uint32_t tox_event_group_join_fail_get_group_number( + const Tox_Event_Group_Join_Fail *group_join_fail); +Tox_Group_Join_Fail tox_event_group_join_fail_get_fail_type( + const Tox_Event_Group_Join_Fail *group_join_fail); + +typedef struct Tox_Event_Group_Moderation Tox_Event_Group_Moderation; +uint32_t tox_event_group_moderation_get_group_number( + const Tox_Event_Group_Moderation *group_moderation); +uint32_t tox_event_group_moderation_get_source_peer_id( + const Tox_Event_Group_Moderation *group_moderation); +uint32_t tox_event_group_moderation_get_target_peer_id( + const Tox_Event_Group_Moderation *group_moderation); +Tox_Group_Mod_Event tox_event_group_moderation_get_mod_type( + const Tox_Event_Group_Moderation *group_moderation); + +typedef enum Tox_Event_Type { TOX_EVENT_SELF_CONNECTION_STATUS = 0, TOX_EVENT_FRIEND_REQUEST = 1, @@ -212,7 +371,121 @@ typedef enum Tox_Event { TOX_EVENT_CONFERENCE_TITLE = 19, TOX_EVENT_CONFERENCE_MESSAGE = 20, -} Tox_Event; + + TOX_EVENT_GROUP_PEER_NAME = 21, + TOX_EVENT_GROUP_PEER_STATUS = 22, + TOX_EVENT_GROUP_TOPIC = 23, + TOX_EVENT_GROUP_PRIVACY_STATE = 24, + TOX_EVENT_GROUP_VOICE_STATE = 25, + TOX_EVENT_GROUP_TOPIC_LOCK = 26, + TOX_EVENT_GROUP_PEER_LIMIT = 27, + TOX_EVENT_GROUP_PASSWORD = 28, + TOX_EVENT_GROUP_MESSAGE = 29, + TOX_EVENT_GROUP_PRIVATE_MESSAGE = 30, + TOX_EVENT_GROUP_CUSTOM_PACKET = 31, + TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET = 32, + TOX_EVENT_GROUP_INVITE = 33, + TOX_EVENT_GROUP_PEER_JOIN = 34, + TOX_EVENT_GROUP_PEER_EXIT = 35, + TOX_EVENT_GROUP_SELF_JOIN = 36, + TOX_EVENT_GROUP_JOIN_FAIL = 37, + TOX_EVENT_GROUP_MODERATION = 38, + + TOX_EVENT_INVALID = 255, +} Tox_Event_Type; + +const char *tox_event_type_to_string(Tox_Event_Type type); + +/** + * A single Tox core event. + * + * It could contain any of the above event types. Use `tox_event_get_type` to + * find out which one it is, then use one of the `get` functions to get the + * actual event object out. The `get` functions will return NULL in case of type + * mismatch. + */ +typedef struct Tox_Event Tox_Event; + +Tox_Event_Type tox_event_get_type(const Tox_Event *event); + +const Tox_Event_Conference_Connected *tox_event_get_conference_connected( + const Tox_Event *event); +const Tox_Event_Conference_Invite *tox_event_get_conference_invite( + const Tox_Event *event); +const Tox_Event_Conference_Message *tox_event_get_conference_message( + const Tox_Event *event); +const Tox_Event_Conference_Peer_List_Changed *tox_event_get_conference_peer_list_changed( + const Tox_Event *event); +const Tox_Event_Conference_Peer_Name *tox_event_get_conference_peer_name( + const Tox_Event *event); +const Tox_Event_Conference_Title *tox_event_get_conference_title( + const Tox_Event *event); +const Tox_Event_File_Chunk_Request *tox_event_get_file_chunk_request( + const Tox_Event *event); +const Tox_Event_File_Recv_Chunk *tox_event_get_file_recv_chunk( + const Tox_Event *event); +const Tox_Event_File_Recv_Control *tox_event_get_file_recv_control( + const Tox_Event *event); +const Tox_Event_File_Recv *tox_event_get_file_recv( + const Tox_Event *event); +const Tox_Event_Friend_Connection_Status *tox_event_get_friend_connection_status( + const Tox_Event *event); +const Tox_Event_Friend_Lossless_Packet *tox_event_get_friend_lossless_packet( + const Tox_Event *event); +const Tox_Event_Friend_Lossy_Packet *tox_event_get_friend_lossy_packet( + const Tox_Event *event); +const Tox_Event_Friend_Message *tox_event_get_friend_message( + const Tox_Event *event); +const Tox_Event_Friend_Name *tox_event_get_friend_name( + const Tox_Event *event); +const Tox_Event_Friend_Read_Receipt *tox_event_get_friend_read_receipt( + const Tox_Event *event); +const Tox_Event_Friend_Request *tox_event_get_friend_request( + const Tox_Event *event); +const Tox_Event_Friend_Status_Message *tox_event_get_friend_status_message( + const Tox_Event *event); +const Tox_Event_Friend_Status *tox_event_get_friend_status( + const Tox_Event *event); +const Tox_Event_Friend_Typing *tox_event_get_friend_typing( + const Tox_Event *event); +const Tox_Event_Self_Connection_Status *tox_event_get_self_connection_status( + const Tox_Event *event); +const Tox_Event_Group_Peer_Name *tox_event_get_group_peer_name( + const Tox_Event *event); +const Tox_Event_Group_Peer_Status *tox_event_get_group_peer_status( + const Tox_Event *event); +const Tox_Event_Group_Topic *tox_event_get_group_topic( + const Tox_Event *event); +const Tox_Event_Group_Privacy_State *tox_event_get_group_privacy_state( + const Tox_Event *event); +const Tox_Event_Group_Voice_State *tox_event_get_group_voice_state( + const Tox_Event *event); +const Tox_Event_Group_Topic_Lock *tox_event_get_group_topic_lock( + const Tox_Event *event); +const Tox_Event_Group_Peer_Limit *tox_event_get_group_peer_limit( + const Tox_Event *event); +const Tox_Event_Group_Password *tox_event_get_group_password( + const Tox_Event *event); +const Tox_Event_Group_Message *tox_event_get_group_message( + const Tox_Event *event); +const Tox_Event_Group_Private_Message *tox_event_get_group_private_message( + const Tox_Event *event); +const Tox_Event_Group_Custom_Packet *tox_event_get_group_custom_packet( + const Tox_Event *event); +const Tox_Event_Group_Custom_Private_Packet *tox_event_get_group_custom_private_packet( + const Tox_Event *event); +const Tox_Event_Group_Invite *tox_event_get_group_invite( + const Tox_Event *event); +const Tox_Event_Group_Peer_Join *tox_event_get_group_peer_join( + const Tox_Event *event); +const Tox_Event_Group_Peer_Exit *tox_event_get_group_peer_exit( + const Tox_Event *event); +const Tox_Event_Group_Self_Join *tox_event_get_group_self_join( + const Tox_Event *event); +const Tox_Event_Group_Join_Fail *tox_event_get_group_join_fail( + const Tox_Event *event); +const Tox_Event_Group_Moderation *tox_event_get_group_moderation( + const Tox_Event *event); /** * Container object for all Tox core events. @@ -221,70 +494,8 @@ typedef enum Tox_Event { */ typedef struct Tox_Events Tox_Events; -uint32_t tox_events_get_conference_connected_size(const Tox_Events *events); -uint32_t tox_events_get_conference_invite_size(const Tox_Events *events); -uint32_t tox_events_get_conference_message_size(const Tox_Events *events); -uint32_t tox_events_get_conference_peer_list_changed_size(const Tox_Events *events); -uint32_t tox_events_get_conference_peer_name_size(const Tox_Events *events); -uint32_t tox_events_get_conference_title_size(const Tox_Events *events); -uint32_t tox_events_get_file_chunk_request_size(const Tox_Events *events); -uint32_t tox_events_get_file_recv_chunk_size(const Tox_Events *events); -uint32_t tox_events_get_file_recv_control_size(const Tox_Events *events); -uint32_t tox_events_get_file_recv_size(const Tox_Events *events); -uint32_t tox_events_get_friend_connection_status_size(const Tox_Events *events); -uint32_t tox_events_get_friend_lossless_packet_size(const Tox_Events *events); -uint32_t tox_events_get_friend_lossy_packet_size(const Tox_Events *events); -uint32_t tox_events_get_friend_message_size(const Tox_Events *events); -uint32_t tox_events_get_friend_name_size(const Tox_Events *events); -uint32_t tox_events_get_friend_read_receipt_size(const Tox_Events *events); -uint32_t tox_events_get_friend_request_size(const Tox_Events *events); -uint32_t tox_events_get_friend_status_message_size(const Tox_Events *events); -uint32_t tox_events_get_friend_status_size(const Tox_Events *events); -uint32_t tox_events_get_friend_typing_size(const Tox_Events *events); -uint32_t tox_events_get_self_connection_status_size(const Tox_Events *events); - -const Tox_Event_Conference_Connected *tox_events_get_conference_connected( - const Tox_Events *events, uint32_t index); -const Tox_Event_Conference_Invite *tox_events_get_conference_invite( - const Tox_Events *events, uint32_t index); -const Tox_Event_Conference_Message *tox_events_get_conference_message( - const Tox_Events *events, uint32_t index); -const Tox_Event_Conference_Peer_List_Changed *tox_events_get_conference_peer_list_changed( - const Tox_Events *events, uint32_t index); -const Tox_Event_Conference_Peer_Name *tox_events_get_conference_peer_name( - const Tox_Events *events, uint32_t index); -const Tox_Event_Conference_Title *tox_events_get_conference_title( - const Tox_Events *events, uint32_t index); -const Tox_Event_File_Chunk_Request *tox_events_get_file_chunk_request( - const Tox_Events *events, uint32_t index); -const Tox_Event_File_Recv_Chunk *tox_events_get_file_recv_chunk( - const Tox_Events *events, uint32_t index); -const Tox_Event_File_Recv_Control *tox_events_get_file_recv_control( - const Tox_Events *events, uint32_t index); -const Tox_Event_File_Recv *tox_events_get_file_recv( - const Tox_Events *events, uint32_t index); -const Tox_Event_Friend_Connection_Status *tox_events_get_friend_connection_status( - const Tox_Events *events, uint32_t index); -const Tox_Event_Friend_Lossless_Packet *tox_events_get_friend_lossless_packet( - const Tox_Events *events, uint32_t index); -const Tox_Event_Friend_Lossy_Packet *tox_events_get_friend_lossy_packet( - const Tox_Events *events, uint32_t index); -const Tox_Event_Friend_Message *tox_events_get_friend_message( - const Tox_Events *events, uint32_t index); -const Tox_Event_Friend_Name *tox_events_get_friend_name( - const Tox_Events *events, uint32_t index); -const Tox_Event_Friend_Read_Receipt *tox_events_get_friend_read_receipt( - const Tox_Events *events, uint32_t index); -const Tox_Event_Friend_Request *tox_events_get_friend_request( - const Tox_Events *events, uint32_t index); -const Tox_Event_Friend_Status_Message *tox_events_get_friend_status_message( - const Tox_Events *events, uint32_t index); -const Tox_Event_Friend_Status *tox_events_get_friend_status( - const Tox_Events *events, uint32_t index); -const Tox_Event_Friend_Typing *tox_events_get_friend_typing( - const Tox_Events *events, uint32_t index); -const Tox_Event_Self_Connection_Status *tox_events_get_self_connection_status( - const Tox_Events *events, uint32_t index); +uint32_t tox_events_get_size(const Tox_Events *events); +const Tox_Event *tox_events_get(const Tox_Events *events, uint32_t index); /** * Initialise the events recording system. @@ -341,7 +552,7 @@ Tox_Events *tox_events_iterate(Tox *tox, bool fail_hard, Tox_Err_Events_Iterate void tox_events_free(Tox_Events *events); uint32_t tox_events_bytes_size(const Tox_Events *events); -void tox_events_get_bytes(const Tox_Events *events, uint8_t *bytes); +bool tox_events_get_bytes(const Tox_Events *events, uint8_t *bytes); Tox_Events *tox_events_load(const Tox_System *sys, const uint8_t *bytes, uint32_t bytes_size); diff --git a/toxcore/tox_events_fuzz_test.cc b/toxcore/tox_events_fuzz_test.cc index 03dd76b7d3..480d80a4c5 100644 --- a/toxcore/tox_events_fuzz_test.cc +++ b/toxcore/tox_events_fuzz_test.cc @@ -1,10 +1,12 @@ #include "tox_events.h" +#include #include #include #include #include "../testing/fuzzing/fuzz_support.h" +#include "tox_dispatch.h" namespace { @@ -27,12 +29,59 @@ void TestUnpack(Fuzz_Data data) // rest of the fuzz data is input for malloc Fuzz_System sys{data}; + Tox_Dispatch *dispatch = tox_dispatch_new(nullptr); + assert(dispatch != nullptr); + + auto ignore = [](Tox *tox, auto *event, void *user_data) {}; + tox_events_callback_conference_connected(dispatch, ignore); + tox_events_callback_conference_invite(dispatch, ignore); + tox_events_callback_conference_message(dispatch, ignore); + tox_events_callback_conference_peer_list_changed(dispatch, ignore); + tox_events_callback_conference_peer_name(dispatch, ignore); + tox_events_callback_conference_title(dispatch, ignore); + tox_events_callback_file_chunk_request(dispatch, ignore); + tox_events_callback_file_recv(dispatch, ignore); + tox_events_callback_file_recv_chunk(dispatch, ignore); + tox_events_callback_file_recv_control(dispatch, ignore); + tox_events_callback_friend_connection_status(dispatch, ignore); + tox_events_callback_friend_lossless_packet(dispatch, ignore); + tox_events_callback_friend_lossy_packet(dispatch, ignore); + tox_events_callback_friend_message(dispatch, ignore); + tox_events_callback_friend_name(dispatch, ignore); + tox_events_callback_friend_read_receipt(dispatch, ignore); + tox_events_callback_friend_request(dispatch, ignore); + tox_events_callback_friend_status(dispatch, ignore); + tox_events_callback_friend_status_message(dispatch, ignore); + tox_events_callback_friend_typing(dispatch, ignore); + tox_events_callback_self_connection_status(dispatch, ignore); + tox_events_callback_group_peer_name(dispatch, ignore); + tox_events_callback_group_peer_status(dispatch, ignore); + tox_events_callback_group_topic(dispatch, ignore); + tox_events_callback_group_privacy_state(dispatch, ignore); + tox_events_callback_group_voice_state(dispatch, ignore); + tox_events_callback_group_topic_lock(dispatch, ignore); + tox_events_callback_group_peer_limit(dispatch, ignore); + tox_events_callback_group_password(dispatch, ignore); + tox_events_callback_group_message(dispatch, ignore); + tox_events_callback_group_private_message(dispatch, ignore); + tox_events_callback_group_custom_packet(dispatch, ignore); + tox_events_callback_group_custom_private_packet(dispatch, ignore); + tox_events_callback_group_invite(dispatch, ignore); + tox_events_callback_group_peer_join(dispatch, ignore); + tox_events_callback_group_peer_exit(dispatch, ignore); + tox_events_callback_group_self_join(dispatch, ignore); + tox_events_callback_group_join_fail(dispatch, ignore); + tox_events_callback_group_moderation(dispatch, ignore); + Tox_Events *events = tox_events_load(sys.sys.get(), events_data, events_size); if (events) { std::vector packed(tox_events_bytes_size(events)); tox_events_get_bytes(events, packed.data()); + + tox_dispatch_invoke(dispatch, events, nullptr, nullptr); } tox_events_free(events); + tox_dispatch_free(dispatch); } } // namespace diff --git a/toxcore/tox_events_test.cc b/toxcore/tox_events_test.cc index 5de29b5e48..749d6768f3 100644 --- a/toxcore/tox_events_test.cc +++ b/toxcore/tox_events_test.cc @@ -33,7 +33,7 @@ TEST(ToxEvents, UnpackEmptyArrayCreatesEmptyEvents) std::array data{0x90}; // empty msgpack array Tox_Events *events = tox_events_load(&sys, data.data(), data.size()); ASSERT_NE(events, nullptr); - EXPECT_EQ(tox_events_get_conference_connected_size(events), 0); + EXPECT_EQ(tox_events_get_size(events), 0); tox_events_free(events); }