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