1 | #include "mtx/events.hpp" |
2 | |
3 | #include <nlohmann/json.hpp> |
4 | |
5 | using json = nlohmann::json; |
6 | |
7 | namespace mtx { |
8 | namespace events { |
9 | |
10 | EventType |
11 | getEventType(const std::string &type) |
12 | { |
13 | if (type == "m.key.verification.request" ) |
14 | return EventType::KeyVerificationRequest; |
15 | else if (type == "m.key.verification.start" ) |
16 | return EventType::KeyVerificationStart; |
17 | else if (type == "m.key.verification.accept" ) |
18 | return EventType::KeyVerificationAccept; |
19 | else if (type == "m.key.verification.ready" ) |
20 | return EventType::KeyVerificationReady; |
21 | else if (type == "m.key.verification.done" ) |
22 | return EventType::KeyVerificationDone; |
23 | else if (type == "m.key.verification.key" ) |
24 | return EventType::KeyVerificationKey; |
25 | else if (type == "m.key.verification.mac" ) |
26 | return EventType::KeyVerificationMac; |
27 | else if (type == "m.key.verification.cancel" ) |
28 | return EventType::KeyVerificationCancel; |
29 | else if (type == "m.reaction" ) |
30 | return EventType::Reaction; |
31 | else if (type == "m.room_key" ) |
32 | return EventType::RoomKey; |
33 | else if (type == "m.forwarded_room_key" ) |
34 | return EventType::ForwardedRoomKey; |
35 | else if (type == "m.room_key_request" ) |
36 | return EventType::RoomKeyRequest; |
37 | else if (type == "m.room.aliases" ) |
38 | return EventType::RoomAliases; |
39 | else if (type == "m.room.avatar" ) |
40 | return EventType::RoomAvatar; |
41 | else if (type == "m.room.canonical_alias" ) |
42 | return EventType::RoomCanonicalAlias; |
43 | else if (type == "m.room.create" ) |
44 | return EventType::RoomCreate; |
45 | else if (type == "m.room.encrypted" ) |
46 | return EventType::RoomEncrypted; |
47 | else if (type == "m.dummy" ) |
48 | return EventType::Dummy; |
49 | else if (type == "m.room.encryption" ) |
50 | return EventType::RoomEncryption; |
51 | else if (type == "m.room.guest_access" ) |
52 | return EventType::RoomGuestAccess; |
53 | else if (type == "m.room.history_visibility" ) |
54 | return EventType::RoomHistoryVisibility; |
55 | else if (type == "m.room.join_rules" ) |
56 | return EventType::RoomJoinRules; |
57 | else if (type == "m.room.member" ) |
58 | return EventType::RoomMember; |
59 | else if (type == "m.room.message" ) |
60 | return EventType::RoomMessage; |
61 | else if (type == "m.room.name" ) |
62 | return EventType::RoomName; |
63 | else if (type == "m.room.power_levels" ) |
64 | return EventType::RoomPowerLevels; |
65 | else if (type == "m.room.topic" ) |
66 | return EventType::RoomTopic; |
67 | else if (type == "m.widget" ) |
68 | return EventType::Widget; |
69 | else if (type == "im.vector.modular.widgets" ) |
70 | return EventType::VectorWidget; |
71 | else if (type == "m.room.redaction" ) |
72 | return EventType::RoomRedaction; |
73 | else if (type == "m.room.pinned_events" ) |
74 | return EventType::RoomPinnedEvents; |
75 | else if (type == "m.room.tombstone" ) |
76 | return EventType::RoomTombstone; |
77 | else if (type == "m.sticker" ) |
78 | return EventType::Sticker; |
79 | |
80 | else if (type == "m.policy.rule.user" ) |
81 | return EventType::PolicyRuleUser; |
82 | else if (type == "m.policy.rule.room" ) |
83 | return EventType::PolicyRuleRoom; |
84 | else if (type == "m.policy.rule.server" ) |
85 | return EventType::PolicyRuleServer; |
86 | |
87 | else if (type == "m.space.child" ) |
88 | return EventType::SpaceChild; |
89 | else if (type == "m.space.parent" ) |
90 | return EventType::SpaceParent; |
91 | |
92 | else if (type == "m.tag" ) |
93 | return EventType::Tag; |
94 | else if (type == "m.direct" ) |
95 | return EventType::Direct; |
96 | else if (type == "m.presence" ) |
97 | return EventType::Presence; |
98 | else if (type == "m.push_rules" ) |
99 | return EventType::PushRules; |
100 | else if (type == "m.call.invite" ) |
101 | return EventType::CallInvite; |
102 | else if (type == "m.call.candidates" ) |
103 | return EventType::CallCandidates; |
104 | else if (type == "m.call.answer" ) |
105 | return EventType::CallAnswer; |
106 | else if (type == "m.call.hangup" ) |
107 | return EventType::CallHangUp; |
108 | else if (type == "m.call.select_answer" ) |
109 | return EventType::CallSelectAnswer; |
110 | else if (type == "m.call.reject" ) |
111 | return EventType::CallReject; |
112 | else if (type == "m.call.negotiate" ) |
113 | return EventType::CallNegotiate; |
114 | else if (type == "m.secret.request" ) |
115 | return EventType::SecretRequest; |
116 | else if (type == "m.secret.send" ) |
117 | return EventType::SecretSend; |
118 | else if (type == "m.typing" ) |
119 | return EventType::Typing; |
120 | else if (type == "m.receipt" ) |
121 | return EventType::Receipt; |
122 | else if (type == "m.fully_read" ) |
123 | return EventType::FullyRead; |
124 | else if (type == "im.nheko.hidden_events" ) |
125 | return EventType::NhekoHiddenEvents; |
126 | else if (type == "im.ponies.room_emotes" ) |
127 | return EventType::ImagePackInRoom; |
128 | else if (type == "im.ponies.user_emotes" ) |
129 | return EventType::ImagePackInAccountData; |
130 | else if (type == "im.ponies.emote_rooms" ) |
131 | return EventType::ImagePackRooms; |
132 | else |
133 | return EventType::Unsupported; |
134 | } |
135 | |
136 | std::string |
137 | to_string(EventType type) |
138 | { |
139 | switch (type) { |
140 | case EventType::KeyVerificationCancel: |
141 | return "m.key.verification.cancel" ; |
142 | case EventType::KeyVerificationRequest: |
143 | return "m.key.verification.request" ; |
144 | case EventType::KeyVerificationStart: |
145 | return "m.key.verification.start" ; |
146 | case EventType::KeyVerificationAccept: |
147 | return "m.key.verification.accept" ; |
148 | case EventType::KeyVerificationKey: |
149 | return "m.key.verification.key" ; |
150 | case EventType::KeyVerificationReady: |
151 | return "m.key.verification.ready" ; |
152 | case EventType::KeyVerificationDone: |
153 | return "m.key.verification.done" ; |
154 | case EventType::KeyVerificationMac: |
155 | return "m.key.verification.mac" ; |
156 | case EventType::Reaction: |
157 | return "m.reaction" ; |
158 | case EventType::RoomKey: |
159 | return "m.room_key" ; |
160 | case EventType::ForwardedRoomKey: |
161 | return "m.forwarded_room_key" ; |
162 | case EventType::RoomKeyRequest: |
163 | return "m.room_key_request" ; |
164 | case EventType::RoomAliases: |
165 | return "m.room.aliases" ; |
166 | case EventType::RoomAvatar: |
167 | return "m.room.avatar" ; |
168 | case EventType::RoomCanonicalAlias: |
169 | return "m.room.canonical_alias" ; |
170 | case EventType::RoomCreate: |
171 | return "m.room.create" ; |
172 | case EventType::RoomEncrypted: |
173 | return "m.room.encrypted" ; |
174 | case EventType::Dummy: |
175 | return "m.dummy" ; |
176 | case EventType::RoomEncryption: |
177 | return "m.room.encryption" ; |
178 | case EventType::RoomGuestAccess: |
179 | return "m.room.guest_access" ; |
180 | case EventType::RoomHistoryVisibility: |
181 | return "m.room.history_visibility" ; |
182 | case EventType::RoomJoinRules: |
183 | return "m.room.join_rules" ; |
184 | case EventType::RoomMember: |
185 | return "m.room.member" ; |
186 | case EventType::RoomMessage: |
187 | return "m.room.message" ; |
188 | case EventType::RoomName: |
189 | return "m.room.name" ; |
190 | case EventType::RoomPowerLevels: |
191 | return "m.room.power_levels" ; |
192 | case EventType::RoomTopic: |
193 | return "m.room.topic" ; |
194 | case EventType::Widget: |
195 | return "m.widget" ; |
196 | case EventType::VectorWidget: |
197 | return "im.vector.modular.widgets" ; |
198 | case EventType::RoomRedaction: |
199 | return "m.room.redaction" ; |
200 | case EventType::RoomPinnedEvents: |
201 | return "m.room.pinned_events" ; |
202 | case EventType::RoomTombstone: |
203 | return "m.room.tombstone" ; |
204 | case EventType::Sticker: |
205 | return "m.sticker" ; |
206 | |
207 | case EventType::PolicyRuleUser: |
208 | return "m.policy.rule.user" ; |
209 | case EventType::PolicyRuleRoom: |
210 | return "m.policy.rule.room" ; |
211 | case EventType::PolicyRuleServer: |
212 | return "m.policy.rule.server" ; |
213 | |
214 | case EventType::SpaceChild: |
215 | return "m.space.child" ; |
216 | case EventType::SpaceParent: |
217 | return "m.space.parent" ; |
218 | |
219 | case EventType::Tag: |
220 | return "m.tag" ; |
221 | case EventType::Direct: |
222 | return "m.direct" ; |
223 | case EventType::Presence: |
224 | return "m.presence" ; |
225 | case EventType::PushRules: |
226 | return "m.push_rules" ; |
227 | case EventType::CallInvite: |
228 | return "m.call.invite" ; |
229 | case EventType::CallCandidates: |
230 | return "m.call.candidates" ; |
231 | case EventType::CallAnswer: |
232 | return "m.call.answer" ; |
233 | case EventType::CallHangUp: |
234 | return "m.call.hangup" ; |
235 | case EventType::CallSelectAnswer: |
236 | return "m.call.select_answer" ; |
237 | case EventType::CallReject: |
238 | return "m.call.reject" ; |
239 | case EventType::CallNegotiate: |
240 | return "m.call.negotiate" ; |
241 | case EventType::SecretRequest: |
242 | return "m.secret.request" ; |
243 | case EventType::SecretSend: |
244 | return "m.secret.send" ; |
245 | case EventType::Typing: |
246 | return "m.typing" ; |
247 | case EventType::Receipt: |
248 | return "m.receipt" ; |
249 | case EventType::FullyRead: |
250 | return "m.fully_read" ; |
251 | case EventType::NhekoHiddenEvents: |
252 | return "im.nheko.hidden_events" ; |
253 | case EventType::ImagePackInRoom: |
254 | return "im.ponies.room_emotes" ; |
255 | case EventType::ImagePackInAccountData: |
256 | return "im.ponies.user_emotes" ; |
257 | case EventType::ImagePackRooms: |
258 | return "im.ponies.emote_rooms" ; |
259 | case EventType::Unsupported: |
260 | return "" ; |
261 | } |
262 | |
263 | return "" ; |
264 | } |
265 | |
266 | EventType |
267 | getEventType(const json &obj) |
268 | { |
269 | if (obj.find(key: "type" ) != obj.end()) |
270 | return getEventType(type: obj.at(key: "type" ).get<std::string>()); |
271 | |
272 | return EventType::Unsupported; |
273 | } |
274 | |
275 | MessageType |
276 | getMessageType(const std::string &type) |
277 | { |
278 | if (type == "m.audio" ) |
279 | return MessageType::Audio; |
280 | else if (type == "m.emote" ) |
281 | return MessageType::Emote; |
282 | else if (type == "m.file" ) |
283 | return MessageType::File; |
284 | else if (type == "m.image" ) |
285 | return MessageType::Image; |
286 | else if (type == "m.location" ) |
287 | return MessageType::Location; |
288 | else if (type == "m.notice" ) |
289 | return MessageType::Notice; |
290 | else if (type == "m.text" ) |
291 | return MessageType::Text; |
292 | else if (type == "nic.custom.confetti" ) |
293 | return MessageType::Confetti; |
294 | else if (type == "m.video" ) |
295 | return MessageType::Video; |
296 | else if (type == "m.key.verification.request" ) |
297 | return MessageType::KeyVerificationRequest; |
298 | |
299 | return MessageType::Unknown; |
300 | } |
301 | |
302 | MessageType |
303 | getMessageType(const json &obj) |
304 | { |
305 | if (obj.is_null()) |
306 | return MessageType::Unknown; |
307 | |
308 | if (obj.find(key: "msgtype" ) == obj.end()) |
309 | return MessageType::Unknown; |
310 | |
311 | try { |
312 | return getMessageType(type: obj.at(key: "msgtype" ).get<std::string>()); |
313 | } catch (...) { |
314 | return MessageType::Unknown; |
315 | } |
316 | } |
317 | } |
318 | } |
319 | |