tomato/toxcore/tox_events.c
Green Sky 3105cc20ef Squashed 'external/toxcore/c-toxcore/' changes from d9b8fa6098d..81b1e4f6348
81b1e4f6348 chore: Release v0.2.21-rc.1
9303e2e49a1 chore: Update the pkgsrc versions in the update-versions tool
71ec4b3b1e9 chore: Update the version-sync script to work in a post-tox.api.h world
66da842f753 chore: Add version update script compatible with ci-tools.
199878f7660 chore: Use new bazel script for circle ci.
8278e9cda46 chore: Add release issue template and workflow.
a9bb3a1c4d1 chore: Fix alpine-s390x build.
6e0a641272e chore: Add a source tarball deploy workflow.
4adebe4d8b1 chore: Don't upload ios/macos variants in deploy workflows.
18f1d858ccb chore: Move one of the 3 freebsd builds to post-submit.
432ab60c002 feat: Add a Makefile for the single file deploy build.
a86c0011fd5 chore: Add deploy job for single C file library.
2e7495e8f2a docs: Update changelog format to use the new clog-compatible way.
a682da99e84 chore: Export wasmExports from the wasm binary.
12f34cdff27 chore: Add wasm to the nightly binary deploys.
1451029613f chore: Add strict-abi support for macOS/iOS.
c53c30e09d9 chore: Add time option to manual fuzz trigger.
2ccecdc2a1a chore: Add remaining fuzz tests to cflite.
4626c2e230e test: Add a Net_Crypto fuzz test.
b4a0e617c48 refactor: Use IP string length from ip_ntoa instead of strlen.
b85b91f22f6 cleanup: rename getnodes/sendnodes to nodes request/response This change alignes the naming to be closer to the spec and make it less ambiguous. This change also changes the naming of some private/experimental marked APIs. - tox_callback_dht_nodes_response() - tox_dht_nodes_request() - Tox_Event_Dht_Get_Nodes_Response
f1991aaa029 perf: Use stack allocation for strerror rendering.
3984211ccbf cleanup: remove kicked peers from saved peers list
26a991ed2be fix: ip to string function not accepting tcp families
712861f2e6d cleanup: Make websockify output qtox-compatible logging.
01932ea2f73 chore: Add opus and vpx to the toxcore wasm build.
d29c42ef631 refactor: don't fully discard received DHT nodes. This is mostly forward thinking, where we might introduce other ip families, in addition to ipv4, ipv6, tcp_ipv4 etc.
21e2325934f chore: Fix xcframework tarball creation.
b10c8b766ba chore: Fix xcframework checksum creation.
93787a9322e chore: Add ios/macos framework build.
9f723f891d3 fix: run do_gca also in bootstrap nodes
496cc703556 chore: Support arm64 iphone simulator.
aa0e2a8e928 chore: Add support for more iOS architectures.
13ad8e81cbf chore: Add binary deploy workflows.
c8344726378 refactor: Move tox_log_level out into its own file.
8799bea76c3 cleanup: Mark events/dispatch headers as experimental.
d4164edb548 refactor: Remove tox_types.h; use `struct` tags instead.
d408c982090 refactor: Move `Tox_Options` to `tox_options.h`.
5ab42d41209 chore: Move most cirrus jobs to circleci.
463eeae1144 cleanup: Avoid clashing with global define `DEBUG`.
92cc1e91747 refactor: Make Tox_Options own the passed proxy host and savedata.
f276b397226 test: Add some more asserts for I/O and alloc to succeed.
edb4dfc4869 fix: Don't crash on malloc failures in bin_unpack.
be457d5d0b2 cleanup: Use tox memory for bin_unpack and net_strerror.

git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: 81b1e4f6348124784088591c4fe9ab41e273031d
2025-03-12 19:16:50 +01:00

215 lines
7.4 KiB
C

