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