/work/toxcore/events/friend_read_receipt.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_struct.h" |
18 | | |
19 | | /***************************************************** |
20 | | * |
21 | | * :: struct and accessors |
22 | | * |
23 | | *****************************************************/ |
24 | | |
25 | | struct Tox_Event_Friend_Read_Receipt { |
26 | | uint32_t friend_number; |
27 | | uint32_t message_id; |
28 | | }; |
29 | | |
30 | | static void tox_event_friend_read_receipt_set_friend_number(Tox_Event_Friend_Read_Receipt *_Nonnull friend_read_receipt, uint32_t friend_number) |
31 | 81.7k | { |
32 | 81.7k | assert(friend_read_receipt != nullptr); |
33 | 81.7k | friend_read_receipt->friend_number = friend_number; |
34 | 81.7k | } |
35 | | uint32_t tox_event_friend_read_receipt_get_friend_number(const Tox_Event_Friend_Read_Receipt *friend_read_receipt) |
36 | 1 | { |
37 | 1 | assert(friend_read_receipt != nullptr); |
38 | 1 | return friend_read_receipt->friend_number; |
39 | 1 | } |
40 | | |
41 | | static void tox_event_friend_read_receipt_set_message_id(Tox_Event_Friend_Read_Receipt *_Nonnull friend_read_receipt, uint32_t message_id) |
42 | 81.7k | { |
43 | 81.7k | assert(friend_read_receipt != nullptr); |
44 | 81.7k | friend_read_receipt->message_id = message_id; |
45 | 81.7k | } |
46 | | uint32_t tox_event_friend_read_receipt_get_message_id(const Tox_Event_Friend_Read_Receipt *friend_read_receipt) |
47 | 1 | { |
48 | 1 | assert(friend_read_receipt != nullptr); |
49 | 1 | return friend_read_receipt->message_id; |
50 | 1 | } |
51 | | |
52 | | static void tox_event_friend_read_receipt_construct(Tox_Event_Friend_Read_Receipt *_Nonnull friend_read_receipt) |
53 | 81.7k | { |
54 | 81.7k | *friend_read_receipt = (Tox_Event_Friend_Read_Receipt) { |
55 | 81.7k | 0 |
56 | 81.7k | }; |
57 | 81.7k | } |
58 | | static void tox_event_friend_read_receipt_destruct(Tox_Event_Friend_Read_Receipt *_Nonnull friend_read_receipt, const Memory *_Nonnull mem) |
59 | 81.7k | { |
60 | 81.7k | return; |
61 | 81.7k | } |
62 | | |
63 | | bool tox_event_friend_read_receipt_pack( |
64 | | const Tox_Event_Friend_Read_Receipt *event, Bin_Pack *bp) |
65 | 36 | { |
66 | 36 | return bin_pack_array(bp, 2) |
67 | 36 | && bin_pack_u32(bp, event->friend_number) |
68 | 36 | && bin_pack_u32(bp, event->message_id); |
69 | 36 | } |
70 | | |
71 | | static bool tox_event_friend_read_receipt_unpack_into(Tox_Event_Friend_Read_Receipt *_Nonnull event, Bin_Unpack *_Nonnull bu) |
72 | 33 | { |
73 | 33 | assert(event != nullptr); |
74 | 33 | if (!bin_unpack_array_fixed(bu, 2, nullptr)) { |
75 | 1 | return false; |
76 | 1 | } |
77 | | |
78 | 32 | return bin_unpack_u32(bu, &event->friend_number) |
79 | 32 | && bin_unpack_u32(bu, &event->message_id); |
80 | 33 | } |
81 | | |
82 | | /***************************************************** |
83 | | * |
84 | | * :: new/free/add/get/size/unpack |
85 | | * |
86 | | *****************************************************/ |
87 | | |
88 | | const Tox_Event_Friend_Read_Receipt *tox_event_get_friend_read_receipt(const Tox_Event *event) |
89 | 0 | { |
90 | 0 | return event->type == TOX_EVENT_FRIEND_READ_RECEIPT ? event->data.friend_read_receipt : nullptr; |
91 | 0 | } |
92 | | |
93 | | Tox_Event_Friend_Read_Receipt *tox_event_friend_read_receipt_new(const Memory *mem) |
94 | 81.7k | { |
95 | 81.7k | Tox_Event_Friend_Read_Receipt *const friend_read_receipt = |
96 | 81.7k | (Tox_Event_Friend_Read_Receipt *)mem_alloc(mem, sizeof(Tox_Event_Friend_Read_Receipt)); |
97 | | |
98 | 81.7k | if (friend_read_receipt == nullptr) { |
99 | 1 | return nullptr; |
100 | 1 | } |
101 | | |
102 | 81.7k | tox_event_friend_read_receipt_construct(friend_read_receipt); |
103 | 81.7k | return friend_read_receipt; |
104 | 81.7k | } |
105 | | |
106 | | void tox_event_friend_read_receipt_free(Tox_Event_Friend_Read_Receipt *friend_read_receipt, const Memory *mem) |
107 | 81.7k | { |
108 | 81.7k | if (friend_read_receipt != nullptr) { |
109 | 81.7k | tox_event_friend_read_receipt_destruct(friend_read_receipt, mem); |
110 | 81.7k | } |
111 | 81.7k | mem_delete(mem, friend_read_receipt); |
112 | 81.7k | } |
113 | | |
114 | | static Tox_Event_Friend_Read_Receipt *_Nullable tox_events_add_friend_read_receipt(Tox_Events *_Nonnull events, const Memory *_Nonnull mem) |
115 | 81.7k | { |
116 | 81.7k | Tox_Event_Friend_Read_Receipt *const friend_read_receipt = tox_event_friend_read_receipt_new(mem); |
117 | | |
118 | 81.7k | if (friend_read_receipt == nullptr) { |
119 | 0 | return nullptr; |
120 | 0 | } |
121 | | |
122 | 81.7k | Tox_Event event; |
123 | 81.7k | event.type = TOX_EVENT_FRIEND_READ_RECEIPT; |
124 | 81.7k | event.data.friend_read_receipt = friend_read_receipt; |
125 | | |
126 | 81.7k | if (!tox_events_add(events, &event)) { |
127 | 0 | tox_event_friend_read_receipt_free(friend_read_receipt, mem); |
128 | 0 | return nullptr; |
129 | 0 | } |
130 | 81.7k | return friend_read_receipt; |
131 | 81.7k | } |
132 | | |
133 | | bool tox_event_friend_read_receipt_unpack( |
134 | | Tox_Event_Friend_Read_Receipt **event, Bin_Unpack *bu, const Memory *mem) |
135 | 34 | { |
136 | 34 | assert(event != nullptr); |
137 | 34 | assert(*event == nullptr); |
138 | 34 | *event = tox_event_friend_read_receipt_new(mem); |
139 | | |
140 | 34 | if (*event == nullptr) { |
141 | 1 | return false; |
142 | 1 | } |
143 | | |
144 | 33 | return tox_event_friend_read_receipt_unpack_into(*event, bu); |
145 | 34 | } |
146 | | |
147 | | static Tox_Event_Friend_Read_Receipt *_Nullable tox_event_friend_read_receipt_alloc(Tox_Events_State *_Nonnull state) |
148 | 81.7k | { |
149 | 81.7k | if (state->events == nullptr) { |
150 | 0 | return nullptr; |
151 | 0 | } |
152 | | |
153 | 81.7k | Tox_Event_Friend_Read_Receipt *friend_read_receipt = tox_events_add_friend_read_receipt(state->events, state->mem); |
154 | | |
155 | 81.7k | if (friend_read_receipt == nullptr) { |
156 | 0 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
157 | 0 | return nullptr; |
158 | 0 | } |
159 | | |
160 | 81.7k | return friend_read_receipt; |
161 | 81.7k | } |
162 | | |
163 | | /***************************************************** |
164 | | * |
165 | | * :: event handler |
166 | | * |
167 | | *****************************************************/ |
168 | | |
169 | | void tox_events_handle_friend_read_receipt( |
170 | | Tox *_Nonnull tox, |
171 | | uint32_t friend_number, |
172 | | uint32_t message_id, |
173 | | void *_Nullable user_data) |
174 | 81.7k | { |
175 | 81.7k | Tox_Events_State *state = tox_events_alloc(user_data); |
176 | 81.7k | Tox_Event_Friend_Read_Receipt *friend_read_receipt = tox_event_friend_read_receipt_alloc(state); |
177 | | |
178 | 81.7k | if (friend_read_receipt == nullptr) { |
179 | 0 | return; |
180 | 0 | } |
181 | | |
182 | 81.7k | tox_event_friend_read_receipt_set_friend_number(friend_read_receipt, friend_number); |
183 | 81.7k | tox_event_friend_read_receipt_set_message_id(friend_read_receipt, message_id); |
184 | 81.7k | } |
185 | | |
186 | | void tox_events_handle_friend_read_receipt_dispatch(Tox *tox, const Tox_Event_Friend_Read_Receipt *event, void *user_data) |
187 | 0 | { |
188 | 0 | if (tox->friend_read_receipt_callback == nullptr) { |
189 | 0 | return; |
190 | 0 | } |
191 | | |
192 | 0 | tox_unlock(tox); |
193 | 0 | tox->friend_read_receipt_callback(tox, event->friend_number, event->message_id, user_data); |
194 | 0 | tox_lock(tox); |
195 | 0 | } |