Line data Source code
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 :
23 : #include "glooxwrapper/glooxwrapper.h"
24 :
25 : #include <ctime>
26 : #include <deque>
27 : #include <string>
28 : #include <vector>
29 :
30 : class ScriptRequest;
31 :
32 : namespace glooxwrapper
33 : {
34 : class Client;
35 : struct CertInfo;
36 : }
37 :
38 : class XmppClient : public IXmppClient, public glooxwrapper::ConnectionListener, public glooxwrapper::MUCRoomHandler, public glooxwrapper::IqHandler, public glooxwrapper::RegistrationHandler, public glooxwrapper::MessageHandler, public glooxwrapper::Jingle::SessionHandler
39 : {
40 : NONCOPYABLE(XmppClient);
41 :
42 : private:
43 : // Components
44 : glooxwrapper::Client* m_client;
45 : glooxwrapper::MUCRoom* m_mucRoom;
46 : glooxwrapper::Registration* m_registration;
47 : glooxwrapper::SessionManager* m_sessionManager;
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
59 : std::string m_connectionDataJid;
60 : std::string m_connectionDataIqId;
61 :
62 : // State
63 : gloox::CertStatus m_certStatus;
64 : bool m_initialLoadComplete;
65 : bool m_isConnected;
66 :
67 : public:
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 0 : static void Trace(JSTracer *trc, void *data)
74 : {
75 0 : static_cast<XmppClient*>(data)->TraceMember(trc);
76 0 : }
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);
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();
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 :
122 : protected:
123 : /* Xmpp handlers */
124 : /* MUC handlers */
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*> >&) {}
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 0 : 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);
159 : virtual void handleSessionInitiation(glooxwrapper::Jingle::Session& session, const glooxwrapper::Jingle::Session::Jingle& jingle);
160 :
161 : public:
162 : JS::Value GuiPollNewMessages(const ScriptInterface& guiInterface);
163 : JS::Value GuiPollHistoricMessages(const ScriptInterface& guiInterface);
164 : bool GuiPollHasPlayerListUpdate();
165 : void SendMUCMessage(const std::string& message);
166 :
167 : protected:
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 :
175 : private:
176 0 : struct SPlayer {
177 0 : SPlayer(const gloox::Presence::PresenceType presence, const gloox::MUCRoomRole role, const glooxwrapper::string& rating)
178 0 : : m_Presence(presence), m_Role(role), m_Rating(rating)
179 : {
180 0 : }
181 : gloox::Presence::PresenceType m_Presence;
182 : gloox::MUCRoomRole m_Role;
183 : glooxwrapper::string m_Rating;
184 : };
185 : using PlayerMap = std::map<glooxwrapper::string, SPlayer>;
186 :
187 : /// Map of players
188 : PlayerMap m_PlayerMap;
189 : /// Whether or not the playermap has changed since the last time the GUI checked.
190 : bool m_PlayerMapUpdate;
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
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;
205 : };
206 :
207 : #endif // XMPPCLIENT_H
|