Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
XmppClient.h
Go to the documentation of this file.
1/* Copyright (C) 2021 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef XXXMPPCLIENT_H
19#define XXXMPPCLIENT_H
20
21#include "IXmppClient.h"
22
24
25#include <ctime>
26#include <deque>
27#include <string>
28#include <vector>
29
30class ScriptRequest;
31
32namespace glooxwrapper
33{
34 class Client;
35 struct CertInfo;
36}
37
39{
41
42private:
43 // Components
48
49 // Account infos
50 std::string m_username;
51 std::string m_password;
52 std::string m_server;
53 std::string m_room;
54 std::string m_nick;
55 std::string m_xpartamuppId;
56 std::string m_echelonId;
57
58 // Security
61
62 // State
63 gloox::CertStatus m_certStatus;
66
67public:
68 // Basic
69 XmppClient(const ScriptInterface* scriptInterface, const std::string& sUsername, const std::string& sPassword, const std::string& sRoom, const std::string& sNick, const int historyRequestSize = 0, const bool regOpt = false);
70 virtual ~XmppClient();
71
72 // JS::Heap is better for GC performance than JS::PersistentRooted
73 static void Trace(JSTracer *trc, void *data)
74 {
75 static_cast<XmppClient*>(data)->TraceMember(trc);
76 }
77
78 void TraceMember(JSTracer *trc);
79
80 // Network
81 void connect();
82 void disconnect();
83 bool isConnected();
84 void recv();
85 void SendIqGetBoardList();
86 void SendIqGetProfile(const std::string& player);
87 void SendIqGameReport(const ScriptRequest& rq, JS::HandleValue data);
88 void SendIqRegisterGame(const ScriptRequest& rq, JS::HandleValue data);
89 void SendIqGetConnectionData(const std::string& jid, const std::string& password, const std::string& clientSalt, bool localIP);
91 void SendIqChangeStateGame(const std::string& nbp, const std::string& players);
92 void SendIqLobbyAuth(const std::string& to, const std::string& token);
93 void SetNick(const std::string& nick);
94 std::string GetNick() const;
95 std::string GetJID() const;
96 void kick(const std::string& nick, const std::string& reason);
97 void ban(const std::string& nick, const std::string& reason);
98 void SetPresence(const std::string& presence);
99 const char* GetPresence(const std::string& nickname);
100 const char* GetRole(const std::string& nickname);
101 std::wstring GetRating(const std::string& nickname);
102 const std::wstring& GetSubject();
103
104 JS::Value GUIGetPlayerList(const ScriptRequest& rq);
105 JS::Value GUIGetGameList(const ScriptRequest& rq);
106 JS::Value GUIGetBoardList(const ScriptRequest& rq);
107 JS::Value GUIGetProfile(const ScriptRequest& rq);
108
109 void SendStunEndpointToHost(const std::string& ip, u16 port, const std::string& hostJID);
110
111 /**
112 * Convert gloox values to string or time.
113 */
114 static const char* GetPresenceString(const gloox::Presence::PresenceType presenceType);
115 static const char* GetRoleString(const gloox::MUCRoomRole role);
116 static std::string StanzaErrorToString(gloox::StanzaError err);
117 static std::string RegistrationResultToString(gloox::RegistrationResult res);
118 static std::string ConnectionErrorToString(gloox::ConnectionError err);
119 static std::string CertificateErrorToString(gloox::CertStatus status);
120 static std::time_t ComputeTimestamp(const glooxwrapper::Message& msg);
121
122protected:
123 /* Xmpp handlers */
124 /* MUC handlers */
126 virtual void handleMUCError(glooxwrapper::MUCRoom& room, gloox::StanzaError);
127 virtual void handleMUCMessage(glooxwrapper::MUCRoom& room, const glooxwrapper::Message& msg, bool priv);
128 virtual void handleMUCSubject(glooxwrapper::MUCRoom& room, const glooxwrapper::string& nick, const glooxwrapper::string& subject);
129 /* MUC handlers not supported by glooxwrapper */
130 // virtual bool handleMUCRoomCreation(glooxwrapper::MUCRoom*) {return false;}
131 // virtual void handleMUCInviteDecline(glooxwrapper::MUCRoom*, const glooxwrapper::JID&, const std::string&) {}
132 // virtual void handleMUCInfo(glooxwrapper::MUCRoom*, int, const std::string&, const glooxwrapper::DataForm*) {}
133 // virtual void handleMUCItems(glooxwrapper::MUCRoom*, const std::list<gloox::Disco::Item*, std::allocator<gloox::Disco::Item*> >&) {}
134
135 /* Log handler */
136 virtual void handleLog(gloox::LogLevel level, gloox::LogArea area, const std::string& message);
137
138 /* ConnectionListener handlers*/
139 virtual void onConnect();
140 virtual void onDisconnect(gloox::ConnectionError e);
141 virtual bool onTLSConnect(const glooxwrapper::CertInfo& info);
142
143 /* Iq Handlers */
144 virtual bool handleIq(const glooxwrapper::IQ& iq);
145 virtual void handleIqID(const glooxwrapper::IQ&, int) {}
146
147 /* Registration Handlers */
148 virtual void handleRegistrationFields(const glooxwrapper::JID& /*from*/, int fields, glooxwrapper::string instructions );
149 virtual void handleRegistrationResult(const glooxwrapper::JID& /*from*/, gloox::RegistrationResult result);
150 virtual void handleAlreadyRegistered(const glooxwrapper::JID& /*from*/);
151 virtual void handleDataForm(const glooxwrapper::JID& /*from*/, const glooxwrapper::DataForm& /*form*/);
152 virtual void handleOOB(const glooxwrapper::JID& /*from*/, const glooxwrapper::OOB& oob);
153
154 /* Message Handler */
155 virtual void handleMessage(const glooxwrapper::Message& msg, glooxwrapper::MessageSession* session);
156
157 /* Session Handler */
158 virtual void handleSessionAction(gloox::Jingle::Action action, glooxwrapper::Jingle::Session& session, const glooxwrapper::Jingle::Session::Jingle& jingle);
160
161public:
162 JS::Value GuiPollNewMessages(const ScriptInterface& guiInterface);
163 JS::Value GuiPollHistoricMessages(const ScriptInterface& guiInterface);
165 void SendMUCMessage(const std::string& message);
166
167protected:
168 template<typename... Args>
169 void CreateGUIMessage(
170 const std::string& type,
171 const std::string& level,
172 const std::time_t time,
173 Args const&... args);
174
175private:
176 struct SPlayer {
177 SPlayer(const gloox::Presence::PresenceType presence, const gloox::MUCRoomRole role, const glooxwrapper::string& rating)
178 : m_Presence(presence), m_Role(role), m_Rating(rating)
179 {
180 }
181 gloox::Presence::PresenceType m_Presence;
182 gloox::MUCRoomRole m_Role;
184 };
185 using PlayerMap = std::map<glooxwrapper::string, SPlayer>;
186
187 /// Map of players
189 /// Whether or not the playermap has changed since the last time the GUI checked.
191 /// List of games
192 std::vector<const glooxwrapper::Tag*> m_GameList;
193 /// List of rankings
194 std::vector<const glooxwrapper::Tag*> m_BoardList;
195 /// Profile data
196 std::vector<const glooxwrapper::Tag*> m_Profile;
197 /// ScriptInterface to root the values
199 /// Queue of messages for the GUI
200 std::deque<JS::Heap<JS::Value> > m_GuiMessageQueue;
201 /// Cache of all GUI messages received since the login
202 std::vector<JS::Heap<JS::Value> > m_HistoricGuiMessages;
203 /// Current room subject/topic.
204 std::wstring m_Subject;
205};
206
207#endif // XMPPCLIENT_H
Definition: IXmppClient.h:26
Abstraction around a SpiderMonkey JS::Realm.
Definition: ScriptInterface.h:72
Spidermonkey maintains some 'local' state via the JSContext* object.
Definition: ScriptRequest.h:60
Definition: XmppClient.h:39
void SendStunEndpointToHost(const std::string &ip, u16 port, const std::string &hostJID)
Definition: XmppClient.cpp:1504
virtual void handleMUCSubject(glooxwrapper::MUCRoom &room, const glooxwrapper::string &nick, const glooxwrapper::string &subject)
Update local cache when subject changes.
Definition: XmppClient.cpp:1172
std::map< glooxwrapper::string, SPlayer > PlayerMap
Definition: XmppClient.h:185
virtual void handleMUCMessage(glooxwrapper::MUCRoom &room, const glooxwrapper::Message &msg, bool priv)
Handle a room message.
Definition: XmppClient.cpp:802
virtual void handleMessage(const glooxwrapper::Message &msg, glooxwrapper::MessageSession *session)
Handle a private message.
Definition: XmppClient.cpp:817
std::wstring GetRating(const std::string &nickname)
Get the most recent received rating of the given nick.
Definition: XmppClient.cpp:1285
PlayerMap m_PlayerMap
Map of players.
Definition: XmppClient.h:188
std::vector< const glooxwrapper::Tag * > m_BoardList
List of rankings.
Definition: XmppClient.h:194
std::deque< JS::Heap< JS::Value > > m_GuiMessageQueue
Queue of messages for the GUI.
Definition: XmppClient.h:200
bool isConnected()
Definition: XmppClient.cpp:223
virtual void handleAlreadyRegistered(const glooxwrapper::JID &)
Definition: XmppClient.cpp:545
std::vector< const glooxwrapper::Tag * > m_GameList
List of games.
Definition: XmppClient.h:192
JS::Value GUIGetGameList(const ScriptRequest &rq)
Handle requests from the GUI for the list of all active games.
Definition: XmppClient.cpp:597
static const char * GetRoleString(const gloox::MUCRoomRole role)
Convert a gloox role type to an untranslated string literal to be used as an identifier by the script...
Definition: XmppClient.cpp:1350
glooxwrapper::SessionManager * m_sessionManager
Definition: XmppClient.h:47
virtual void handleMUCError(glooxwrapper::MUCRoom &room, gloox::StanzaError)
Handle MUC room errors.
Definition: XmppClient.cpp:323
void recv()
Definition: XmppClient.cpp:228
virtual void handleMUCParticipantPresence(glooxwrapper::MUCRoom &room, const glooxwrapper::MUCRoomParticipant, const glooxwrapper::Presence &)
Update local data when a user changes presence.
Definition: XmppClient.cpp:1053
virtual void handleLog(gloox::LogLevel level, gloox::LogArea area, const std::string &message)
Log (debug) Handler.
Definition: XmppClient.cpp:236
bool m_initialLoadComplete
Definition: XmppClient.h:64
std::string m_server
Definition: XmppClient.h:52
virtual void handleSessionAction(gloox::Jingle::Action action, glooxwrapper::Jingle::Session &session, const glooxwrapper::Jingle::Session::Jingle &jingle)
Definition: XmppClient.cpp:1513
glooxwrapper::Client * m_client
Definition: XmppClient.h:44
virtual void handleDataForm(const glooxwrapper::JID &, const glooxwrapper::DataForm &)
Definition: XmppClient.cpp:550
std::wstring m_Subject
Current room subject/topic.
Definition: XmppClient.h:204
void ban(const std::string &nick, const std::string &reason)
Ban a player from the current room.
Definition: XmppClient.cpp:1232
JS::Value GuiPollNewMessages(const ScriptInterface &guiInterface)
Definition: XmppClient.cpp:726
virtual bool onTLSConnect(const glooxwrapper::CertInfo &info)
Handle TLS connection.
Definition: XmppClient.cpp:299
bool m_isConnected
Definition: XmppClient.h:65
gloox::CertStatus m_certStatus
Definition: XmppClient.h:63
bool m_PlayerMapUpdate
Whether or not the playermap has changed since the last time the GUI checked.
Definition: XmppClient.h:190
void SendIqGetBoardList()
Request the leaderboard data from the server.
Definition: XmppClient.cpp:336
std::string m_password
Definition: XmppClient.h:51
void SendIqChangeStateGame(const std::string &nbp, const std::string &players)
Send a request to change the state of a registered game on the server.
Definition: XmppClient.cpp:486
std::string m_nick
Definition: XmppClient.h:54
virtual void handleIqID(const glooxwrapper::IQ &, int)
Definition: XmppClient.h:145
void TraceMember(JSTracer *trc)
Definition: XmppClient.cpp:202
const char * GetRole(const std::string &nickname)
Get the current xmpp role of the given nick.
Definition: XmppClient.cpp:1271
JS::Value GuiPollHistoricMessages(const ScriptInterface &guiInterface)
Definition: XmppClient.cpp:773
std::string GetNick() const
Get current nickname.
Definition: XmppClient.cpp:1205
void SendIqLobbyAuth(const std::string &to, const std::string &token)
Send lobby authentication token.
Definition: XmppClient.cpp:511
std::string m_echelonId
Definition: XmppClient.h:56
void SendMUCMessage(const std::string &message)
Send a standard MUC textual message.
Definition: XmppClient.cpp:794
std::string m_connectionDataJid
Definition: XmppClient.h:59
virtual void handleRegistrationFields(const glooxwrapper::JID &, int fields, glooxwrapper::string instructions)
Definition: XmppClient.cpp:527
void kick(const std::string &nick, const std::string &reason)
Kick a player from the current room.
Definition: XmppClient.cpp:1221
std::string m_connectionDataIqId
Definition: XmppClient.h:60
void SendIqRegisterGame(const ScriptRequest &rq, JS::HandleValue data)
Send a request to register a game to the server.
Definition: XmppClient.cpp:422
virtual void handleSessionInitiation(glooxwrapper::Jingle::Session &session, const glooxwrapper::Jingle::Session::Jingle &jingle)
Definition: XmppClient.cpp:1519
NONCOPYABLE(XmppClient)
virtual void onDisconnect(gloox::ConnectionError e)
Handle disconnection.
Definition: XmppClient.cpp:264
virtual void onConnect()
Handle connection.
Definition: XmppClient.cpp:248
static std::string RegistrationResultToString(gloox::RegistrationResult res)
Convert a gloox registration result enum to string Keep in sync with Gloox documentation.
Definition: XmppClient.cpp:1482
void CreateGUIMessage(const std::string &type, const std::string &level, const std::time_t time, Args const &... args)
Definition: XmppClient.cpp:690
virtual void handleRegistrationResult(const glooxwrapper::JID &, gloox::RegistrationResult result)
Definition: XmppClient.cpp:535
void SetPresence(const std::string &presence)
Change the xmpp presence of the client.
Definition: XmppClient.cpp:1242
const char * GetPresence(const std::string &nickname)
Get the current xmpp presence of the given nick.
Definition: XmppClient.cpp:1258
std::vector< JS::Heap< JS::Value > > m_HistoricGuiMessages
Cache of all GUI messages received since the login.
Definition: XmppClient.h:202
std::string m_xpartamuppId
Definition: XmppClient.h:55
void SendIqUnregisterGame()
Send a request to unregister a game to the server.
Definition: XmppClient.cpp:464
void SendIqGetConnectionData(const std::string &jid, const std::string &password, const std::string &clientSalt, bool localIP)
Request the Connection data (ip, port...) from the server.
Definition: XmppClient.cpp:368
virtual void handleOOB(const glooxwrapper::JID &, const glooxwrapper::OOB &oob)
Definition: XmppClient.cpp:555
virtual bool handleIq(const glooxwrapper::IQ &iq)
Handle portions of messages containing custom stanza extensions.
Definition: XmppClient.cpp:834
std::string m_room
Definition: XmppClient.h:53
std::vector< const glooxwrapper::Tag * > m_Profile
Profile data.
Definition: XmppClient.h:196
static std::string ConnectionErrorToString(gloox::ConnectionError err)
Convert a gloox connection error enum to string Keep in sync with Gloox documentation.
Definition: XmppClient.cpp:1443
static const char * GetPresenceString(const gloox::Presence::PresenceType presenceType)
Convert gloox values to string or time.
Definition: XmppClient.cpp:1326
glooxwrapper::MUCRoom * m_mucRoom
Definition: XmppClient.h:45
const std::wstring & GetSubject()
Get current subject.
Definition: XmppClient.cpp:1187
static std::string CertificateErrorToString(gloox::CertStatus status)
Translates a gloox certificate error codes, i.e.
Definition: XmppClient.cpp:1371
JS::Value GUIGetProfile(const ScriptRequest &rq)
Handle requests from the GUI for profile data.
Definition: XmppClient.cpp:651
JS::Value GUIGetBoardList(const ScriptRequest &rq)
Handle requests from the GUI for leaderboard data.
Definition: XmppClient.cpp:625
static void Trace(JSTracer *trc, void *data)
Definition: XmppClient.h:73
void SetNick(const std::string &nick)
Request nick change, real change via mucRoomHandler.
Definition: XmppClient.cpp:1197
glooxwrapper::Registration * m_registration
Definition: XmppClient.h:46
XmppClient(const ScriptInterface *scriptInterface, const std::string &sUsername, const std::string &sPassword, const std::string &sRoom, const std::string &sNick, const int historyRequestSize=0, const bool regOpt=false)
Construct the XMPP client.
Definition: XmppClient.cpp:79
void SendIqGameReport(const ScriptRequest &rq, JS::HandleValue data)
Send game report containing numerous game properties to the server.
Definition: XmppClient.cpp:389
virtual ~XmppClient()
Destroy the xmpp client.
Definition: XmppClient.cpp:179
void disconnect()
Definition: XmppClient.cpp:218
void connect()
Network.
Definition: XmppClient.cpp:212
JS::Value GUIGetPlayerList(const ScriptRequest &rq)
Handle requests from the GUI for the list of players.
Definition: XmppClient.cpp:569
std::string GetJID() const
Definition: XmppClient.cpp:1210
static std::time_t ComputeTimestamp(const glooxwrapper::Message &msg)
Parse and return the timestamp of a historic chat message and return the current time for new chat me...
Definition: XmppClient.cpp:1306
void SendIqGetProfile(const std::string &player)
Request the profile data from the server.
Definition: XmppClient.cpp:352
bool GuiPollHasPlayerListUpdate()
Definition: XmppClient.cpp:714
std::string m_username
Definition: XmppClient.h:50
static std::string StanzaErrorToString(gloox::StanzaError err)
Convert a gloox stanza error type to string.
Definition: XmppClient.cpp:1399
const ScriptInterface * m_ScriptInterface
ScriptInterface to root the values.
Definition: XmppClient.h:198
Definition: glooxwrapper.h:406
Definition: glooxwrapper.h:345
Definition: glooxwrapper.h:465
Definition: glooxwrapper.h:354
Definition: glooxwrapper.h:495
Definition: glooxwrapper.h:682
Definition: glooxwrapper.h:661
Definition: glooxwrapper.h:654
Definition: glooxwrapper.h:369
Definition: glooxwrapper.h:534
Definition: glooxwrapper.h:362
Definition: glooxwrapper.h:515
Definition: glooxwrapper.h:558
Definition: glooxwrapper.h:379
Definition: glooxwrapper.h:566
Definition: glooxwrapper.h:691
Definition: glooxwrapper.h:120
Definition: glooxwrapper.cpp:46
Definition: XmppClient.h:176
SPlayer(const gloox::Presence::PresenceType presence, const gloox::MUCRoomRole role, const glooxwrapper::string &rating)
Definition: XmppClient.h:177
glooxwrapper::string m_Rating
Definition: XmppClient.h:183
gloox::MUCRoomRole m_Role
Definition: XmppClient.h:182
gloox::Presence::PresenceType m_Presence
Definition: XmppClient.h:181
Definition: glooxwrapper.h:294
Definition: glooxwrapper.h:330
uint16_t u16
Definition: types.h:38