Pyrogenesis  trunk
XmppClient.h
Go to the documentation of this file.
1 /* Copyright (C) 2016 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 <deque>
24 
27 
28 class ScriptInterface;
29 
30 namespace glooxwrapper
31 {
32  class Client;
33  struct CertInfo;
34 }
35 
37 {
39 
40 private:
41  // Components
45 
46  // Account infos
47  std::string m_username;
48  std::string m_password;
49  std::string m_nick;
50  std::string m_xpartamuppId;
51 
52  // State
54 
55 public:
56  // Basic
57  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);
58  virtual ~XmppClient();
59 
60  // Network
61  void connect();
62  void disconnect();
63  void recv();
64  void SendIqGetGameList();
65  void SendIqGetBoardList();
66  void SendIqGetRatingList();
67  void SendIqGetProfile(const std::string& player);
68  void SendIqGameReport(ScriptInterface& scriptInterface, JS::HandleValue data);
69  void SendIqRegisterGame(ScriptInterface& scriptInterface, JS::HandleValue data);
70  void SendIqUnregisterGame();
71  void SendIqChangeStateGame(const std::string& nbp, const std::string& players);
72  void SetNick(const std::string& nick);
73  void GetNick(std::string& nick);
74  void kick(const std::string& nick, const std::string& reason);
75  void ban(const std::string& nick, const std::string& reason);
76  void SetPresence(const std::string& presence);
77  void GetPresence(const std::string& nickname, std::string& presence);
78  void GetRole(const std::string& nickname, std::string& role);
79  void GetSubject(std::string& subject);
80 
81  void GUIGetPlayerList(ScriptInterface& scriptInterface, JS::MutableHandleValue ret);
82  void GUIGetGameList(ScriptInterface& scriptInterface, JS::MutableHandleValue ret);
83  void GUIGetBoardList(ScriptInterface& scriptInterface, JS::MutableHandleValue ret);
84  void GUIGetProfile(ScriptInterface& scriptInterface, JS::MutableHandleValue ret);
85 
86  //Script
87  ScriptInterface& GetScriptInterface();
88 
89 protected:
90  /* Xmpp handlers */
91  /* MUC handlers */
92  virtual void handleMUCParticipantPresence(glooxwrapper::MUCRoom*, const glooxwrapper::MUCRoomParticipant, const glooxwrapper::Presence&);
93  virtual void handleMUCError(glooxwrapper::MUCRoom*, gloox::StanzaError);
94  virtual void handleMUCMessage(glooxwrapper::MUCRoom* room, const glooxwrapper::Message& msg, bool priv);
95  virtual void handleMUCSubject(glooxwrapper::MUCRoom*, const glooxwrapper::string& nick, const glooxwrapper::string& subject);
96  /* MUC handlers not supported by glooxwrapper */
97  // virtual bool handleMUCRoomCreation(glooxwrapper::MUCRoom*) {return false;}
98  // virtual void handleMUCInviteDecline(glooxwrapper::MUCRoom*, const glooxwrapper::JID&, const std::string&) {}
99  // virtual void handleMUCInfo(glooxwrapper::MUCRoom*, int, const std::string&, const glooxwrapper::DataForm*) {}
100  // virtual void handleMUCItems(glooxwrapper::MUCRoom*, const std::list<gloox::Disco::Item*, std::allocator<gloox::Disco::Item*> >&) {}
101 
102  /* Log handler */
103  virtual void handleLog(gloox::LogLevel level, gloox::LogArea area, const std::string& message);
104 
105  /* ConnectionListener handlers*/
106  virtual void onConnect();
107  virtual void onDisconnect(gloox::ConnectionError e);
108  virtual bool onTLSConnect(const glooxwrapper::CertInfo& info);
109 
110  /* Iq Handlers */
111  virtual bool handleIq(const glooxwrapper::IQ& iq);
112  virtual void handleIqID(const glooxwrapper::IQ&, int) {}
113 
114  /* Registration Handlers */
115  virtual void handleRegistrationFields(const glooxwrapper::JID& /*from*/, int fields, glooxwrapper::string instructions );
116  virtual void handleRegistrationResult(const glooxwrapper::JID& /*from*/, gloox::RegistrationResult result);
117  virtual void handleAlreadyRegistered(const glooxwrapper::JID& /*from*/);
118  virtual void handleDataForm(const glooxwrapper::JID& /*from*/, const glooxwrapper::DataForm& /*form*/);
119  virtual void handleOOB(const glooxwrapper::JID& /*from*/, const glooxwrapper::OOB& oob);
120 
121  /* Message Handler */
122  virtual void handleMessage(const glooxwrapper::Message& msg, glooxwrapper::MessageSession * session);
123 
124  // Helpers
125  void GetPresenceString(const gloox::Presence::PresenceType p, std::string& presence) const;
126  void GetRoleString(const gloox::MUCRoomRole r, std::string& role) const;
127  std::string StanzaErrorToString(gloox::StanzaError err) const;
128  std::string ConnectionErrorToString(gloox::ConnectionError err) const;
129  std::string RegistrationResultToString(gloox::RegistrationResult res) const;
130 
131 public:
132  /* Messages */
133  struct GUIMessage
134  {
135  std::wstring type;
136  std::wstring level;
137  std::wstring text;
138  std::wstring data;
139  std::wstring from;
140  std::wstring message;
141  std::string datetime;
142  };
143  void GuiPollMessage(ScriptInterface& scriptInterface, JS::MutableHandleValue ret);
144  void SendMUCMessage(const std::string& message);
145  void ClearPresenceUpdates();
146  int GetMucMessageCount();
147 protected:
148  void PushGuiMessage(XmppClient::GUIMessage message);
149  void CreateGUIMessage(const std::string& type, const std::string& level, const std::string& text = "", const std::string& data = "");
150 
151 private:
152  /// Map of players
153  std::map<std::string, std::vector<std::string> > m_PlayerMap;
154  /// List of games
155  std::vector<const glooxwrapper::Tag*> m_GameList;
156  /// List of rankings
157  std::vector<const glooxwrapper::Tag*> m_BoardList;
158  /// Profile data
159  std::vector<const glooxwrapper::Tag*> m_Profile;
160  /// Queue of messages for the GUI
161  std::deque<GUIMessage> m_GuiMessageQueue;
162  /// Current room subject/topic.
163  std::string m_Subject;
164 };
165 
166 #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:217
Definition: glooxwrapper.h:347
bool m_initialLoadComplete
Definition: XmppClient.h:53
std::map< std::string, std::vector< std::string > > m_PlayerMap
Map of players.
Definition: XmppClient.h:153
std::string m_username
Definition: XmppClient.h:47
std::wstring message
Definition: XmppClient.h:140
Definition: glooxwrapper.h:298
std::wstring data
Definition: XmppClient.h:138
Definition: glooxwrapper.h:497
Definition: glooxwrapper.h:459
Definition: glooxwrapper.h:517
Definition: IXmppClient.h:25
Definition: glooxwrapper.h:479
Definition: glooxwrapper.h:525
glooxwrapper::Registration * m_registration
Definition: XmppClient.h:44
glooxwrapper::Client * m_client
Definition: XmppClient.h:42
Definition: glooxwrapper.h:330
std::wstring type
Definition: XmppClient.h:135
Definition: glooxwrapper.h:374
std::string m_password
Definition: XmppClient.h:48
Definition: glooxwrapper.h:337
std::wstring level
Definition: XmppClient.h:136
std::vector< const glooxwrapper::Tag * > m_BoardList
List of rankings.
Definition: XmppClient.h:157
Definition: glooxwrapper.h:108
std::vector< const glooxwrapper::Tag * > m_GameList
List of games.
Definition: XmppClient.h:155
Definition: XmppClient.h:133
Definition: XmppClient.h:36
std::string m_xpartamuppId
Definition: XmppClient.h:50
std::string m_nick
Definition: XmppClient.h:49
Definition: glooxwrapper.h:264
std::string m_Subject
Current room subject/topic.
Definition: XmppClient.h:163
std::vector< const glooxwrapper::Tag * > m_Profile
Profile data.
Definition: XmppClient.h:159
Definition: glooxwrapper.h:322
virtual void handleIqID(const glooxwrapper::IQ &, int)
Definition: XmppClient.h:112
std::string datetime
Definition: XmppClient.h:141
Abstraction around a SpiderMonkey JSContext.
Definition: ScriptInterface.h:71
std::deque< GUIMessage > m_GuiMessageQueue
Queue of messages for the GUI.
Definition: XmppClient.h:161
Definition: glooxwrapper.h:431
Definition: glooxwrapper.h:313
Definition: glooxwrapper.cpp:45
std::wstring text
Definition: XmppClient.h:137
glooxwrapper::MUCRoom * m_mucRoom
Definition: XmppClient.h:43
std::wstring from
Definition: XmppClient.h:139