From 8c35e0fefbe84858f4d1ec63ecb451d830dea025 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Tue, 28 Mar 2023 00:22:23 +0200 Subject: [PATCH] feat: add ngc events --- CMakeLists.txt | 18 + other/event_tooling/generate_event_c.cpp | 55 ++- toxcore/Makefile.inc | 18 + toxcore/events/conference_connected.c | 5 +- toxcore/events/conference_invite.c | 3 +- toxcore/events/conference_message.c | 3 +- toxcore/events/conference_peer_list_changed.c | 5 +- toxcore/events/conference_peer_name.c | 4 +- toxcore/events/conference_title.c | 4 +- toxcore/events/events_alloc.c | 3 +- toxcore/events/events_alloc.h | 18 + toxcore/events/file_chunk_request.c | 5 +- toxcore/events/file_recv.c | 4 +- toxcore/events/file_recv_chunk.c | 4 +- toxcore/events/file_recv_control.c | 4 +- toxcore/events/friend_connection_status.c | 4 +- toxcore/events/friend_lossless_packet.c | 4 +- toxcore/events/friend_lossy_packet.c | 4 +- toxcore/events/friend_message.c | 3 +- toxcore/events/friend_name.c | 4 +- toxcore/events/friend_read_receipt.c | 5 +- toxcore/events/friend_request.c | 2 +- toxcore/events/friend_status.c | 4 +- toxcore/events/friend_status_message.c | 4 +- toxcore/events/friend_typing.c | 5 +- toxcore/events/group_custom_packet.c | 271 +++++++++++ toxcore/events/group_custom_private_packet.c | 271 +++++++++++ toxcore/events/group_invite.c | 294 ++++++++++++ toxcore/events/group_join_fail.c | 231 +++++++++ toxcore/events/group_message.c | 306 ++++++++++++ toxcore/events/group_moderation.c | 265 +++++++++++ toxcore/events/group_password.c | 254 ++++++++++ toxcore/events/group_peer_exit.c | 329 +++++++++++++ toxcore/events/group_peer_join.c | 230 +++++++++ toxcore/events/group_peer_limit.c | 230 +++++++++ toxcore/events/group_peer_name.c | 271 +++++++++++ toxcore/events/group_peer_status.c | 248 ++++++++++ toxcore/events/group_privacy_state.c | 231 +++++++++ toxcore/events/group_private_message.c | 289 ++++++++++++ toxcore/events/group_self_join.c | 208 ++++++++ toxcore/events/group_topic.c | 271 +++++++++++ toxcore/events/group_topic_lock.c | 231 +++++++++ toxcore/events/group_voice_state.c | 231 +++++++++ toxcore/events/self_connection_status.c | 4 +- toxcore/tox_dispatch.c | 256 +++++++++- toxcore/tox_dispatch.h | 72 +++ toxcore/tox_event.c | 443 +++++++++++++++++- toxcore/tox_event.h | 90 ++++ toxcore/tox_events.c | 20 + toxcore/tox_events.h | 268 +++++++++++ toxcore/tox_events_test.cc | 6 +- 51 files changed, 5945 insertions(+), 67 deletions(-) create mode 100644 toxcore/events/group_custom_packet.c create mode 100644 toxcore/events/group_custom_private_packet.c create mode 100644 toxcore/events/group_invite.c create mode 100644 toxcore/events/group_join_fail.c create mode 100644 toxcore/events/group_message.c create mode 100644 toxcore/events/group_moderation.c create mode 100644 toxcore/events/group_password.c create mode 100644 toxcore/events/group_peer_exit.c create mode 100644 toxcore/events/group_peer_join.c create mode 100644 toxcore/events/group_peer_limit.c create mode 100644 toxcore/events/group_peer_name.c create mode 100644 toxcore/events/group_peer_status.c create mode 100644 toxcore/events/group_privacy_state.c create mode 100644 toxcore/events/group_private_message.c create mode 100644 toxcore/events/group_self_join.c create mode 100644 toxcore/events/group_topic.c create mode 100644 toxcore/events/group_topic_lock.c create mode 100644 toxcore/events/group_voice_state.c diff --git a/CMakeLists.txt b/CMakeLists.txt index dfc4937f..aaf6b375 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 diff --git a/other/event_tooling/generate_event_c.cpp b/other/event_tooling/generate_event_c.cpp index d440c301..e338cb60 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"; } @@ -626,7 +659,6 @@ int main(int argc, char** argv) { } }, -#if 0 // not yet :) { "Group_Peer_Name", { @@ -768,7 +800,6 @@ int main(int argc, char** argv) { EventTypeTrivial{"Tox_Group_Mod_Event", "mod_type"}, } }, -#endif }; if (argc < 2) { diff --git a/toxcore/Makefile.inc b/toxcore/Makefile.inc index f8e6a454..5783c91b 100644 --- a/toxcore/Makefile.inc +++ b/toxcore/Makefile.inc @@ -38,6 +38,24 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \ ../toxcore/events/friend_status_message.c \ ../toxcore/events/friend_typing.c \ ../toxcore/events/self_connection_status.c \ + ../toxcore/events/group_custom_packet.c \ + ../toxcore/events/group_custom_private_packet.c \ + ../toxcore/events/group_invite.c \ + ../toxcore/events/group_join_fail.c \ + ../toxcore/events/group_message.c \ + ../toxcore/events/group_moderation.c \ + ../toxcore/events/group_password.c \ + ../toxcore/events/group_peer_exit.c \ + ../toxcore/events/group_peer_join.c \ + ../toxcore/events/group_peer_limit.c \ + ../toxcore/events/group_peer_name.c \ + ../toxcore/events/group_peer_status.c \ + ../toxcore/events/group_privacy_state.c \ + ../toxcore/events/group_private_message.c \ + ../toxcore/events/group_self_join.c \ + ../toxcore/events/group_topic.c \ + ../toxcore/events/group_topic_lock.c \ + ../toxcore/events/group_voice_state.c \ ../toxcore/DHT.h \ ../toxcore/DHT.c \ ../toxcore/mem.h \ diff --git a/toxcore/events/conference_connected.c b/toxcore/events/conference_connected.c index a6c6ad22..8fd64541 100644 --- a/toxcore/events/conference_connected.c +++ b/toxcore/events/conference_connected.c @@ -1,18 +1,17 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 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" /***************************************************** diff --git a/toxcore/events/conference_invite.c b/toxcore/events/conference_invite.c index 75d39182..e02f2027 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 © 2023 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" diff --git a/toxcore/events/conference_message.c b/toxcore/events/conference_message.c index 1574b68d..02bec358 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 © 2023 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" diff --git a/toxcore/events/conference_peer_list_changed.c b/toxcore/events/conference_peer_list_changed.c index 3d7cc1c6..1bcb685d 100644 --- a/toxcore/events/conference_peer_list_changed.c +++ b/toxcore/events/conference_peer_list_changed.c @@ -1,18 +1,17 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 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" /***************************************************** diff --git a/toxcore/events/conference_peer_name.c b/toxcore/events/conference_peer_name.c index c47f258f..511962f8 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 © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,9 +11,9 @@ #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" /***************************************************** diff --git a/toxcore/events/conference_title.c b/toxcore/events/conference_title.c index 76bc9057..d401edf5 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 © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,9 +11,9 @@ #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" /***************************************************** diff --git a/toxcore/events/events_alloc.c b/toxcore/events/events_alloc.c index 88d4b1a7..271dfc00 100644 --- a/toxcore/events/events_alloc.c +++ b/toxcore/events/events_alloc.c @@ -5,9 +5,10 @@ #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) diff --git a/toxcore/events/events_alloc.h b/toxcore/events/events_alloc.h index 9bbddd4b..11ac6c99 100644 --- a/toxcore/events/events_alloc.h +++ b/toxcore/events/events_alloc.h @@ -49,6 +49,24 @@ tox_friend_status_cb tox_events_handle_friend_status; tox_friend_status_message_cb tox_events_handle_friend_status_message; tox_friend_typing_cb tox_events_handle_friend_typing; tox_self_connection_status_cb tox_events_handle_self_connection_status; +tox_group_peer_name_cb tox_events_handle_group_peer_name; +tox_group_peer_status_cb tox_events_handle_group_peer_status; +tox_group_topic_cb tox_events_handle_group_topic; +tox_group_privacy_state_cb tox_events_handle_group_privacy_state; +tox_group_voice_state_cb tox_events_handle_group_voice_state; +tox_group_topic_lock_cb tox_events_handle_group_topic_lock; +tox_group_peer_limit_cb tox_events_handle_group_peer_limit; +tox_group_password_cb tox_events_handle_group_password; +tox_group_message_cb tox_events_handle_group_message; +tox_group_private_message_cb tox_events_handle_group_private_message; +tox_group_custom_packet_cb tox_events_handle_group_custom_packet; +tox_group_custom_private_packet_cb tox_events_handle_group_custom_private_packet; +tox_group_invite_cb tox_events_handle_group_invite; +tox_group_peer_join_cb tox_events_handle_group_peer_join; +tox_group_peer_exit_cb tox_events_handle_group_peer_exit; +tox_group_self_join_cb tox_events_handle_group_self_join; +tox_group_join_fail_cb tox_events_handle_group_join_fail; +tox_group_moderation_cb tox_events_handle_group_moderation; non_null(2) nullable(1) bool tox_events_pack(const Tox_Events *events, Bin_Pack *bp); diff --git a/toxcore/events/file_chunk_request.c b/toxcore/events/file_chunk_request.c index 55bb6d0a..ae8269c6 100644 --- a/toxcore/events/file_chunk_request.c +++ b/toxcore/events/file_chunk_request.c @@ -1,18 +1,17 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 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" /***************************************************** diff --git a/toxcore/events/file_recv.c b/toxcore/events/file_recv.c index 21e076fd..49f04d80 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 © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,9 +11,9 @@ #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" /***************************************************** diff --git a/toxcore/events/file_recv_chunk.c b/toxcore/events/file_recv_chunk.c index 291f4bf7..8ffc7b5f 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 © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,9 +11,9 @@ #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" /***************************************************** diff --git a/toxcore/events/file_recv_control.c b/toxcore/events/file_recv_control.c index ee5bec1d..bfff9a05 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 © 2023 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" diff --git a/toxcore/events/friend_connection_status.c b/toxcore/events/friend_connection_status.c index 0701ccd0..a84ac25e 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 © 2023 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" diff --git a/toxcore/events/friend_lossless_packet.c b/toxcore/events/friend_lossless_packet.c index ebecf5ee..6f2f9e7d 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 © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,9 +11,9 @@ #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" /***************************************************** diff --git a/toxcore/events/friend_lossy_packet.c b/toxcore/events/friend_lossy_packet.c index 384ffb55..ec84ac90 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 © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,9 +11,9 @@ #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" /***************************************************** diff --git a/toxcore/events/friend_message.c b/toxcore/events/friend_message.c index 092d26b3..411cd858 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 © 2023 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" diff --git a/toxcore/events/friend_name.c b/toxcore/events/friend_name.c index 97791b75..f3d9a6bc 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 © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,9 +11,9 @@ #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" /***************************************************** diff --git a/toxcore/events/friend_read_receipt.c b/toxcore/events/friend_read_receipt.c index 7b65c885..bf53234f 100644 --- a/toxcore/events/friend_read_receipt.c +++ b/toxcore/events/friend_read_receipt.c @@ -1,18 +1,17 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 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" /***************************************************** diff --git a/toxcore/events/friend_request.c b/toxcore/events/friend_request.c index 5c7b6b96..1722c1d1 100644 --- a/toxcore/events/friend_request.c +++ b/toxcore/events/friend_request.c @@ -5,12 +5,12 @@ #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" diff --git a/toxcore/events/friend_status.c b/toxcore/events/friend_status.c index a5c5b039..794aa668 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 © 2023 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" diff --git a/toxcore/events/friend_status_message.c b/toxcore/events/friend_status_message.c index 322f2adb..5de6f400 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 © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,9 +11,9 @@ #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" /***************************************************** diff --git a/toxcore/events/friend_typing.c b/toxcore/events/friend_typing.c index 6d48391b..563cbbc8 100644 --- a/toxcore/events/friend_typing.c +++ b/toxcore/events/friend_typing.c @@ -1,18 +1,17 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 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" /***************************************************** diff --git a/toxcore/events/group_custom_packet.c b/toxcore/events/group_custom_packet.c new file mode 100644 index 00000000..8bf1927e --- /dev/null +++ b/toxcore/events/group_custom_packet.c @@ -0,0 +1,271 @@ +/* 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; +} + +const Tox_Event_Group_Custom_Packet *tox_events_get_group_custom_packet(const Tox_Events *events, uint32_t index) +{ + uint32_t group_custom_packet_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (group_custom_packet_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_CUSTOM_PACKET) { + const Tox_Event_Group_Custom_Packet *group_custom_packet = events->events[i].data.group_custom_packet; + if (group_custom_packet_index == index) { + return group_custom_packet; + } + ++group_custom_packet_index; + } + } + + return nullptr; +} + +uint32_t tox_events_get_group_custom_packet_size(const Tox_Events *events) +{ + uint32_t group_custom_packet_size = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_CUSTOM_PACKET) { + ++group_custom_packet_size; + } + } + + return group_custom_packet_size; +} + +bool tox_event_group_custom_packet_unpack( + Tox_Event_Group_Custom_Packet **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_custom_packet_new(mem); + + if (*event == nullptr) { + return false; + } + + return tox_event_group_custom_packet_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Custom_Packet *tox_event_group_custom_packet_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Group_Custom_Packet *group_custom_packet = tox_events_add_group_custom_packet(state->events, state->mem); + + if (group_custom_packet == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_custom_packet; +} + + +/***************************************************** + * + * :: event handler + * + *****************************************************/ + + +void tox_events_handle_group_custom_packet(Tox *tox, uint32_t group_number, uint32_t peer_id, const uint8_t *data, size_t length, + void *user_data) +{ + Tox_Event_Group_Custom_Packet *group_custom_packet = tox_event_group_custom_packet_alloc(user_data); + + if (group_custom_packet == nullptr) { + return; + } + + tox_event_group_custom_packet_set_group_number(group_custom_packet, group_number); + tox_event_group_custom_packet_set_peer_id(group_custom_packet, peer_id); + tox_event_group_custom_packet_set_data(group_custom_packet, data, length); +} diff --git a/toxcore/events/group_custom_private_packet.c b/toxcore/events/group_custom_private_packet.c new file mode 100644 index 00000000..8e69712d --- /dev/null +++ b/toxcore/events/group_custom_private_packet.c @@ -0,0 +1,271 @@ +/* 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; +} + +const Tox_Event_Group_Custom_Private_Packet *tox_events_get_group_custom_private_packet(const Tox_Events *events, uint32_t index) +{ + uint32_t group_custom_private_packet_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (group_custom_private_packet_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET) { + const Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet = events->events[i].data.group_custom_private_packet; + if (group_custom_private_packet_index == index) { + return group_custom_private_packet; + } + ++group_custom_private_packet_index; + } + } + + return nullptr; +} + +uint32_t tox_events_get_group_custom_private_packet_size(const Tox_Events *events) +{ + uint32_t group_custom_private_packet_size = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET) { + ++group_custom_private_packet_size; + } + } + + return group_custom_private_packet_size; +} + +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 00000000..f3eccb3f --- /dev/null +++ b/toxcore/events/group_invite.c @@ -0,0 +1,294 @@ +/* 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; +} + +const Tox_Event_Group_Invite *tox_events_get_group_invite(const Tox_Events *events, uint32_t index) +{ + uint32_t group_invite_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (group_invite_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_INVITE) { + const Tox_Event_Group_Invite *group_invite = events->events[i].data.group_invite; + if (group_invite_index == index) { + return group_invite; + } + ++group_invite_index; + } + } + + return nullptr; +} + +uint32_t tox_events_get_group_invite_size(const Tox_Events *events) +{ + uint32_t group_invite_size = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_INVITE) { + ++group_invite_size; + } + } + + return group_invite_size; +} + +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 00000000..0e526131 --- /dev/null +++ b/toxcore/events/group_join_fail.c @@ -0,0 +1,231 @@ +/* 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; +} + +const Tox_Event_Group_Join_Fail *tox_events_get_group_join_fail(const Tox_Events *events, uint32_t index) +{ + uint32_t group_join_fail_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (group_join_fail_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_JOIN_FAIL) { + const Tox_Event_Group_Join_Fail *group_join_fail = events->events[i].data.group_join_fail; + if (group_join_fail_index == index) { + return group_join_fail; + } + ++group_join_fail_index; + } + } + + return nullptr; +} + +uint32_t tox_events_get_group_join_fail_size(const Tox_Events *events) +{ + uint32_t group_join_fail_size = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_JOIN_FAIL) { + ++group_join_fail_size; + } + } + + return group_join_fail_size; +} + +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 00000000..17181621 --- /dev/null +++ b/toxcore/events/group_message.c @@ -0,0 +1,306 @@ +/* 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; +} + +const Tox_Event_Group_Message *tox_events_get_group_message(const Tox_Events *events, uint32_t index) +{ + uint32_t group_message_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (group_message_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_MESSAGE) { + const Tox_Event_Group_Message *group_message = events->events[i].data.group_message; + if (group_message_index == index) { + return group_message; + } + ++group_message_index; + } + } + + return nullptr; +} + +uint32_t tox_events_get_group_message_size(const Tox_Events *events) +{ + uint32_t group_message_size = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_MESSAGE) { + ++group_message_size; + } + } + + return group_message_size; +} + +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 00000000..74a5e9d0 --- /dev/null +++ b/toxcore/events/group_moderation.c @@ -0,0 +1,265 @@ +/* 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; +} + +const Tox_Event_Group_Moderation *tox_events_get_group_moderation(const Tox_Events *events, uint32_t index) +{ + uint32_t group_moderation_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (group_moderation_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_MODERATION) { + const Tox_Event_Group_Moderation *group_moderation = events->events[i].data.group_moderation; + if (group_moderation_index == index) { + return group_moderation; + } + ++group_moderation_index; + } + } + + return nullptr; +} + +uint32_t tox_events_get_group_moderation_size(const Tox_Events *events) +{ + uint32_t group_moderation_size = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_MODERATION) { + ++group_moderation_size; + } + } + + return group_moderation_size; +} + +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 00000000..514e725b --- /dev/null +++ b/toxcore/events/group_password.c @@ -0,0 +1,254 @@ +/* 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; +} + +const Tox_Event_Group_Password *tox_events_get_group_password(const Tox_Events *events, uint32_t index) +{ + uint32_t group_password_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (group_password_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_PASSWORD) { + const Tox_Event_Group_Password *group_password = events->events[i].data.group_password; + if (group_password_index == index) { + return group_password; + } + ++group_password_index; + } + } + + return nullptr; +} + +uint32_t tox_events_get_group_password_size(const Tox_Events *events) +{ + uint32_t group_password_size = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_PASSWORD) { + ++group_password_size; + } + } + + return group_password_size; +} + +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 00000000..dccd2c2a --- /dev/null +++ b/toxcore/events/group_peer_exit.c @@ -0,0 +1,329 @@ +/* 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; +} + +const Tox_Event_Group_Peer_Exit *tox_events_get_group_peer_exit(const Tox_Events *events, uint32_t index) +{ + uint32_t group_peer_exit_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (group_peer_exit_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_PEER_EXIT) { + const Tox_Event_Group_Peer_Exit *group_peer_exit = events->events[i].data.group_peer_exit; + if (group_peer_exit_index == index) { + return group_peer_exit; + } + ++group_peer_exit_index; + } + } + + return nullptr; +} + +uint32_t tox_events_get_group_peer_exit_size(const Tox_Events *events) +{ + uint32_t group_peer_exit_size = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_PEER_EXIT) { + ++group_peer_exit_size; + } + } + + return group_peer_exit_size; +} + +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 00000000..f19ab492 --- /dev/null +++ b/toxcore/events/group_peer_join.c @@ -0,0 +1,230 @@ +/* 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; +} + +const Tox_Event_Group_Peer_Join *tox_events_get_group_peer_join(const Tox_Events *events, uint32_t index) +{ + uint32_t group_peer_join_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (group_peer_join_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_PEER_JOIN) { + const Tox_Event_Group_Peer_Join *group_peer_join = events->events[i].data.group_peer_join; + if (group_peer_join_index == index) { + return group_peer_join; + } + ++group_peer_join_index; + } + } + + return nullptr; +} + +uint32_t tox_events_get_group_peer_join_size(const Tox_Events *events) +{ + uint32_t group_peer_join_size = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_PEER_JOIN) { + ++group_peer_join_size; + } + } + + return group_peer_join_size; +} + +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 00000000..47cdb5b5 --- /dev/null +++ b/toxcore/events/group_peer_limit.c @@ -0,0 +1,230 @@ +/* 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; +} + +const Tox_Event_Group_Peer_Limit *tox_events_get_group_peer_limit(const Tox_Events *events, uint32_t index) +{ + uint32_t group_peer_limit_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (group_peer_limit_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_PEER_LIMIT) { + const Tox_Event_Group_Peer_Limit *group_peer_limit = events->events[i].data.group_peer_limit; + if (group_peer_limit_index == index) { + return group_peer_limit; + } + ++group_peer_limit_index; + } + } + + return nullptr; +} + +uint32_t tox_events_get_group_peer_limit_size(const Tox_Events *events) +{ + uint32_t group_peer_limit_size = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_PEER_LIMIT) { + ++group_peer_limit_size; + } + } + + return group_peer_limit_size; +} + +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 00000000..e99e7989 --- /dev/null +++ b/toxcore/events/group_peer_name.c @@ -0,0 +1,271 @@ +/* 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; +} + +const Tox_Event_Group_Peer_Name *tox_events_get_group_peer_name(const Tox_Events *events, uint32_t index) +{ + uint32_t group_peer_name_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (group_peer_name_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_PEER_NAME) { + const Tox_Event_Group_Peer_Name *group_peer_name = events->events[i].data.group_peer_name; + if (group_peer_name_index == index) { + return group_peer_name; + } + ++group_peer_name_index; + } + } + + return nullptr; +} + +uint32_t tox_events_get_group_peer_name_size(const Tox_Events *events) +{ + uint32_t group_peer_name_size = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_PEER_NAME) { + ++group_peer_name_size; + } + } + + return group_peer_name_size; +} + +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 00000000..cbf5f2cb --- /dev/null +++ b/toxcore/events/group_peer_status.c @@ -0,0 +1,248 @@ +/* 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; +} + +const Tox_Event_Group_Peer_Status *tox_events_get_group_peer_status(const Tox_Events *events, uint32_t index) +{ + uint32_t group_peer_status_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (group_peer_status_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_PEER_STATUS) { + const Tox_Event_Group_Peer_Status *group_peer_status = events->events[i].data.group_peer_status; + if (group_peer_status_index == index) { + return group_peer_status; + } + ++group_peer_status_index; + } + } + + return nullptr; +} + +uint32_t tox_events_get_group_peer_status_size(const Tox_Events *events) +{ + uint32_t group_peer_status_size = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_PEER_STATUS) { + ++group_peer_status_size; + } + } + + return group_peer_status_size; +} + +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 00000000..330ed224 --- /dev/null +++ b/toxcore/events/group_privacy_state.c @@ -0,0 +1,231 @@ +/* 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; +} + +const Tox_Event_Group_Privacy_State *tox_events_get_group_privacy_state(const Tox_Events *events, uint32_t index) +{ + uint32_t group_privacy_state_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (group_privacy_state_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_PRIVACY_STATE) { + const Tox_Event_Group_Privacy_State *group_privacy_state = events->events[i].data.group_privacy_state; + if (group_privacy_state_index == index) { + return group_privacy_state; + } + ++group_privacy_state_index; + } + } + + return nullptr; +} + +uint32_t tox_events_get_group_privacy_state_size(const Tox_Events *events) +{ + uint32_t group_privacy_state_size = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_PRIVACY_STATE) { + ++group_privacy_state_size; + } + } + + return group_privacy_state_size; +} + +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 00000000..8e171c4d --- /dev/null +++ b/toxcore/events/group_private_message.c @@ -0,0 +1,289 @@ +/* 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; +} + +const Tox_Event_Group_Private_Message *tox_events_get_group_private_message(const Tox_Events *events, uint32_t index) +{ + uint32_t group_private_message_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (group_private_message_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_PRIVATE_MESSAGE) { + const Tox_Event_Group_Private_Message *group_private_message = events->events[i].data.group_private_message; + if (group_private_message_index == index) { + return group_private_message; + } + ++group_private_message_index; + } + } + + return nullptr; +} + +uint32_t tox_events_get_group_private_message_size(const Tox_Events *events) +{ + uint32_t group_private_message_size = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_PRIVATE_MESSAGE) { + ++group_private_message_size; + } + } + + return group_private_message_size; +} + +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 00000000..5d23cdea --- /dev/null +++ b/toxcore/events/group_self_join.c @@ -0,0 +1,208 @@ +/* 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; +} + +const Tox_Event_Group_Self_Join *tox_events_get_group_self_join(const Tox_Events *events, uint32_t index) +{ + uint32_t group_self_join_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (group_self_join_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_SELF_JOIN) { + const Tox_Event_Group_Self_Join *group_self_join = events->events[i].data.group_self_join; + if (group_self_join_index == index) { + return group_self_join; + } + ++group_self_join_index; + } + } + + return nullptr; +} + +uint32_t tox_events_get_group_self_join_size(const Tox_Events *events) +{ + uint32_t group_self_join_size = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_SELF_JOIN) { + ++group_self_join_size; + } + } + + return group_self_join_size; +} + +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 00000000..5a4fffea --- /dev/null +++ b/toxcore/events/group_topic.c @@ -0,0 +1,271 @@ +/* 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; +} + +const Tox_Event_Group_Topic *tox_events_get_group_topic(const Tox_Events *events, uint32_t index) +{ + uint32_t group_topic_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (group_topic_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_TOPIC) { + const Tox_Event_Group_Topic *group_topic = events->events[i].data.group_topic; + if (group_topic_index == index) { + return group_topic; + } + ++group_topic_index; + } + } + + return nullptr; +} + +uint32_t tox_events_get_group_topic_size(const Tox_Events *events) +{ + uint32_t group_topic_size = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_TOPIC) { + ++group_topic_size; + } + } + + return group_topic_size; +} + +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 00000000..1f301667 --- /dev/null +++ b/toxcore/events/group_topic_lock.c @@ -0,0 +1,231 @@ +/* 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; +} + +const Tox_Event_Group_Topic_Lock *tox_events_get_group_topic_lock(const Tox_Events *events, uint32_t index) +{ + uint32_t group_topic_lock_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (group_topic_lock_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_TOPIC_LOCK) { + const Tox_Event_Group_Topic_Lock *group_topic_lock = events->events[i].data.group_topic_lock; + if (group_topic_lock_index == index) { + return group_topic_lock; + } + ++group_topic_lock_index; + } + } + + return nullptr; +} + +uint32_t tox_events_get_group_topic_lock_size(const Tox_Events *events) +{ + uint32_t group_topic_lock_size = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_TOPIC_LOCK) { + ++group_topic_lock_size; + } + } + + return group_topic_lock_size; +} + +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 00000000..8ed19827 --- /dev/null +++ b/toxcore/events/group_voice_state.c @@ -0,0 +1,231 @@ +/* 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; +} + +const Tox_Event_Group_Voice_State *tox_events_get_group_voice_state(const Tox_Events *events, uint32_t index) +{ + uint32_t group_voice_state_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (group_voice_state_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_VOICE_STATE) { + const Tox_Event_Group_Voice_State *group_voice_state = events->events[i].data.group_voice_state; + if (group_voice_state_index == index) { + return group_voice_state; + } + ++group_voice_state_index; + } + } + + return nullptr; +} + +uint32_t tox_events_get_group_voice_state_size(const Tox_Events *events) +{ + uint32_t group_voice_state_size = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_VOICE_STATE) { + ++group_voice_state_size; + } + } + + return group_voice_state_size; +} + +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 c8fdaa56..60043a4b 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 © 2023 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" diff --git a/toxcore/tox_dispatch.c b/toxcore/tox_dispatch.c index 218e506d..63b4eb02 100644 --- a/toxcore/tox_dispatch.c +++ b/toxcore/tox_dispatch.c @@ -7,10 +7,10 @@ #include #include "ccompat.h" -#include "events/events_alloc.h" +#include "events/events_alloc.h" // IWYU pragma: keep +#include "tox.h" #include "tox_event.h" #include "tox_events.h" -#include "tox.h" struct Tox_Dispatch { tox_events_conference_connected_cb *conference_connected_callback; @@ -34,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) @@ -169,6 +187,96 @@ 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, 2, 3) nullable(4) static void tox_dispatch_invoke_event(const Tox_Dispatch *dispatch, const Tox_Event *event, Tox *tox, void *user_data) @@ -342,6 +450,150 @@ static void tox_dispatch_invoke_event(const Tox_Dispatch *dispatch, const Tox_Ev 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; } diff --git a/toxcore/tox_dispatch.h b/toxcore/tox_dispatch.h index 7e8fbba3..bf2df139 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 index feda8e02..d7ac2a90 100644 --- a/toxcore/tox_event.c +++ b/toxcore/tox_event.c @@ -5,14 +5,12 @@ #include "tox_event.h" #include -#include -#include -#include +#include "bin_pack.h" #include "bin_unpack.h" #include "ccompat.h" -#include "events/events_alloc.h" -#include "tox.h" +#include "mem.h" +#include "tox_events.h" const char *tox_event_type_to_string(Tox_Event_Type type) { @@ -80,6 +78,60 @@ const char *tox_event_type_to_string(Tox_Event_Type type) 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"; } @@ -204,8 +256,99 @@ bool tox_event_construct(Tox_Event *event, Tox_Event_Type type, const Memory *me break; } - case TOX_EVENT_INVALID: + 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; @@ -323,6 +466,96 @@ void tox_event_destruct(Tox_Event *event, const Memory *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; } @@ -399,6 +632,60 @@ bool tox_event_pack(const Tox_Event *event, Bin_Pack *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; } @@ -515,6 +802,96 @@ static bool tox_event_type_from_int(uint32_t value, Tox_Event_Type *out) 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; @@ -617,6 +994,60 @@ bool tox_event_unpack_into(Tox_Event *event, Bin_Unpack *bu, const Memory *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; } diff --git a/toxcore/tox_event.h b/toxcore/tox_event.h index fcf0fa3a..c28ba79b 100644 --- a/toxcore/tox_event.h +++ b/toxcore/tox_event.h @@ -42,6 +42,24 @@ typedef union Tox_Event_Data { 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 { @@ -75,6 +93,24 @@ non_null() Tox_Event_Friend_Status_Message *tox_event_friend_status_message_new( 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. @@ -102,6 +138,24 @@ non_null(2) nullable(1) void tox_event_friend_status_message_free(Tox_Event_Frie 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. @@ -129,6 +183,24 @@ non_null() bool tox_event_friend_status_message_pack(const Tox_Event_Friend_Stat 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. @@ -156,6 +228,24 @@ non_null() bool tox_event_friend_status_message_unpack(Tox_Event_Friend_Status_M 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 } diff --git a/toxcore/tox_events.c b/toxcore/tox_events.c index 9991d247..fea4adc5 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,6 +49,24 @@ 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) diff --git a/toxcore/tox_events.h b/toxcore/tox_events.h index c8713674..40108489 100644 --- a/toxcore/tox_events.h +++ b/toxcore/tox_events.h @@ -183,6 +183,165 @@ 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 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, @@ -213,6 +372,25 @@ typedef enum Tox_Event_Type { TOX_EVENT_CONFERENCE_MESSAGE = 20, + 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; @@ -272,6 +450,42 @@ 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. @@ -304,6 +518,24 @@ 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); +uint32_t tox_events_get_group_peer_name_size(const Tox_Events *events); +uint32_t tox_events_get_group_peer_status_size(const Tox_Events *events); +uint32_t tox_events_get_group_topic_size(const Tox_Events *events); +uint32_t tox_events_get_group_privacy_state_size(const Tox_Events *events); +uint32_t tox_events_get_group_voice_state_size(const Tox_Events *events); +uint32_t tox_events_get_group_topic_lock_size(const Tox_Events *events); +uint32_t tox_events_get_group_peer_limit_size(const Tox_Events *events); +uint32_t tox_events_get_group_password_size(const Tox_Events *events); +uint32_t tox_events_get_group_message_size(const Tox_Events *events); +uint32_t tox_events_get_group_private_message_size(const Tox_Events *events); +uint32_t tox_events_get_group_custom_packet_size(const Tox_Events *events); +uint32_t tox_events_get_group_custom_private_packet_size(const Tox_Events *events); +uint32_t tox_events_get_group_invite_size(const Tox_Events *events); +uint32_t tox_events_get_group_peer_join_size(const Tox_Events *events); +uint32_t tox_events_get_group_peer_exit_size(const Tox_Events *events); +uint32_t tox_events_get_group_self_join_size(const Tox_Events *events); +uint32_t tox_events_get_group_join_fail_size(const Tox_Events *events); +uint32_t tox_events_get_group_moderation_size(const Tox_Events *events); const Tox_Event_Conference_Connected *tox_events_get_conference_connected( const Tox_Events *events, uint32_t index); @@ -347,6 +579,42 @@ 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); +const Tox_Event_Group_Peer_Name *tox_events_get_group_peer_name( + const Tox_Events *events, uint32_t index); +const Tox_Event_Group_Peer_Status *tox_events_get_group_peer_status( + const Tox_Events *events, uint32_t index); +const Tox_Event_Group_Topic *tox_events_get_group_topic( + const Tox_Events *events, uint32_t index); +const Tox_Event_Group_Privacy_State *tox_events_get_group_privacy_state( + const Tox_Events *events, uint32_t index); +const Tox_Event_Group_Voice_State *tox_events_get_group_voice_state( + const Tox_Events *events, uint32_t index); +const Tox_Event_Group_Topic_Lock *tox_events_get_group_topic_lock( + const Tox_Events *events, uint32_t index); +const Tox_Event_Group_Peer_Limit *tox_events_get_group_peer_limit( + const Tox_Events *events, uint32_t index); +const Tox_Event_Group_Password *tox_events_get_group_password( + const Tox_Events *events, uint32_t index); +const Tox_Event_Group_Message *tox_events_get_group_message( + const Tox_Events *events, uint32_t index); +const Tox_Event_Group_Private_Message *tox_events_get_group_private_message( + const Tox_Events *events, uint32_t index); +const Tox_Event_Group_Custom_Packet *tox_events_get_group_custom_packet( + const Tox_Events *events, uint32_t index); +const Tox_Event_Group_Custom_Private_Packet *tox_events_get_group_custom_private_packet( + const Tox_Events *events, uint32_t index); +const Tox_Event_Group_Invite *tox_events_get_group_invite( + const Tox_Events *events, uint32_t index); +const Tox_Event_Group_Peer_Join *tox_events_get_group_peer_join( + const Tox_Events *events, uint32_t index); +const Tox_Event_Group_Peer_Exit *tox_events_get_group_peer_exit( + const Tox_Events *events, uint32_t index); +const Tox_Event_Group_Self_Join *tox_events_get_group_self_join( + const Tox_Events *events, uint32_t index); +const Tox_Event_Group_Join_Fail *tox_events_get_group_join_fail( + const Tox_Events *events, uint32_t index); +const Tox_Event_Group_Moderation *tox_events_get_group_moderation( + const Tox_Events *events, uint32_t index); /** * Initialise the events recording system. diff --git a/toxcore/tox_events_test.cc b/toxcore/tox_events_test.cc index fa76d393..5de29b5e 100644 --- a/toxcore/tox_events_test.cc +++ b/toxcore/tox_events_test.cc @@ -48,14 +48,14 @@ TEST(ToxEvents, NullEventsPacksToEmptyArray) TEST(ToxEvents, PackedEventsCanBeUnpacked) { const Tox_System sys = tox_default_system(); - // [[1, 1]] == Tox_Self_Connection_Status { .connection_status = TOX_CONNECTION_TCP } - std::array packed{0x91, 0x92, 0xcc, 0x01, 0xcc, 0x01}; + // [[0, 1]] == Tox_Self_Connection_Status { .connection_status = TOX_CONNECTION_TCP } + std::array packed{0x91, 0x92, 0xcc, 0x00, 0xcc, 0x01}; Tox_Events *events = tox_events_load(&sys, packed.data(), packed.size()); ASSERT_NE(events, nullptr); std::array bytes; ASSERT_EQ(tox_events_bytes_size(events), bytes.size()); tox_events_get_bytes(events, bytes.data()); - EXPECT_EQ(bytes, (std::array{0x91, 0x92, 0x01, 0x01})); + EXPECT_EQ(bytes, (std::array{0x91, 0x92, 0x00, 0x01})); tox_events_free(events); }