/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2022-2025 The TokTok team.
*/
#include "tox_events.h"
#include <assert.h>
#include <string.h>
#include "attributes.h"
#include "bin_pack.h"
#include "bin_unpack.h"
#include "ccompat.h"
#include "events/events_alloc.h"
#include "logger.h"
#include "mem.h"
#include "tox.h"
#include "tox_event.h"
#include "tox_private.h"
#include "tox_struct.h" // IWYU pragma: keep
/*****************************************************
*
* :: Set up event handlers.
*
*****************************************************/
void tox_events_init(Tox *tox)
{
tox_callback_conference_connected(tox, tox_events_handle_conference_connected);
tox_callback_conference_invite(tox, tox_events_handle_conference_invite);
tox_callback_conference_message(tox, tox_events_handle_conference_message);
tox_callback_conference_peer_list_changed(tox, tox_events_handle_conference_peer_list_changed);
tox_callback_conference_peer_name(tox, tox_events_handle_conference_peer_name);
tox_callback_conference_title(tox, tox_events_handle_conference_title);
tox_callback_file_chunk_request(tox, tox_events_handle_file_chunk_request);
tox_callback_file_recv_chunk(tox, tox_events_handle_file_recv_chunk);
tox_callback_file_recv_control(tox, tox_events_handle_file_recv_control);
tox_callback_file_recv(tox, tox_events_handle_file_recv);
tox_callback_friend_connection_status(tox, tox_events_handle_friend_connection_status);
tox_callback_friend_lossless_packet(tox, tox_events_handle_friend_lossless_packet);
tox_callback_friend_lossy_packet(tox, tox_events_handle_friend_lossy_packet);
tox_callback_friend_message(tox, tox_events_handle_friend_message);
tox_callback_friend_name(tox, tox_events_handle_friend_name);
tox_callback_friend_read_receipt(tox, tox_events_handle_friend_read_receipt);
tox_callback_friend_request(tox, tox_events_handle_friend_request);
tox_callback_friend_status_message(tox, tox_events_handle_friend_status_message);
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);
tox_callback_dht_nodes_response(tox, tox_events_handle_dht_nodes_response);
}
uint32_t tox_events_get_size(const Tox_Events *events)
{
return events == nullptr ? 0 : events->events_size;
}
nullable(1)
static const Tox_Event *tox_events_get_events(const Tox_Events *events)
{
return events == nullptr ? nullptr : events->events;
}
const Tox_Event *tox_events_get(const Tox_Events *events, uint32_t index)
{
if (index >= tox_events_get_size(events)) {
return nullptr;
}
return &events->events[index];
}
Tox_Events *tox_events_iterate(Tox *tox, bool fail_hard, Tox_Err_Events_Iterate *error)
{
const Tox_System *sys = tox_get_system(tox);
Tox_Events_State state = {TOX_ERR_EVENTS_ITERATE_OK, sys->mem};
tox_iterate(tox, &state);
if (error != nullptr) {
*error = state.error;
}
if (fail_hard && state.error != TOX_ERR_EVENTS_ITERATE_OK) {
tox_events_free(state.events);
return nullptr;
}
return state.events;
}
non_null()
static bool tox_event_pack_handler(const void *arr, uint32_t index, const Logger *logger, Bin_Pack *bp)
{
const Tox_Event *events = (const Tox_Event *)arr;
assert(events != nullptr);
return tox_event_pack(&events[index], bp);
}
non_null(3) nullable(1, 2)
static bool tox_events_pack_handler(const void *obj, const Logger *logger, Bin_Pack *bp)
{
const Tox_Events *events = (const Tox_Events *)obj;
return bin_pack_obj_array(bp, tox_event_pack_handler, tox_events_get_events(events), tox_events_get_size(events), logger);
}
uint32_t tox_events_bytes_size(const Tox_Events *events)
{
return bin_pack_obj_size(tox_events_pack_handler, events, nullptr);
}
bool tox_events_get_bytes(const Tox_Events *events, uint8_t *bytes)
{
return bin_pack_obj(tox_events_pack_handler, events, nullptr, bytes, UINT32_MAX);
}
non_null()
static bool tox_events_unpack_handler(void *obj, Bin_Unpack *bu)
{
Tox_Events *events = (Tox_Events *)obj;
uint32_t size;
if (!bin_unpack_array(bu, &size)) {
return false;
}
for (uint32_t i = 0; i < size; ++i) {
Tox_Event event = {TOX_EVENT_INVALID};
if (!tox_event_unpack_into(&event, bu, events->mem)) {
tox_event_destruct(&event, events->mem);
return false;
}
if (!tox_events_add(events, &event)) {
tox_event_destruct(&event, events->mem);
return false;
}
}
// Invariant: if all adds worked, the events size must be the input array size.
assert(tox_events_get_size(events) == size);
return true;
}
Tox_Events *tox_events_load(const Tox_System *sys, const uint8_t *bytes, uint32_t bytes_size)
{
Tox_Events *events = (Tox_Events *)mem_alloc(sys->mem, sizeof(Tox_Events));
if (events == nullptr) {
return nullptr;
}
*events = (Tox_Events) {
nullptr
};
events->mem = sys->mem;
if (!bin_unpack_obj(sys->mem, tox_events_unpack_handler, events, bytes, bytes_size)) {
tox_events_free(events);
return nullptr;
}
return events;
}
bool tox_events_equal(const Tox_System *sys, const Tox_Events *a, const Tox_Events *b)
{
assert(sys != nullptr);
assert(sys->mem != nullptr);
const uint32_t a_size = tox_events_bytes_size(a);
const uint32_t b_size = tox_events_bytes_size(b);
if (a_size != b_size) {
return false;
}
uint8_t *a_bytes = (uint8_t *)mem_balloc(sys->mem, a_size);
uint8_t *b_bytes = (uint8_t *)mem_balloc(sys->mem, b_size);
if (a_bytes == nullptr || b_bytes == nullptr) {
mem_delete(sys->mem, b_bytes);
mem_delete(sys->mem, a_bytes);
return false;
}
tox_events_get_bytes(a, a_bytes);
tox_events_get_bytes(b, b_bytes);
const bool ret = memcmp(a_bytes, b_bytes, a_size) == 0;
mem_delete(sys->mem, b_bytes);
mem_delete(sys->mem, a_bytes);
return ret;
}