1 // Copyright 2005-2019 The Mumble Developers. All rights reserved.
2 // Use of this source code is governed by a BSD-style license
3 // that can be found in the LICENSE file at the root of the
4 // Mumble source tree or at <https://www.mumble.info/LICENSE>.
5 
6 #ifndef MUMBLE_MESSAGE_H_
7 #define MUMBLE_MESSAGE_H_
8 
9 #include <string>
10 #include <QtCore/QCryptographicHash>
11 #include <QtCore/QString>
12 
13 /**
14   Protobuf packet type enumeration for message handler generation.
15 
16   Warning: Only append to the end.
17  */
18 #define MUMBLE_MH_ALL \
19 	MUMBLE_MH_MSG(Version) \
20 	MUMBLE_MH_MSG(UDPTunnel) \
21 	MUMBLE_MH_MSG(Authenticate) \
22 	MUMBLE_MH_MSG(Ping) \
23 	MUMBLE_MH_MSG(Reject) \
24 	MUMBLE_MH_MSG(ServerSync) \
25 	MUMBLE_MH_MSG(ChannelRemove) \
26 	MUMBLE_MH_MSG(ChannelState) \
27 	MUMBLE_MH_MSG(UserRemove) \
28 	MUMBLE_MH_MSG(UserState) \
29 	MUMBLE_MH_MSG(BanList) \
30 	MUMBLE_MH_MSG(TextMessage) \
31 	MUMBLE_MH_MSG(PermissionDenied) \
32 	MUMBLE_MH_MSG(ACL) \
33 	MUMBLE_MH_MSG(QueryUsers) \
34 	MUMBLE_MH_MSG(CryptSetup) \
35 	MUMBLE_MH_MSG(ContextActionModify) \
36 	MUMBLE_MH_MSG(ContextAction) \
37 	MUMBLE_MH_MSG(UserList) \
38 	MUMBLE_MH_MSG(VoiceTarget) \
39 	MUMBLE_MH_MSG(PermissionQuery) \
40 	MUMBLE_MH_MSG(CodecVersion) \
41 	MUMBLE_MH_MSG(UserStats) \
42 	MUMBLE_MH_MSG(RequestBlob) \
43 	MUMBLE_MH_MSG(ServerConfig) \
44 	MUMBLE_MH_MSG(SuggestConfig)
45 
46 class MessageHandler {
47 	public:
48 		enum UDPMessageType { UDPVoiceCELTAlpha, UDPPing, UDPVoiceSpeex, UDPVoiceCELTBeta, UDPVoiceOpus };
49 
50 #define MUMBLE_MH_MSG(x) x,
51 		enum MessageType {
52 			MUMBLE_MH_ALL
53 		};
54 #undef MUMBLE_MH_MSG
55 };
56 
57 /// UDPMessageTypeIsValidVoicePacket checks whether the given
58 /// UDPMessageType is a valid voice packet.
UDPMessageTypeIsValidVoicePacket(MessageHandler::UDPMessageType umt)59 inline bool UDPMessageTypeIsValidVoicePacket(MessageHandler::UDPMessageType umt) {
60 	switch (umt) {
61 		case MessageHandler::UDPVoiceCELTAlpha:
62 		case MessageHandler::UDPVoiceSpeex:
63 		case MessageHandler::UDPVoiceCELTBeta:
64 		case MessageHandler::UDPVoiceOpus:
65 			return true;
66 		case MessageHandler::UDPPing:
67 			return false;
68 	}
69 	return false;
70 }
71 
u8(const::std::string & str)72 inline QString u8(const ::std::string &str) {
73 	return QString::fromUtf8(str.data(), static_cast<int>(str.length()));
74 }
75 
u8(const::std::wstring & str)76 inline QString u8(const ::std::wstring &str) {
77 	return QString::fromStdWString(str);
78 }
79 
u8(const QString & str)80 inline ::std::string u8(const QString &str) {
81 	const QByteArray &qba = str.toUtf8();
82 	return ::std::string(qba.constData(), qba.length());
83 }
84 
blob(const::std::string & str)85 inline QByteArray blob(const ::std::string &str) {
86 	return QByteArray(str.data(), static_cast<int>(str.length()));
87 }
88 
blob(const QByteArray & str)89 inline ::std::string blob(const QByteArray &str) {
90 	return ::std::string(str.constData(), str.length());
91 }
92 
sha1(const QByteArray & blob)93 inline QByteArray sha1(const QByteArray &blob) {
94 	return QCryptographicHash::hash(blob, QCryptographicHash::Sha1);
95 }
96 
sha1(const QString & str)97 inline QByteArray sha1(const QString &str) {
98 	return QCryptographicHash::hash(str.toUtf8(), QCryptographicHash::Sha1);
99 }
100 
101 #endif
102