Pyrogenesis  trunk
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 
30 class ScriptRequest;
31 
32 namespace glooxwrapper
33 {
34  class Client;
35  struct CertInfo;
36 }
37 
39 {
41 
42 private:
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
59  std::string m_connectionDataJid;
60  std::string m_connectionDataIqId;
61 
62  // State
63  gloox::CertStatus m_certStatus;
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  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);
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  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  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
#define NONCOPYABLE(className)
Indicates that a class is noncopyable (usually due to const or reference members, or because the clas...
Definition: code_annotation.h:227
glooxwrapper::SessionManager * m_sessionManager
Definition: XmppClient.h:47
Definition: glooxwrapper.h:376
bool m_initialLoadComplete
Definition: XmppClient.h:64
static void Trace(JSTracer *trc, void *data)
Definition: XmppClient.h:73
std::string m_username
Definition: XmppClient.h:50
gloox::Presence::PresenceType m_Presence
Definition: XmppClient.h:181
bool m_isConnected
Definition: XmppClient.h:65
gloox::CertStatus m_certStatus
Definition: XmppClient.h:63
uint16_t u16
Definition: types.h:38
std::string m_connectionDataIqId
Definition: XmppClient.h:60
bool(void *pContext, const CFsmEvent *pEvent) Action
Definition: FSM.h:34
Definition: glooxwrapper.h:327
JS::Value GuiPollNewMessages(const ScriptInterface &scriptInterface)
Definition: JSInterface_Lobby.cpp:133
Definition: glooxwrapper.h:658
Definition: glooxwrapper.h:531
Definition: glooxwrapper.h:492
Definition: glooxwrapper.h:555
Definition: IXmppClient.h:25
Definition: glooxwrapper.h:512
Definition: glooxwrapper.h:563
std::string m_server
Definition: XmppClient.h:52
glooxwrapper::Registration * m_registration
Definition: XmppClient.h:46
glooxwrapper::Client * m_client
Definition: XmppClient.h:44
std::string m_echelonId
Definition: XmppClient.h:56
Config::Value_type Value
Definition: json_spirit_value.h:182
Definition: glooxwrapper.h:679
Definition: glooxwrapper.h:688
Definition: glooxwrapper.h:359
std::string m_connectionDataJid
Definition: XmppClient.h:59
Definition: glooxwrapper.h:403
std::string m_password
Definition: XmppClient.h:51
Definition: glooxwrapper.h:366
const ScriptInterface * m_ScriptInterface
ScriptInterface to root the values.
Definition: XmppClient.h:198
std::vector< const glooxwrapper::Tag * > m_BoardList
List of rankings.
Definition: XmppClient.h:194
Definition: glooxwrapper.h:119
std::vector< JS::Heap< JS::Value > > m_HistoricGuiMessages
Cache of all GUI messages received since the login.
Definition: XmppClient.h:202
std::vector< const glooxwrapper::Tag * > m_GameList
List of games.
Definition: XmppClient.h:192
Definition: XmppClient.h:38
std::string m_xpartamuppId
Definition: XmppClient.h:55
std::map< glooxwrapper::string, SPlayer > PlayerMap
Definition: XmppClient.h:185
std::string m_nick
Definition: XmppClient.h:54
Definition: glooxwrapper.h:293
Definition: glooxwrapper.h:651
bool m_PlayerMapUpdate
Whether or not the playermap has changed since the last time the GUI checked.
Definition: XmppClient.h:190
PlayerMap m_PlayerMap
Map of players.
Definition: XmppClient.h:188
gloox::MUCRoomRole m_Role
Definition: XmppClient.h:182
std::wstring m_Subject
Current room subject/topic.
Definition: XmppClient.h:204
std::vector< const glooxwrapper::Tag * > m_Profile
Profile data.
Definition: XmppClient.h:196
Definition: XmppClient.h:176
Definition: glooxwrapper.h:351
std::deque< JS::Heap< JS::Value > > m_GuiMessageQueue
Queue of messages for the GUI.
Definition: XmppClient.h:200
virtual void handleIqID(const glooxwrapper::IQ &, int)
Definition: XmppClient.h:145
Abstraction around a SpiderMonkey JS::Realm.
Definition: ScriptInterface.h:71
Definition: glooxwrapper.h:462
Definition: glooxwrapper.h:342
Definition: glooxwrapper.cpp:45
glooxwrapper::string m_Rating
Definition: XmppClient.h:183
Spidermonkey maintains some &#39;local&#39; state via the JSContext* object.
Definition: ScriptRequest.h:59
glooxwrapper::MUCRoom * m_mucRoom
Definition: XmppClient.h:45
std::string m_room
Definition: XmppClient.h:53
SPlayer(const gloox::Presence::PresenceType presence, const gloox::MUCRoomRole role, const glooxwrapper::string &rating)
Definition: XmppClient.h:177