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