/work/toxcore/events/friend_request.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* SPDX-License-Identifier: GPL-3.0-or-later |
2 | | * Copyright © 2023-2026 The TokTok team. |
3 | | */ |
4 | | |
5 | | #include "events_alloc.h" |
6 | | |
7 | | #include <assert.h> |
8 | | #include <string.h> |
9 | | |
10 | | #include "../attributes.h" |
11 | | #include "../bin_pack.h" |
12 | | #include "../bin_unpack.h" |
13 | | #include "../ccompat.h" |
14 | | #include "../mem.h" |
15 | | #include "../tox.h" |
16 | | #include "../tox_event.h" |
17 | | #include "../tox_events.h" |
18 | | #include "../tox_struct.h" |
19 | | |
20 | | /***************************************************** |
21 | | * |
22 | | * :: struct and accessors |
23 | | * |
24 | | *****************************************************/ |
25 | | |
26 | | struct Tox_Event_Friend_Request { |
27 | | uint8_t public_key[TOX_PUBLIC_KEY_SIZE]; |
28 | | uint8_t *_Nullable message; |
29 | | uint32_t message_length; |
30 | | }; |
31 | | |
32 | | static bool tox_event_friend_request_set_public_key(Tox_Event_Friend_Request *_Nonnull friend_request, const uint8_t public_key[TOX_PUBLIC_KEY_SIZE]) |
33 | 137 | { |
34 | 137 | assert(friend_request != nullptr); |
35 | 137 | memcpy(friend_request->public_key, public_key, TOX_PUBLIC_KEY_SIZE); |
36 | 137 | return true; |
37 | 137 | } |
38 | | const uint8_t *tox_event_friend_request_get_public_key(const Tox_Event_Friend_Request *friend_request) |
39 | 136 | { |
40 | 136 | assert(friend_request != nullptr); |
41 | 136 | return friend_request->public_key; |
42 | 136 | } |
43 | | |
44 | | static bool tox_event_friend_request_set_message(Tox_Event_Friend_Request *_Nonnull friend_request, |
45 | | const Memory *_Nonnull mem, const uint8_t *_Nullable message, uint32_t message_length) |
46 | 137 | { |
47 | 137 | assert(friend_request != nullptr); |
48 | 137 | if (friend_request->message != nullptr) { |
49 | 0 | mem_delete(mem, friend_request->message); |
50 | 0 | friend_request->message = nullptr; |
51 | 0 | friend_request->message_length = 0; |
52 | 0 | } |
53 | | |
54 | 137 | if (message == nullptr) { |
55 | 0 | assert(message_length == 0); |
56 | 0 | return true; |
57 | 0 | } |
58 | | |
59 | 137 | if (message_length == 0) { |
60 | 0 | friend_request->message = nullptr; |
61 | 0 | friend_request->message_length = 0; |
62 | 0 | return true; |
63 | 0 | } |
64 | | |
65 | 137 | uint8_t *message_copy = (uint8_t *)mem_balloc(mem, message_length); |
66 | | |
67 | 137 | if (message_copy == nullptr) { |
68 | 0 | return false; |
69 | 0 | } |
70 | | |
71 | 137 | memcpy(message_copy, message, message_length); |
72 | 137 | friend_request->message = message_copy; |
73 | 137 | friend_request->message_length = message_length; |
74 | 137 | return true; |
75 | 137 | } |
76 | | uint32_t tox_event_friend_request_get_message_length(const Tox_Event_Friend_Request *friend_request) |
77 | 136 | { |
78 | 136 | assert(friend_request != nullptr); |
79 | 136 | return friend_request->message_length; |
80 | 136 | } |
81 | | const uint8_t *tox_event_friend_request_get_message(const Tox_Event_Friend_Request *friend_request) |
82 | 136 | { |
83 | 136 | assert(friend_request != nullptr); |
84 | 136 | return friend_request->message; |
85 | 136 | } |
86 | | |
87 | | static void tox_event_friend_request_construct(Tox_Event_Friend_Request *_Nonnull friend_request) |
88 | 162 | { |
89 | 162 | *friend_request = (Tox_Event_Friend_Request) { |
90 | 162 | { |
91 | 162 | 0 |
92 | 162 | } |
93 | 162 | }; |
94 | 162 | } |
95 | | static void tox_event_friend_request_destruct(Tox_Event_Friend_Request *_Nonnull friend_request, const Memory *_Nonnull mem) |
96 | 162 | { |
97 | 162 | mem_delete(mem, friend_request->message); |
98 | 162 | } |
99 | | |
100 | | bool tox_event_friend_request_pack( |
101 | | const Tox_Event_Friend_Request *event, Bin_Pack *bp) |
102 | 20 | { |
103 | 20 | return bin_pack_array(bp, 2) |
104 | 20 | && bin_pack_bin(bp, event->public_key, TOX_PUBLIC_KEY_SIZE) |
105 | 20 | && bin_pack_bin(bp, event->message, event->message_length); |
106 | 20 | } |
107 | | |
108 | | static bool tox_event_friend_request_unpack_into(Tox_Event_Friend_Request *_Nonnull event, Bin_Unpack *_Nonnull bu) |
109 | 25 | { |
110 | 25 | assert(event != nullptr); |
111 | 25 | if (!bin_unpack_array_fixed(bu, 2, nullptr)) { |
112 | 1 | return false; |
113 | 1 | } |
114 | | |
115 | 24 | return bin_unpack_bin_fixed(bu, event->public_key, TOX_PUBLIC_KEY_SIZE) |
116 | 24 | && bin_unpack_bin(bu, &event->message, &event->message_length); |
117 | 25 | } |
118 | | |
119 | | /***************************************************** |
120 | | * |
121 | | * :: new/free/add/get/size/unpack |
122 | | * |
123 | | *****************************************************/ |
124 | | |
125 | | const Tox_Event_Friend_Request *tox_event_get_friend_request(const Tox_Event *event) |
126 | 0 | { |
127 | 0 | return event->type == TOX_EVENT_FRIEND_REQUEST ? event->data.friend_request : nullptr; |
128 | 0 | } |
129 | | |
130 | | Tox_Event_Friend_Request *tox_event_friend_request_new(const Memory *mem) |
131 | 163 | { |
132 | 163 | Tox_Event_Friend_Request *const friend_request = |
133 | 163 | (Tox_Event_Friend_Request *)mem_alloc(mem, sizeof(Tox_Event_Friend_Request)); |
134 | | |
135 | 163 | if (friend_request == nullptr) { |
136 | 1 | return nullptr; |
137 | 1 | } |
138 | | |
139 | 162 | tox_event_friend_request_construct(friend_request); |
140 | 162 | return friend_request; |
141 | 163 | } |
142 | | |
143 | | void tox_event_friend_request_free(Tox_Event_Friend_Request *friend_request, const Memory *mem) |
144 | 163 | { |
145 | 163 | if (friend_request != nullptr) { |
146 | 162 | tox_event_friend_request_destruct(friend_request, mem); |
147 | 162 | } |
148 | 163 | mem_delete(mem, friend_request); |
149 | 163 | } |
150 | | |
151 | | static Tox_Event_Friend_Request *_Nullable tox_events_add_friend_request(Tox_Events *_Nonnull events, const Memory *_Nonnull mem) |
152 | 137 | { |
153 | 137 | Tox_Event_Friend_Request *const friend_request = tox_event_friend_request_new(mem); |
154 | | |
155 | 137 | if (friend_request == nullptr) { |
156 | 0 | return nullptr; |
157 | 0 | } |
158 | | |
159 | 137 | Tox_Event event; |
160 | 137 | event.type = TOX_EVENT_FRIEND_REQUEST; |
161 | 137 | event.data.friend_request = friend_request; |
162 | | |
163 | 137 | if (!tox_events_add(events, &event)) { |
164 | 0 | tox_event_friend_request_free(friend_request, mem); |
165 | 0 | return nullptr; |
166 | 0 | } |
167 | 137 | return friend_request; |
168 | 137 | } |
169 | | |
170 | | bool tox_event_friend_request_unpack( |
171 | | Tox_Event_Friend_Request **event, Bin_Unpack *bu, const Memory *mem) |
172 | 26 | { |
173 | 26 | assert(event != nullptr); |
174 | 26 | assert(*event == nullptr); |
175 | 26 | *event = tox_event_friend_request_new(mem); |
176 | | |
177 | 26 | if (*event == nullptr) { |
178 | 1 | return false; |
179 | 1 | } |
180 | | |
181 | 25 | return tox_event_friend_request_unpack_into(*event, bu); |
182 | 26 | } |
183 | | |
184 | | static Tox_Event_Friend_Request *_Nullable tox_event_friend_request_alloc(Tox_Events_State *_Nonnull state) |
185 | 137 | { |
186 | 137 | if (state->events == nullptr) { |
187 | 0 | return nullptr; |
188 | 0 | } |
189 | | |
190 | 137 | Tox_Event_Friend_Request *friend_request = tox_events_add_friend_request(state->events, state->mem); |
191 | | |
192 | 137 | if (friend_request == nullptr) { |
193 | 0 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
194 | 0 | return nullptr; |
195 | 0 | } |
196 | | |
197 | 137 | return friend_request; |
198 | 137 | } |
199 | | |
200 | | /***************************************************** |
201 | | * |
202 | | * :: event handler |
203 | | * |
204 | | *****************************************************/ |
205 | | |
206 | | void tox_events_handle_friend_request( |
207 | | Tox *_Nonnull tox, |
208 | | const uint8_t *_Nonnull public_key, |
209 | | const uint8_t *_Nullable message, size_t length, |
210 | | void *_Nullable user_data) |
211 | 137 | { |
212 | 137 | Tox_Events_State *state = tox_events_alloc(user_data); |
213 | 137 | Tox_Event_Friend_Request *friend_request = tox_event_friend_request_alloc(state); |
214 | | |
215 | 137 | if (friend_request == nullptr) { |
216 | 0 | return; |
217 | 0 | } |
218 | | |
219 | 137 | tox_event_friend_request_set_public_key(friend_request, public_key); |
220 | 137 | if (!tox_event_friend_request_set_message(friend_request, state->mem, message, length)) { |
221 | 0 | tox_event_friend_request_free(friend_request, state->mem); |
222 | 0 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
223 | 0 | return; |
224 | 0 | } |
225 | 137 | } |
226 | | |
227 | | void tox_events_handle_friend_request_dispatch(Tox *tox, const Tox_Event_Friend_Request *event, void *user_data) |
228 | 0 | { |
229 | 0 | if (tox->friend_request_callback == nullptr) { |
230 | 0 | return; |
231 | 0 | } |
232 | | |
233 | 0 | tox_unlock(tox); |
234 | 0 | tox->friend_request_callback(tox, event->public_key, event->message, event->message_length, user_data); |
235 | 0 | tox_lock(tox); |
236 | 0 | } |