/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 © 2022-2025 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_private.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 *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 *_Nonnull public_key) |
33 | 136 | { |
34 | 136 | assert(friend_request != nullptr); |
35 | | |
36 | 136 | memcpy(friend_request->public_key, public_key, TOX_PUBLIC_KEY_SIZE); |
37 | 136 | return true; |
38 | 136 | } |
39 | | const uint8_t *tox_event_friend_request_get_public_key(const Tox_Event_Friend_Request *friend_request) |
40 | 136 | { |
41 | 136 | assert(friend_request != nullptr); |
42 | 136 | return friend_request->public_key; |
43 | 136 | } |
44 | | |
45 | | static bool tox_event_friend_request_set_message(Tox_Event_Friend_Request *_Nonnull friend_request, const uint8_t *_Nonnull message, uint32_t message_length, const Memory *_Nonnull mem) |
46 | 136 | { |
47 | 136 | assert(friend_request != nullptr); |
48 | | |
49 | 136 | if (friend_request->message != nullptr) { |
50 | 0 | mem_delete(mem, friend_request->message); |
51 | 0 | friend_request->message = nullptr; |
52 | 0 | friend_request->message_length = 0; |
53 | 0 | } |
54 | | |
55 | 136 | uint8_t *message_copy = (uint8_t *)mem_balloc(mem, message_length); |
56 | | |
57 | 136 | if (message_copy == nullptr) { |
58 | 0 | return false; |
59 | 0 | } |
60 | | |
61 | 136 | memcpy(message_copy, message, message_length); |
62 | 136 | friend_request->message = message_copy; |
63 | 136 | friend_request->message_length = message_length; |
64 | 136 | return true; |
65 | 136 | } |
66 | | uint32_t tox_event_friend_request_get_message_length(const Tox_Event_Friend_Request *friend_request) |
67 | 136 | { |
68 | 136 | assert(friend_request != nullptr); |
69 | 136 | return friend_request->message_length; |
70 | 136 | } |
71 | | const uint8_t *tox_event_friend_request_get_message(const Tox_Event_Friend_Request *friend_request) |
72 | 136 | { |
73 | 136 | assert(friend_request != nullptr); |
74 | 136 | return friend_request->message; |
75 | 136 | } |
76 | | |
77 | | static void tox_event_friend_request_construct(Tox_Event_Friend_Request *_Nonnull friend_request) |
78 | 162 | { |
79 | 162 | *friend_request = (Tox_Event_Friend_Request) { |
80 | 162 | { |
81 | 162 | 0 |
82 | 162 | } |
83 | 162 | }; |
84 | 162 | } |
85 | | static void tox_event_friend_request_destruct(Tox_Event_Friend_Request *_Nonnull friend_request, const Memory *_Nonnull mem) |
86 | 162 | { |
87 | 162 | mem_delete(mem, friend_request->message); |
88 | 162 | } |
89 | | |
90 | | bool tox_event_friend_request_pack( |
91 | | const Tox_Event_Friend_Request *event, Bin_Pack *bp) |
92 | 20 | { |
93 | 20 | return bin_pack_array(bp, 2) |
94 | 20 | && bin_pack_bin(bp, event->public_key, TOX_PUBLIC_KEY_SIZE) |
95 | 20 | && bin_pack_bin(bp, event->message, event->message_length); |
96 | 20 | } |
97 | | |
98 | | static bool tox_event_friend_request_unpack_into(Tox_Event_Friend_Request *_Nonnull event, Bin_Unpack *_Nonnull bu) |
99 | 26 | { |
100 | 26 | assert(event != nullptr); |
101 | 26 | if (!bin_unpack_array_fixed(bu, 2, nullptr)) { |
102 | 2 | return false; |
103 | 2 | } |
104 | | |
105 | 24 | return bin_unpack_bin_fixed(bu, event->public_key, TOX_PUBLIC_KEY_SIZE) |
106 | 24 | && bin_unpack_bin(bu, &event->message, &event->message_length); |
107 | 26 | } |
108 | | |
109 | | const Tox_Event_Friend_Request *tox_event_get_friend_request( |
110 | | const Tox_Event *event) |
111 | 0 | { |
112 | 0 | return event->type == TOX_EVENT_FRIEND_REQUEST ? event->data.friend_request : nullptr; |
113 | 0 | } |
114 | | |
115 | | Tox_Event_Friend_Request *tox_event_friend_request_new(const Memory *mem) |
116 | 163 | { |
117 | 163 | Tox_Event_Friend_Request *const friend_request = |
118 | 163 | (Tox_Event_Friend_Request *)mem_alloc(mem, sizeof(Tox_Event_Friend_Request)); |
119 | | |
120 | 163 | if (friend_request == nullptr) { |
121 | 1 | return nullptr; |
122 | 1 | } |
123 | | |
124 | 162 | tox_event_friend_request_construct(friend_request); |
125 | 162 | return friend_request; |
126 | 163 | } |
127 | | |
128 | | void tox_event_friend_request_free(Tox_Event_Friend_Request *friend_request, const Memory *mem) |
129 | 163 | { |
130 | 163 | if (friend_request != nullptr) { |
131 | 162 | tox_event_friend_request_destruct(friend_request, mem); |
132 | 162 | } |
133 | 163 | mem_delete(mem, friend_request); |
134 | 163 | } |
135 | | |
136 | | static Tox_Event_Friend_Request *tox_events_add_friend_request(Tox_Events *_Nonnull events, const Memory *_Nonnull mem) |
137 | 136 | { |
138 | 136 | Tox_Event_Friend_Request *const friend_request = tox_event_friend_request_new(mem); |
139 | | |
140 | 136 | if (friend_request == nullptr) { |
141 | 0 | return nullptr; |
142 | 0 | } |
143 | | |
144 | 136 | Tox_Event event; |
145 | 136 | event.type = TOX_EVENT_FRIEND_REQUEST; |
146 | 136 | event.data.friend_request = friend_request; |
147 | | |
148 | 136 | if (!tox_events_add(events, &event)) { |
149 | 0 | tox_event_friend_request_free(friend_request, mem); |
150 | 0 | return nullptr; |
151 | 0 | } |
152 | 136 | return friend_request; |
153 | 136 | } |
154 | | |
155 | | bool tox_event_friend_request_unpack( |
156 | | Tox_Event_Friend_Request **event, Bin_Unpack *bu, const Memory *mem) |
157 | 27 | { |
158 | 27 | assert(event != nullptr); |
159 | 27 | assert(*event == nullptr); |
160 | 27 | *event = tox_event_friend_request_new(mem); |
161 | | |
162 | 27 | if (*event == nullptr) { |
163 | 1 | return false; |
164 | 1 | } |
165 | | |
166 | 26 | return tox_event_friend_request_unpack_into(*event, bu); |
167 | 27 | } |
168 | | |
169 | | static Tox_Event_Friend_Request *tox_event_friend_request_alloc(void *_Nonnull user_data) |
170 | 136 | { |
171 | 136 | Tox_Events_State *state = tox_events_alloc(user_data); |
172 | 136 | assert(state != nullptr); |
173 | | |
174 | 136 | if (state->events == nullptr) { |
175 | 0 | return nullptr; |
176 | 0 | } |
177 | | |
178 | 136 | Tox_Event_Friend_Request *friend_request = tox_events_add_friend_request(state->events, state->mem); |
179 | | |
180 | 136 | if (friend_request == nullptr) { |
181 | 0 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
182 | 0 | return nullptr; |
183 | 0 | } |
184 | | |
185 | 136 | return friend_request; |
186 | 136 | } |
187 | | |
188 | | /***************************************************** |
189 | | * |
190 | | * :: event handler |
191 | | * |
192 | | *****************************************************/ |
193 | | |
194 | | void tox_events_handle_friend_request(Tox *tox, const uint8_t *public_key, const uint8_t *message, size_t length, |
195 | | void *user_data) |
196 | 136 | { |
197 | 136 | Tox_Event_Friend_Request *friend_request = tox_event_friend_request_alloc(user_data); |
198 | | |
199 | 136 | if (friend_request == nullptr) { |
200 | 0 | return; |
201 | 0 | } |
202 | | |
203 | 136 | const Tox_System *sys = tox_get_system(tox); |
204 | | |
205 | 136 | tox_event_friend_request_set_public_key(friend_request, public_key); |
206 | 136 | tox_event_friend_request_set_message(friend_request, message, length, sys->mem); |
207 | 136 | } |