1 /* Copyright (C) 2021 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #ifndef XXXMPPCLIENT_H
19 #define XXXMPPCLIENT_H
21 #include "IXmppClient.h"
23 #include "glooxwrapper/glooxwrapper.h"
32 namespace glooxwrapper
38 class XmppClient
: public IXmppClient
, public glooxwrapper::ConnectionListener
, public glooxwrapper::MUCRoomHandler
, public glooxwrapper::IqHandler
, public glooxwrapper::RegistrationHandler
, public glooxwrapper::MessageHandler
, public glooxwrapper::Jingle::SessionHandler
40 NONCOPYABLE(XmppClient
);
44 glooxwrapper::Client
* m_client
;
45 glooxwrapper::MUCRoom
* m_mucRoom
;
46 glooxwrapper::Registration
* m_registration
;
47 glooxwrapper::SessionManager
* m_sessionManager
;
50 std::string m_username
;
51 std::string m_password
;
55 std::string m_xpartamuppId
;
56 std::string m_echelonId
;
59 std::string m_connectionDataJid
;
60 std::string m_connectionDataIqId
;
63 gloox::CertStatus m_certStatus
;
64 bool m_initialLoadComplete
;
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();
72 // JS::Heap is better for GC performance than JS::PersistentRooted
73 static void Trace(JSTracer
*trc
, void *data
)
75 static_cast<XmppClient
*>(data
)->TraceMember(trc
);
78 void TraceMember(JSTracer
*trc
);
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
);
90 void SendIqUnregisterGame();
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();
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
);
109 void SendStunEndpointToHost(const std::string
& ip
, u16 port
, const std::string
& hostJID
);
112 * Convert gloox values to string or time.
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
);
125 virtual void handleMUCParticipantPresence(glooxwrapper::MUCRoom
& room
, const glooxwrapper::MUCRoomParticipant
, const glooxwrapper::Presence
&);
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*> >&) {}
136 virtual void handleLog(gloox::LogLevel level
, gloox::LogArea area
, const std::string
& message
);
138 /* ConnectionListener handlers*/
139 virtual void onConnect();
140 virtual void onDisconnect(gloox::ConnectionError e
);
141 virtual bool onTLSConnect(const glooxwrapper::CertInfo
& info
);
144 virtual bool handleIq(const glooxwrapper::IQ
& iq
);
145 virtual void handleIqID(const glooxwrapper::IQ
&, int) {}
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
);
154 /* Message Handler */
155 virtual void handleMessage(const glooxwrapper::Message
& msg
, glooxwrapper::MessageSession
* session
);
157 /* Session Handler */
158 virtual void handleSessionAction(gloox::Jingle::Action action
, glooxwrapper::Jingle::Session
& session
, const glooxwrapper::Jingle::Session::Jingle
& jingle
);
159 virtual void handleSessionInitiation(glooxwrapper::Jingle::Session
& session
, const glooxwrapper::Jingle::Session::Jingle
& jingle
);
162 JS::Value
GuiPollNewMessages(const ScriptInterface
& guiInterface
);
163 JS::Value
GuiPollHistoricMessages(const ScriptInterface
& guiInterface
);
164 bool GuiPollHasPlayerListUpdate();
165 void SendMUCMessage(const std::string
& message
);
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
);
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
)
181 gloox::Presence::PresenceType m_Presence
;
182 gloox::MUCRoomRole m_Role
;
183 glooxwrapper::string m_Rating
;
185 using PlayerMap
= std::map
<glooxwrapper::string
, SPlayer
>;
188 PlayerMap m_PlayerMap
;
189 /// Whether or not the playermap has changed since the last time the GUI checked.
190 bool m_PlayerMapUpdate
;
192 std::vector
<const glooxwrapper::Tag
*> m_GameList
;
194 std::vector
<const glooxwrapper::Tag
*> m_BoardList
;
196 std::vector
<const glooxwrapper::Tag
*> m_Profile
;
197 /// ScriptInterface to root the values
198 const ScriptInterface
* m_ScriptInterface
;
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
;
207 #endif // XMPPCLIENT_H