/work/toxcore/events/group_moderation.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 | | |
9 | | #include "../bin_pack.h" |
10 | | #include "../bin_unpack.h" |
11 | | #include "../ccompat.h" |
12 | | #include "../mem.h" |
13 | | #include "../tox.h" |
14 | | #include "../tox_events.h" |
15 | | #include "../tox_pack.h" |
16 | | #include "../tox_unpack.h" |
17 | | |
18 | | |
19 | | /***************************************************** |
20 | | * |
21 | | * :: struct and accessors |
22 | | * |
23 | | *****************************************************/ |
24 | | |
25 | | |
26 | | struct Tox_Event_Group_Moderation { |
27 | | uint32_t group_number; |
28 | | uint32_t source_peer_id; |
29 | | uint32_t target_peer_id; |
30 | | Tox_Group_Mod_Event mod_type; |
31 | | }; |
32 | | |
33 | | non_null() |
34 | | static void tox_event_group_moderation_set_group_number(Tox_Event_Group_Moderation *group_moderation, |
35 | | uint32_t group_number) |
36 | 110 | { |
37 | 110 | assert(group_moderation != nullptr); |
38 | 110 | group_moderation->group_number = group_number; |
39 | 110 | } |
40 | | uint32_t tox_event_group_moderation_get_group_number(const Tox_Event_Group_Moderation *group_moderation) |
41 | 43 | { |
42 | 43 | assert(group_moderation != nullptr); |
43 | 43 | return group_moderation->group_number; |
44 | 43 | } |
45 | | |
46 | | non_null() |
47 | | static void tox_event_group_moderation_set_source_peer_id(Tox_Event_Group_Moderation *group_moderation, |
48 | | uint32_t source_peer_id) |
49 | 110 | { |
50 | 110 | assert(group_moderation != nullptr); |
51 | 110 | group_moderation->source_peer_id = source_peer_id; |
52 | 110 | } |
53 | | uint32_t tox_event_group_moderation_get_source_peer_id(const Tox_Event_Group_Moderation *group_moderation) |
54 | 0 | { |
55 | 0 | assert(group_moderation != nullptr); |
56 | 0 | return group_moderation->source_peer_id; |
57 | 0 | } |
58 | | |
59 | | non_null() |
60 | | static void tox_event_group_moderation_set_target_peer_id(Tox_Event_Group_Moderation *group_moderation, |
61 | | uint32_t target_peer_id) |
62 | 110 | { |
63 | 110 | assert(group_moderation != nullptr); |
64 | 110 | group_moderation->target_peer_id = target_peer_id; |
65 | 110 | } |
66 | | uint32_t tox_event_group_moderation_get_target_peer_id(const Tox_Event_Group_Moderation *group_moderation) |
67 | 43 | { |
68 | 43 | assert(group_moderation != nullptr); |
69 | 43 | return group_moderation->target_peer_id; |
70 | 43 | } |
71 | | |
72 | | non_null() |
73 | | static void tox_event_group_moderation_set_mod_type(Tox_Event_Group_Moderation *group_moderation, |
74 | | Tox_Group_Mod_Event mod_type) |
75 | 110 | { |
76 | 110 | assert(group_moderation != nullptr); |
77 | 110 | group_moderation->mod_type = mod_type; |
78 | 110 | } |
79 | | Tox_Group_Mod_Event tox_event_group_moderation_get_mod_type(const Tox_Event_Group_Moderation *group_moderation) |
80 | 43 | { |
81 | 43 | assert(group_moderation != nullptr); |
82 | 43 | return group_moderation->mod_type; |
83 | 43 | } |
84 | | |
85 | | non_null() |
86 | | static void tox_event_group_moderation_construct(Tox_Event_Group_Moderation *group_moderation) |
87 | 162 | { |
88 | 162 | *group_moderation = (Tox_Event_Group_Moderation) { |
89 | 162 | 0 |
90 | 162 | }; |
91 | 162 | } |
92 | | non_null() |
93 | | static void tox_event_group_moderation_destruct(Tox_Event_Group_Moderation *group_moderation, const Memory *mem) |
94 | 162 | { |
95 | 162 | return; |
96 | 162 | } |
97 | | |
98 | | bool tox_event_group_moderation_pack( |
99 | | const Tox_Event_Group_Moderation *event, Bin_Pack *bp) |
100 | 26 | { |
101 | 26 | return bin_pack_array(bp, 4) |
102 | 26 | && bin_pack_u32(bp, event->group_number) |
103 | 26 | && bin_pack_u32(bp, event->source_peer_id) |
104 | 26 | && bin_pack_u32(bp, event->target_peer_id) |
105 | 26 | && tox_group_mod_event_pack(event->mod_type, bp); |
106 | 26 | } |
107 | | |
108 | | non_null() |
109 | | static bool tox_event_group_moderation_unpack_into( |
110 | | Tox_Event_Group_Moderation *event, Bin_Unpack *bu) |
111 | 52 | { |
112 | 52 | assert(event != nullptr); |
113 | 52 | if (!bin_unpack_array_fixed(bu, 4, nullptr)) { |
114 | 1 | return false; |
115 | 1 | } |
116 | | |
117 | 51 | return bin_unpack_u32(bu, &event->group_number) |
118 | 51 | && bin_unpack_u32(bu, &event->source_peer_id) |
119 | 51 | && bin_unpack_u32(bu, &event->target_peer_id) |
120 | 51 | && tox_group_mod_event_unpack(&event->mod_type, bu); |
121 | 52 | } |
122 | | |
123 | | |
124 | | /***************************************************** |
125 | | * |
126 | | * :: new/free/add/get/size/unpack |
127 | | * |
128 | | *****************************************************/ |
129 | | |
130 | | const Tox_Event_Group_Moderation *tox_event_get_group_moderation(const Tox_Event *event) |
131 | 0 | { |
132 | 0 | return event->type == TOX_EVENT_GROUP_MODERATION ? event->data.group_moderation : nullptr; |
133 | 0 | } |
134 | | |
135 | | Tox_Event_Group_Moderation *tox_event_group_moderation_new(const Memory *mem) |
136 | 163 | { |
137 | 163 | Tox_Event_Group_Moderation *const group_moderation = |
138 | 163 | (Tox_Event_Group_Moderation *)mem_alloc(mem, sizeof(Tox_Event_Group_Moderation)); |
139 | | |
140 | 163 | if (group_moderation == nullptr) { |
141 | 1 | return nullptr; |
142 | 1 | } |
143 | | |
144 | 162 | tox_event_group_moderation_construct(group_moderation); |
145 | 162 | return group_moderation; |
146 | 163 | } |
147 | | |
148 | | void tox_event_group_moderation_free(Tox_Event_Group_Moderation *group_moderation, const Memory *mem) |
149 | 163 | { |
150 | 163 | if (group_moderation != nullptr) { |
151 | 162 | tox_event_group_moderation_destruct(group_moderation, mem); |
152 | 162 | } |
153 | 163 | mem_delete(mem, group_moderation); |
154 | 163 | } |
155 | | |
156 | | non_null() |
157 | | static Tox_Event_Group_Moderation *tox_events_add_group_moderation(Tox_Events *events, const Memory *mem) |
158 | 110 | { |
159 | 110 | Tox_Event_Group_Moderation *const group_moderation = tox_event_group_moderation_new(mem); |
160 | | |
161 | 110 | if (group_moderation == nullptr) { |
162 | 0 | return nullptr; |
163 | 0 | } |
164 | | |
165 | 110 | Tox_Event event; |
166 | 110 | event.type = TOX_EVENT_GROUP_MODERATION; |
167 | 110 | event.data.group_moderation = group_moderation; |
168 | | |
169 | 110 | tox_events_add(events, &event); |
170 | 110 | return group_moderation; |
171 | 110 | } |
172 | | |
173 | | bool tox_event_group_moderation_unpack( |
174 | | Tox_Event_Group_Moderation **event, Bin_Unpack *bu, const Memory *mem) |
175 | 53 | { |
176 | 53 | assert(event != nullptr); |
177 | 53 | assert(*event == nullptr); |
178 | 53 | *event = tox_event_group_moderation_new(mem); |
179 | | |
180 | 53 | if (*event == nullptr) { |
181 | 1 | return false; |
182 | 1 | } |
183 | | |
184 | 52 | return tox_event_group_moderation_unpack_into(*event, bu); |
185 | 53 | } |
186 | | |
187 | | non_null() |
188 | | static Tox_Event_Group_Moderation *tox_event_group_moderation_alloc(void *user_data) |
189 | 110 | { |
190 | 110 | Tox_Events_State *state = tox_events_alloc(user_data); |
191 | 110 | assert(state != nullptr); |
192 | | |
193 | 110 | if (state->events == nullptr) { |
194 | 0 | return nullptr; |
195 | 0 | } |
196 | | |
197 | 110 | Tox_Event_Group_Moderation *group_moderation = tox_events_add_group_moderation(state->events, state->mem); |
198 | | |
199 | 110 | if (group_moderation == nullptr) { |
200 | 0 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
201 | 0 | return nullptr; |
202 | 0 | } |
203 | | |
204 | 110 | return group_moderation; |
205 | 110 | } |
206 | | |
207 | | |
208 | | /***************************************************** |
209 | | * |
210 | | * :: event handler |
211 | | * |
212 | | *****************************************************/ |
213 | | |
214 | | |
215 | | void tox_events_handle_group_moderation(Tox *tox, uint32_t group_number, uint32_t source_peer_id, uint32_t target_peer_id, Tox_Group_Mod_Event mod_type, |
216 | | void *user_data) |
217 | 110 | { |
218 | 110 | Tox_Event_Group_Moderation *group_moderation = tox_event_group_moderation_alloc(user_data); |
219 | | |
220 | 110 | if (group_moderation == nullptr) { |
221 | 0 | return; |
222 | 0 | } |
223 | | |
224 | 110 | tox_event_group_moderation_set_group_number(group_moderation, group_number); |
225 | 110 | tox_event_group_moderation_set_source_peer_id(group_moderation, source_peer_id); |
226 | 110 | tox_event_group_moderation_set_target_peer_id(group_moderation, target_peer_id); |
227 | 110 | tox_event_group_moderation_set_mod_type(group_moderation, mod_type); |
228 | 110 | } |