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