LCOV - code coverage report
Current view: top level - source/lobby - StanzaExtensions.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 1 178 0.6 %
Date: 2023-01-19 00:18:29 Functions: 2 32 6.2 %

          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             : #include "precompiled.h"
      18             : #include "StanzaExtensions.h"
      19             : 
      20             : /******************************************************
      21             :  * GameReport, fairly generic custom stanza extension used
      22             :  * to report game statistics.
      23             :  */
      24           0 : GameReport::GameReport(const glooxwrapper::Tag* tag)
      25           0 :     : StanzaExtension(EXTGAMEREPORT)
      26             : {
      27           0 :     if (!tag || tag->name() != "report" || tag->xmlns() != XMLNS_GAMEREPORT)
      28           0 :         return;
      29             :     // TODO if we want to handle receiving this stanza extension.
      30             : };
      31             : 
      32             : /**
      33             :  * Required by gloox, used to serialize the GameReport into XML for sending.
      34             :  */
      35           0 : glooxwrapper::Tag* GameReport::tag() const
      36             : {
      37           0 :     glooxwrapper::Tag* t = glooxwrapper::Tag::allocate("report");
      38           0 :     t->setXmlns(XMLNS_GAMEREPORT);
      39             : 
      40           0 :     for (const glooxwrapper::Tag* const& tag : m_GameReport)
      41           0 :         t->addChild(tag->clone());
      42             : 
      43           0 :     return t;
      44             : }
      45             : 
      46             : /**
      47             :  * Required by gloox, used to find the GameReport element in a recived IQ.
      48             :  */
      49           0 : const glooxwrapper::string& GameReport::filterString() const
      50             : {
      51           0 :     static const glooxwrapper::string filter = "/iq/report[@xmlns='" XMLNS_GAMEREPORT "']";
      52           0 :     return filter;
      53             : }
      54             : 
      55           0 : glooxwrapper::StanzaExtension* GameReport::clone() const
      56             : {
      57           0 :     GameReport* q = new GameReport();
      58           0 :     return q;
      59             : }
      60             : 
      61             : /******************************************************
      62             :  * BoardListQuery, a flexible custom IQ Stanza useful for anything with ratings, used to
      63             :  * request and receive leaderboard and rating data from server.
      64             :  * Example stanza:
      65             :  * <board player="foobar">1200</board>
      66             :  */
      67           0 : BoardListQuery::BoardListQuery(const glooxwrapper::Tag* tag)
      68           0 :     : StanzaExtension(EXTBOARDLISTQUERY)
      69             : {
      70           0 :     if (!tag || tag->name() != "query" || tag->xmlns() != XMLNS_BOARDLIST)
      71           0 :         return;
      72             : 
      73           0 :     const glooxwrapper::Tag* c = tag->findTag_clone("query/command");
      74           0 :     if (c)
      75           0 :         m_Command = c->cdata();
      76           0 :     glooxwrapper::Tag::free(c);
      77           0 :     for (const glooxwrapper::Tag* const& t : tag->findTagList_clone("query/board"))
      78           0 :         m_StanzaBoardList.emplace_back(t);
      79             : }
      80             : 
      81             : /**
      82             :  * Required by gloox, used to find the BoardList element in a received IQ.
      83             :  */
      84           0 : const glooxwrapper::string& BoardListQuery::filterString() const
      85             : {
      86           0 :     static const glooxwrapper::string filter = "/iq/query[@xmlns='" XMLNS_BOARDLIST "']";
      87           0 :     return filter;
      88             : }
      89             : 
      90             : /**
      91             :  * Required by gloox, used to serialize the BoardList request into XML for sending.
      92             :  */
      93           0 : glooxwrapper::Tag* BoardListQuery::tag() const
      94             : {
      95           0 :     glooxwrapper::Tag* t = glooxwrapper::Tag::allocate("query");
      96           0 :     t->setXmlns(XMLNS_BOARDLIST);
      97             : 
      98             :     // Check for ratinglist or boardlist command
      99           0 :     if (!m_Command.empty())
     100           0 :         t->addChild(glooxwrapper::Tag::allocate("command", m_Command));
     101             : 
     102           0 :     for (const glooxwrapper::Tag* const& tag : m_StanzaBoardList)
     103           0 :         t->addChild(tag->clone());
     104             : 
     105           0 :     return t;
     106             : }
     107             : 
     108           0 : glooxwrapper::StanzaExtension* BoardListQuery::clone() const
     109             : {
     110           0 :     BoardListQuery* q = new BoardListQuery();
     111           0 :     return q;
     112             : }
     113             : 
     114           0 : BoardListQuery::~BoardListQuery()
     115             : {
     116           0 :     for (const glooxwrapper::Tag* const& t : m_StanzaBoardList)
     117           0 :         glooxwrapper::Tag::free(t);
     118           0 :     m_StanzaBoardList.clear();
     119           0 : }
     120             : 
     121             : /******************************************************
     122             :  * GameListQuery, custom IQ Stanza, used to receive
     123             :  * the listing of games from the server, and register/
     124             :  * unregister/changestate games on the server.
     125             :  */
     126           0 : GameListQuery::GameListQuery(const glooxwrapper::Tag* tag)
     127           0 :     : StanzaExtension(EXTGAMELISTQUERY)
     128             : {
     129           0 :     if (!tag || tag->name() != "query" || tag->xmlns() != XMLNS_GAMELIST)
     130           0 :         return;
     131             : 
     132           0 :     const glooxwrapper::Tag* c = tag->findTag_clone("query/command");
     133           0 :     if (c)
     134           0 :         m_Command = c->cdata();
     135           0 :     glooxwrapper::Tag::free(c);
     136             : 
     137           0 :     for (const glooxwrapper::Tag* const& t : tag->findTagList_clone("query/game"))
     138           0 :         m_GameList.emplace_back(t);
     139             : }
     140             : 
     141             : /**
     142             :  * Required by gloox, used to find the GameList element in a received IQ.
     143             :  */
     144           0 : const glooxwrapper::string& GameListQuery::filterString() const
     145             : {
     146           0 :     static const glooxwrapper::string filter = "/iq/query[@xmlns='" XMLNS_GAMELIST "']";
     147           0 :     return filter;
     148             : }
     149             : 
     150             : /**
     151             :  * Required by gloox, used to serialize the game object into XML for sending.
     152             :  */
     153           0 : glooxwrapper::Tag* GameListQuery::tag() const
     154             : {
     155           0 :     glooxwrapper::Tag* t = glooxwrapper::Tag::allocate("query");
     156           0 :     t->setXmlns(XMLNS_GAMELIST);
     157             : 
     158             :     // Check for register / unregister command
     159           0 :     if (!m_Command.empty())
     160           0 :         t->addChild(glooxwrapper::Tag::allocate("command", m_Command));
     161             : 
     162           0 :     for (const glooxwrapper::Tag* const& tag : m_GameList)
     163           0 :         t->addChild(tag->clone());
     164             : 
     165           0 :     return t;
     166             : }
     167             : 
     168           0 : glooxwrapper::StanzaExtension* GameListQuery::clone() const
     169             : {
     170           0 :     GameListQuery* q = new GameListQuery();
     171           0 :     return q;
     172             : }
     173             : 
     174           0 : GameListQuery::~GameListQuery()
     175             : {
     176           0 :     for (const glooxwrapper::Tag* const & t : m_GameList)
     177           0 :         glooxwrapper::Tag::free(t);
     178           0 :     m_GameList.clear();
     179           0 : }
     180             : 
     181             : /******************************************************
     182             :  * ProfileQuery, a custom IQ Stanza useful for fetching
     183             :  * user profiles
     184             :  * Example stanza:
     185             :  * <profile player="foobar" highestRating="1500" rank="1895" totalGamesPlayed="50"
     186             :  *  wins="25" losses="25" /><command>foobar</command>
     187             :  */
     188           0 : ProfileQuery::ProfileQuery(const glooxwrapper::Tag* tag)
     189           0 :     : StanzaExtension(EXTPROFILEQUERY)
     190             : {
     191           0 :     if (!tag || tag->name() != "query" || tag->xmlns() != XMLNS_PROFILE)
     192           0 :         return;
     193             : 
     194           0 :     const glooxwrapper::Tag* c = tag->findTag_clone("query/command");
     195           0 :     if (c)
     196           0 :         m_Command = c->cdata();
     197           0 :     glooxwrapper::Tag::free(c);
     198             : 
     199           0 :     for (const glooxwrapper::Tag* const& t : tag->findTagList_clone("query/profile"))
     200           0 :         m_StanzaProfile.emplace_back(t);
     201             : }
     202             : 
     203             : /**
     204             :  * Required by gloox, used to find the Profile element in a received IQ.
     205             :  */
     206           0 : const glooxwrapper::string& ProfileQuery::filterString() const
     207             : {
     208           0 :     static const glooxwrapper::string filter = "/iq/query[@xmlns='" XMLNS_PROFILE "']";
     209           0 :     return filter;
     210             : }
     211             : 
     212             : /**
     213             :  * Required by gloox, used to serialize the Profile request into XML for sending.
     214             :  */
     215           0 : glooxwrapper::Tag* ProfileQuery::tag() const
     216             : {
     217           0 :     glooxwrapper::Tag* t = glooxwrapper::Tag::allocate("query");
     218           0 :     t->setXmlns(XMLNS_PROFILE);
     219             : 
     220           0 :     if (!m_Command.empty())
     221           0 :         t->addChild(glooxwrapper::Tag::allocate("command", m_Command));
     222             : 
     223           0 :     for (const glooxwrapper::Tag* const& tag : m_StanzaProfile)
     224           0 :         t->addChild(tag->clone());
     225             : 
     226           0 :     return t;
     227             : }
     228             : 
     229           0 : glooxwrapper::StanzaExtension* ProfileQuery::clone() const
     230             : {
     231           0 :     ProfileQuery* q = new ProfileQuery();
     232           0 :     return q;
     233             : }
     234             : 
     235           0 : ProfileQuery::~ProfileQuery()
     236             : {
     237           0 :     for (const glooxwrapper::Tag* const& t : m_StanzaProfile)
     238           0 :         glooxwrapper::Tag::free(t);
     239           0 :     m_StanzaProfile.clear();
     240           0 : }
     241             : 
     242             : /******************************************************
     243             :  * LobbyAuth, a custom IQ Stanza, used to send and
     244             :  * receive a security token for hosting authentication.
     245             :  */
     246           0 : LobbyAuth::LobbyAuth(const glooxwrapper::Tag* tag)
     247           0 :     : StanzaExtension(EXTLOBBYAUTH)
     248             : {
     249           0 :     if (!tag || tag->name() != "auth" || tag->xmlns() != XMLNS_LOBBYAUTH)
     250           0 :         return;
     251             : 
     252           0 :     const glooxwrapper::Tag* c = tag->findTag_clone("auth/token");
     253           0 :     if (c)
     254           0 :         m_Token = c->cdata();
     255             : 
     256           0 :     glooxwrapper::Tag::free(c);
     257             : }
     258             : 
     259             : /**
     260             :  * Required by gloox, used to find the LobbyAuth element in a received IQ.
     261             :  */
     262           0 : const glooxwrapper::string& LobbyAuth::filterString() const
     263             : {
     264           0 :     static const glooxwrapper::string filter = "/iq/auth[@xmlns='" XMLNS_LOBBYAUTH "']";
     265           0 :     return filter;
     266             : }
     267             : 
     268             : /**
     269             :  * Required by gloox, used to serialize the auth object into XML for sending.
     270             :  */
     271           0 : glooxwrapper::Tag* LobbyAuth::tag() const
     272             : {
     273           0 :     glooxwrapper::Tag* t = glooxwrapper::Tag::allocate("auth");
     274           0 :     t->setXmlns(XMLNS_LOBBYAUTH);
     275             : 
     276             :     // Check for the auth token
     277           0 :     if (!m_Token.empty())
     278           0 :         t->addChild(glooxwrapper::Tag::allocate("token", m_Token));
     279           0 :     return t;
     280             : }
     281             : 
     282           0 : glooxwrapper::StanzaExtension* LobbyAuth::clone() const
     283             : {
     284           0 :     return new LobbyAuth();
     285             : }
     286             : 
     287             : /******************************************************
     288             :  * ConnectionData, a custom IQ Stanza, used to send and
     289             :  * receive a ip and port of the server.
     290             :  */
     291           0 : ConnectionData::ConnectionData(const glooxwrapper::Tag* tag)
     292           0 :     : StanzaExtension(EXTCONNECTIONDATA)
     293             : {
     294           0 :     if (!tag || tag->name() != "connectiondata" || tag->xmlns() != XMLNS_CONNECTIONDATA)
     295           0 :         return;
     296             : 
     297           0 :     const glooxwrapper::Tag* c = tag->findTag_clone("connectiondata/ip");
     298           0 :     if (c)
     299           0 :         m_Ip = c->cdata();
     300           0 :     const glooxwrapper::Tag* p= tag->findTag_clone("connectiondata/port");
     301           0 :     if (p)
     302           0 :         m_Port = p->cdata();
     303           0 :     const glooxwrapper::Tag* pip = tag->findTag_clone("connectiondata/isLocalIP");
     304           0 :     if (pip)
     305           0 :         m_IsLocalIP = pip->cdata();
     306           0 :     const glooxwrapper::Tag* s = tag->findTag_clone("connectiondata/useSTUN");
     307           0 :     if (s)
     308           0 :         m_UseSTUN = s->cdata();
     309           0 :     const glooxwrapper::Tag* pw = tag->findTag_clone("connectiondata/password");
     310           0 :     if (pw)
     311           0 :         m_Password = pw->cdata();
     312           0 :     const glooxwrapper::Tag* cs = tag->findTag_clone("connectiondata/clientsalt");
     313           0 :     if (cs)
     314           0 :         m_ClientSalt = cs->cdata();
     315           0 :     const glooxwrapper::Tag* e = tag->findTag_clone("connectiondata/error");
     316           0 :     if (e)
     317           0 :         m_Error= e->cdata();
     318             : 
     319           0 :     glooxwrapper::Tag::free(c);
     320           0 :     glooxwrapper::Tag::free(p);
     321           0 :     glooxwrapper::Tag::free(pip);
     322           0 :     glooxwrapper::Tag::free(s);
     323           0 :     glooxwrapper::Tag::free(pw);
     324           0 :     glooxwrapper::Tag::free(cs);
     325           0 :     glooxwrapper::Tag::free(e);
     326             : }
     327             : 
     328             : /**
     329             :  * Required by gloox, used to find the LobbyAuth element in a received IQ.
     330             :  */
     331           0 : const glooxwrapper::string& ConnectionData::filterString() const
     332             : {
     333           0 :     static const glooxwrapper::string filter = "/iq/connectiondata[@xmlns='" XMLNS_CONNECTIONDATA "']";
     334           0 :     return filter;
     335             : }
     336             : 
     337             : /**
     338             :  * Required by gloox, used to serialize the auth object into XML for sending.
     339             :  */
     340           0 : glooxwrapper::Tag* ConnectionData::tag() const
     341             : {
     342           0 :     glooxwrapper::Tag* t = glooxwrapper::Tag::allocate("connectiondata");
     343           0 :     t->setXmlns(XMLNS_CONNECTIONDATA);
     344             : 
     345           0 :     if (!m_Ip.empty())
     346           0 :         t->addChild(glooxwrapper::Tag::allocate("ip", m_Ip));
     347           0 :     if (!m_Port.empty())
     348           0 :         t->addChild(glooxwrapper::Tag::allocate("port", m_Port));
     349           0 :     if (!m_IsLocalIP.empty())
     350           0 :         t->addChild(glooxwrapper::Tag::allocate("isLocalIP", m_IsLocalIP));
     351           0 :     if (!m_UseSTUN.empty())
     352           0 :         t->addChild(glooxwrapper::Tag::allocate("useSTUN", m_UseSTUN));
     353           0 :     if (!m_Password.empty())
     354           0 :         t->addChild(glooxwrapper::Tag::allocate("password", m_Password));
     355           0 :     if (!m_ClientSalt.empty())
     356           0 :         t->addChild(glooxwrapper::Tag::allocate("clientsalt", m_ClientSalt));
     357           0 :     if (!m_Error.empty())
     358           0 :         t->addChild(glooxwrapper::Tag::allocate("error", m_Error));
     359           0 :     return t;
     360             : }
     361             : 
     362           0 : glooxwrapper::StanzaExtension* ConnectionData::clone() const
     363             : {
     364           0 :     return new ConnectionData();
     365           3 : }

Generated by: LCOV version 1.13