/work/toxcore/events/group_private_message.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_Group_Private_Message { |
29 | | uint32_t group_number; |
30 | | uint32_t peer_id; |
31 | | Tox_Message_Type type; |
32 | | uint8_t *message; |
33 | | uint32_t message_length; |
34 | | }; |
35 | | |
36 | | non_null() |
37 | | static void tox_event_group_private_message_set_group_number(Tox_Event_Group_Private_Message *group_private_message, |
38 | | uint32_t group_number) |
39 | 1 | { |
40 | 1 | assert(group_private_message != nullptr); |
41 | 1 | group_private_message->group_number = group_number; |
42 | 1 | } |
43 | | uint32_t tox_event_group_private_message_get_group_number(const Tox_Event_Group_Private_Message *group_private_message) |
44 | 1 | { |
45 | 1 | assert(group_private_message != nullptr); |
46 | 1 | return group_private_message->group_number; |
47 | 1 | } |
48 | | |
49 | | non_null() |
50 | | static void tox_event_group_private_message_set_peer_id(Tox_Event_Group_Private_Message *group_private_message, |
51 | | uint32_t peer_id) |
52 | 1 | { |
53 | 1 | assert(group_private_message != nullptr); |
54 | 1 | group_private_message->peer_id = peer_id; |
55 | 1 | } |
56 | | uint32_t tox_event_group_private_message_get_peer_id(const Tox_Event_Group_Private_Message *group_private_message) |
57 | 1 | { |
58 | 1 | assert(group_private_message != nullptr); |
59 | 1 | return group_private_message->peer_id; |
60 | 1 | } |
61 | | |
62 | | non_null() |
63 | | static void tox_event_group_private_message_set_type(Tox_Event_Group_Private_Message *group_private_message, |
64 | | Tox_Message_Type type) |
65 | 1 | { |
66 | 1 | assert(group_private_message != nullptr); |
67 | 1 | group_private_message->type = type; |
68 | 1 | } |
69 | | Tox_Message_Type tox_event_group_private_message_get_type(const Tox_Event_Group_Private_Message *group_private_message) |
70 | 1 | { |
71 | 1 | assert(group_private_message != nullptr); |
72 | 1 | return group_private_message->type; |
73 | 1 | } |
74 | | |
75 | | non_null(1) nullable(2) |
76 | | static bool tox_event_group_private_message_set_message(Tox_Event_Group_Private_Message *group_private_message, |
77 | | const uint8_t *message, uint32_t message_length) |
78 | 1 | { |
79 | 1 | assert(group_private_message != nullptr); |
80 | | |
81 | 1 | if (group_private_message->message != nullptr) { |
82 | 0 | free(group_private_message->message); |
83 | 0 | group_private_message->message = nullptr; |
84 | 0 | group_private_message->message_length = 0; |
85 | 0 | } |
86 | | |
87 | 1 | if (message == nullptr) { |
88 | 0 | assert(message_length == 0); |
89 | 0 | return true; |
90 | 0 | } |
91 | | |
92 | 1 | uint8_t *message_copy = (uint8_t *)malloc(message_length); |
93 | | |
94 | 1 | if (message_copy == nullptr) { |
95 | 0 | return false; |
96 | 0 | } |
97 | | |
98 | 1 | memcpy(message_copy, message, message_length); |
99 | 1 | group_private_message->message = message_copy; |
100 | 1 | group_private_message->message_length = message_length; |
101 | 1 | return true; |
102 | 1 | } |
103 | | uint32_t tox_event_group_private_message_get_message_length(const Tox_Event_Group_Private_Message *group_private_message) |
104 | 1 | { |
105 | 1 | assert(group_private_message != nullptr); |
106 | 1 | return group_private_message->message_length; |
107 | 1 | } |
108 | | const uint8_t *tox_event_group_private_message_get_message(const Tox_Event_Group_Private_Message *group_private_message) |
109 | 1 | { |
110 | 1 | assert(group_private_message != nullptr); |
111 | 1 | return group_private_message->message; |
112 | 1 | } |
113 | | |
114 | | non_null() |
115 | | static void tox_event_group_private_message_construct(Tox_Event_Group_Private_Message *group_private_message) |
116 | 7 | { |
117 | 7 | *group_private_message = (Tox_Event_Group_Private_Message) { |
118 | 7 | 0 |
119 | 7 | }; |
120 | 7 | } |
121 | | non_null() |
122 | | static void tox_event_group_private_message_destruct(Tox_Event_Group_Private_Message *group_private_message, const Memory *mem) |
123 | 7 | { |
124 | 7 | free(group_private_message->message); |
125 | 7 | } |
126 | | |
127 | | bool tox_event_group_private_message_pack( |
128 | | const Tox_Event_Group_Private_Message *event, Bin_Pack *bp) |
129 | 2 | { |
130 | 2 | return bin_pack_array(bp, 4) |
131 | 2 | && bin_pack_u32(bp, event->group_number) |
132 | 2 | && bin_pack_u32(bp, event->peer_id) |
133 | 2 | && tox_message_type_pack(event->type, bp) |
134 | 2 | && bin_pack_bin(bp, event->message, event->message_length); |
135 | 2 | } |
136 | | |
137 | | non_null() |
138 | | static bool tox_event_group_private_message_unpack_into( |
139 | | Tox_Event_Group_Private_Message *event, Bin_Unpack *bu) |
140 | 6 | { |
141 | 6 | assert(event != nullptr); |
142 | 6 | if (!bin_unpack_array_fixed(bu, 4, nullptr)) { |
143 | 1 | return false; |
144 | 1 | } |
145 | | |
146 | 5 | return bin_unpack_u32(bu, &event->group_number) |
147 | 5 | && bin_unpack_u32(bu, &event->peer_id) |
148 | 5 | && tox_message_type_unpack(&event->type, bu) |
149 | 5 | && bin_unpack_bin(bu, &event->message, &event->message_length); |
150 | 6 | } |
151 | | |
152 | | |
153 | | /***************************************************** |
154 | | * |
155 | | * :: new/free/add/get/size/unpack |
156 | | * |
157 | | *****************************************************/ |
158 | | |
159 | | const Tox_Event_Group_Private_Message *tox_event_get_group_private_message(const Tox_Event *event) |
160 | 0 | { |
161 | 0 | return event->type == TOX_EVENT_GROUP_PRIVATE_MESSAGE ? event->data.group_private_message : nullptr; |
162 | 0 | } |
163 | | |
164 | | Tox_Event_Group_Private_Message *tox_event_group_private_message_new(const Memory *mem) |
165 | 8 | { |
166 | 8 | Tox_Event_Group_Private_Message *const group_private_message = |
167 | 8 | (Tox_Event_Group_Private_Message *)mem_alloc(mem, sizeof(Tox_Event_Group_Private_Message)); |
168 | | |
169 | 8 | if (group_private_message == nullptr) { |
170 | 1 | return nullptr; |
171 | 1 | } |
172 | | |
173 | 7 | tox_event_group_private_message_construct(group_private_message); |
174 | 7 | return group_private_message; |
175 | 8 | } |
176 | | |
177 | | void tox_event_group_private_message_free(Tox_Event_Group_Private_Message *group_private_message, const Memory *mem) |
178 | 8 | { |
179 | 8 | if (group_private_message != nullptr) { |
180 | 7 | tox_event_group_private_message_destruct(group_private_message, mem); |
181 | 7 | } |
182 | 8 | mem_delete(mem, group_private_message); |
183 | 8 | } |
184 | | |
185 | | non_null() |
186 | | static Tox_Event_Group_Private_Message *tox_events_add_group_private_message(Tox_Events *events, const Memory *mem) |
187 | 1 | { |
188 | 1 | Tox_Event_Group_Private_Message *const group_private_message = tox_event_group_private_message_new(mem); |
189 | | |
190 | 1 | if (group_private_message == nullptr) { |
191 | 0 | return nullptr; |
192 | 0 | } |
193 | | |
194 | 1 | Tox_Event event; |
195 | 1 | event.type = TOX_EVENT_GROUP_PRIVATE_MESSAGE; |
196 | 1 | event.data.group_private_message = group_private_message; |
197 | | |
198 | 1 | tox_events_add(events, &event); |
199 | 1 | return group_private_message; |
200 | 1 | } |
201 | | |
202 | | bool tox_event_group_private_message_unpack( |
203 | | Tox_Event_Group_Private_Message **event, Bin_Unpack *bu, const Memory *mem) |
204 | 7 | { |
205 | 7 | assert(event != nullptr); |
206 | 7 | assert(*event == nullptr); |
207 | 7 | *event = tox_event_group_private_message_new(mem); |
208 | | |
209 | 7 | if (*event == nullptr) { |
210 | 1 | return false; |
211 | 1 | } |
212 | | |
213 | 6 | return tox_event_group_private_message_unpack_into(*event, bu); |
214 | 7 | } |
215 | | |
216 | | non_null() |
217 | | static Tox_Event_Group_Private_Message *tox_event_group_private_message_alloc(void *user_data) |
218 | 1 | { |
219 | 1 | Tox_Events_State *state = tox_events_alloc(user_data); |
220 | 1 | assert(state != nullptr); |
221 | | |
222 | 1 | if (state->events == nullptr) { |
223 | 0 | return nullptr; |
224 | 0 | } |
225 | | |
226 | 1 | Tox_Event_Group_Private_Message *group_private_message = tox_events_add_group_private_message(state->events, state->mem); |
227 | | |
228 | 1 | if (group_private_message == nullptr) { |
229 | 0 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
230 | 0 | return nullptr; |
231 | 0 | } |
232 | | |
233 | 1 | return group_private_message; |
234 | 1 | } |
235 | | |
236 | | |
237 | | /***************************************************** |
238 | | * |
239 | | * :: event handler |
240 | | * |
241 | | *****************************************************/ |
242 | | |
243 | | |
244 | | void tox_events_handle_group_private_message(Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Message_Type type, const uint8_t *message, size_t length, |
245 | | void *user_data) |
246 | 1 | { |
247 | 1 | Tox_Event_Group_Private_Message *group_private_message = tox_event_group_private_message_alloc(user_data); |
248 | | |
249 | 1 | if (group_private_message == nullptr) { |
250 | 0 | return; |
251 | 0 | } |
252 | | |
253 | 1 | tox_event_group_private_message_set_group_number(group_private_message, group_number); |
254 | 1 | tox_event_group_private_message_set_peer_id(group_private_message, peer_id); |
255 | 1 | tox_event_group_private_message_set_type(group_private_message, type); |
256 | 1 | tox_event_group_private_message_set_message(group_private_message, message, length); |
257 | 1 | } |