Fix wrong forest type replacement in the prior commit (rP20301).
[0ad.git] / source / lobby / XmppClient.h
blobfbe5f6e5acfd743c42073dc896f5c06593aa96ed
1 /* Copyright (C) 2017 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 <deque>
25 #include "glooxwrapper/glooxwrapper.h"
26 #include "scriptinterface/ScriptVal.h"
28 class ScriptInterface;
30 namespace glooxwrapper
32 class Client;
33 struct CertInfo;
36 class XmppClient : public IXmppClient, public glooxwrapper::ConnectionListener, public glooxwrapper::MUCRoomHandler, public glooxwrapper::IqHandler, public glooxwrapper::RegistrationHandler, public glooxwrapper::MessageHandler, public glooxwrapper::Jingle::SessionHandler
38 NONCOPYABLE(XmppClient);
40 private:
41 // Components
42 glooxwrapper::Client* m_client;
43 glooxwrapper::MUCRoom* m_mucRoom;
44 glooxwrapper::Registration* m_registration;
45 glooxwrapper::SessionManager* m_sessionManager;
47 // Account infos
48 std::string m_username;
49 std::string m_password;
50 std::string m_nick;
51 std::string m_xpartamuppId;
53 // State
54 bool m_initialLoadComplete;
56 public:
57 // Basic
58 XmppClient(const std::string& sUsername, const std::string& sPassword, const std::string& sRoom, const std::string& sNick, const int historyRequestSize = 0, const bool regOpt = false);
59 virtual ~XmppClient();
61 // Network
62 void connect();
63 void disconnect();
64 void recv();
65 void SendIqGetBoardList();
66 void SendIqGetProfile(const std::string& player);
67 void SendIqGameReport(const ScriptInterface& scriptInterface, JS::HandleValue data);
68 void SendIqRegisterGame(const ScriptInterface& scriptInterface, JS::HandleValue data);
69 void SendIqUnregisterGame();
70 void SendIqChangeStateGame(const std::string& nbp, const std::string& players);
71 void SetNick(const std::string& nick);
72 void GetNick(std::string& nick);
73 void kick(const std::string& nick, const std::string& reason);
74 void ban(const std::string& nick, const std::string& reason);
75 void SetPresence(const std::string& presence);
76 void GetPresence(const std::string& nickname, std::string& presence);
77 void GetRole(const std::string& nickname, std::string& role);
78 void GetSubject(std::string& subject);
80 void GUIGetPlayerList(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret);
81 void GUIGetGameList(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret);
82 void GUIGetBoardList(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret);
83 void GUIGetProfile(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret);
85 void SendStunEndpointToHost(StunClient::StunEndpoint* stunEndpoint, const std::string& hostJID);
87 protected:
88 /* Xmpp handlers */
89 /* MUC handlers */
90 virtual void handleMUCParticipantPresence(glooxwrapper::MUCRoom*, const glooxwrapper::MUCRoomParticipant, const glooxwrapper::Presence&);
91 virtual void handleMUCError(glooxwrapper::MUCRoom*, gloox::StanzaError);
92 virtual void handleMUCMessage(glooxwrapper::MUCRoom* room, const glooxwrapper::Message& msg, bool priv);
93 virtual void handleMUCSubject(glooxwrapper::MUCRoom*, const glooxwrapper::string& nick, const glooxwrapper::string& subject);
94 /* MUC handlers not supported by glooxwrapper */
95 // virtual bool handleMUCRoomCreation(glooxwrapper::MUCRoom*) {return false;}
96 // virtual void handleMUCInviteDecline(glooxwrapper::MUCRoom*, const glooxwrapper::JID&, const std::string&) {}
97 // virtual void handleMUCInfo(glooxwrapper::MUCRoom*, int, const std::string&, const glooxwrapper::DataForm*) {}
98 // virtual void handleMUCItems(glooxwrapper::MUCRoom*, const std::list<gloox::Disco::Item*, std::allocator<gloox::Disco::Item*> >&) {}
100 /* Log handler */
101 virtual void handleLog(gloox::LogLevel level, gloox::LogArea area, const std::string& message);
103 /* ConnectionListener handlers*/
104 virtual void onConnect();
105 virtual void onDisconnect(gloox::ConnectionError e);
106 virtual bool onTLSConnect(const glooxwrapper::CertInfo& info);
108 /* Iq Handlers */
109 virtual bool handleIq(const glooxwrapper::IQ& iq);
110 virtual void handleIqID(const glooxwrapper::IQ&, int) {}
112 /* Registration Handlers */
113 virtual void handleRegistrationFields(const glooxwrapper::JID& /*from*/, int fields, glooxwrapper::string instructions );
114 virtual void handleRegistrationResult(const glooxwrapper::JID& /*from*/, gloox::RegistrationResult result);
115 virtual void handleAlreadyRegistered(const glooxwrapper::JID& /*from*/);
116 virtual void handleDataForm(const glooxwrapper::JID& /*from*/, const glooxwrapper::DataForm& /*form*/);
117 virtual void handleOOB(const glooxwrapper::JID& /*from*/, const glooxwrapper::OOB& oob);
119 /* Message Handler */
120 virtual void handleMessage(const glooxwrapper::Message& msg, glooxwrapper::MessageSession* session);
122 /* Session Handler */
123 virtual void handleSessionAction(gloox::Jingle::Action action, glooxwrapper::Jingle::Session* UNUSED(session), const glooxwrapper::Jingle::Session::Jingle* jingle);
124 virtual void handleSessionInitiation(const glooxwrapper::Jingle::Session::Jingle* jingle);
126 // Helpers
127 void GetPresenceString(const gloox::Presence::PresenceType p, std::string& presence) const;
128 void GetRoleString(const gloox::MUCRoomRole r, std::string& role) const;
129 std::string StanzaErrorToString(gloox::StanzaError err) const;
130 std::string ConnectionErrorToString(gloox::ConnectionError err) const;
131 std::string RegistrationResultToString(gloox::RegistrationResult res) const;
132 std::time_t ComputeTimestamp(const glooxwrapper::Message& msg) const;
134 public:
135 /* Messages */
136 struct GUIMessage
138 std::string type;
139 std::string level;
140 std::string property1_name;
141 std::string property1_value;
142 std::string property2_name;
143 std::string property2_value;
144 std::time_t time;
146 JS::Value GuiMessageToJSVal(const ScriptInterface& scriptInterface, const GUIMessage& message, const bool historic);
147 JS::Value GuiPollNewMessage(const ScriptInterface& scriptInterface);
148 JS::Value GuiPollHistoricMessages(const ScriptInterface& scriptInterface);
149 void SendMUCMessage(const std::string& message);
150 void ClearPresenceUpdates();
151 protected:
152 void CreateGUIMessage(
153 const std::string& type,
154 const std::string& level = "",
155 const std::string& property1_name = "",
156 const std::string& property1_value = "",
157 const std::string& property2_name = "",
158 const std::string& property2_value = "",
159 const std::time_t time = std::time(nullptr));
161 private:
162 /// Map of players
163 std::map<std::string, std::vector<std::string> > m_PlayerMap;
164 /// List of games
165 std::vector<const glooxwrapper::Tag*> m_GameList;
166 /// List of rankings
167 std::vector<const glooxwrapper::Tag*> m_BoardList;
168 /// Profile data
169 std::vector<const glooxwrapper::Tag*> m_Profile;
170 /// Queue of messages for the GUI
171 std::deque<GUIMessage> m_GuiMessageQueue;
172 /// Cache of all GUI messages received since the login
173 std::vector<GUIMessage> m_HistoricGuiMessages;
174 /// Current room subject/topic.
175 std::string m_Subject;
178 #endif // XMPPCLIENT_H