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: 0 192 0.0 %
Date: 2022-06-14 00:41:00 Functions: 0 30 0.0 %

          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             :         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             :         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           0 : 
     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           0 :  */
     126           0 : GameListQuery::GameListQuery(const glooxwrapper::Tag* tag)
     127             :     : StanzaExtension(EXTGAMELISTQUERY)
     128           0 : {
     129           0 :     if (!tag || tag->name() != "query" || tag->xmlns() != XMLNS_GAMELIST)
     130           0 :         return;
     131           0 : 
     132             :     const glooxwrapper::Tag* c = tag->findTag_clone("query/command");
     133             :     if (c)
     134             :         m_Command = c->cdata();
     135             :     glooxwrapper::Tag::free(c);
     136             : 
     137             :     for (const glooxwrapper::Tag* const& t : tag->findTagList_clone("query/game"))
     138           0 :         m_GameList.emplace_back(t);
     139           0 : }
     140             : 
     141           0 : /**
     142             :  * Required by gloox, used to find the GameList element in a received IQ.
     143             :  */
     144           0 : const glooxwrapper::string& GameListQuery::filterString() const
     145           0 : {
     146           0 :     static const glooxwrapper::string filter = "/iq/query[@xmlns='" XMLNS_GAMELIST "']";
     147           0 :     return filter;
     148             : }
     149           0 : 
     150           0 : /**
     151             :  * Required by gloox, used to serialize the game object into XML for sending.
     152             :  */
     153             : glooxwrapper::Tag* GameListQuery::tag() const
     154             : {
     155             :     glooxwrapper::Tag* t = glooxwrapper::Tag::allocate("query");
     156           0 :     t->setXmlns(XMLNS_GAMELIST);
     157             : 
     158           0 :     // Check for register / unregister command
     159           0 :     if (!m_Command.empty())
     160             :         t->addChild(glooxwrapper::Tag::allocate("command", m_Command));
     161             : 
     162             :     for (const glooxwrapper::Tag* const& tag : m_GameList)
     163             :         t->addChild(tag->clone());
     164             : 
     165           0 :     return t;
     166             : }
     167           0 : 
     168           0 : glooxwrapper::StanzaExtension* GameListQuery::clone() const
     169             : {
     170             :     GameListQuery* q = new GameListQuery();
     171           0 :     return q;
     172           0 : }
     173             : 
     174           0 : GameListQuery::~GameListQuery()
     175           0 : {
     176             :     for (const glooxwrapper::Tag* const & t : m_GameList)
     177           0 :         glooxwrapper::Tag::free(t);
     178             :     m_GameList.clear();
     179             : }
     180           0 : 
     181             : /******************************************************
     182           0 :  * ProfileQuery, a custom IQ Stanza useful for fetching
     183           0 :  * user profiles
     184             :  * Example stanza:
     185             :  * <profile player="foobar" highestRating="1500" rank="1895" totalGamesPlayed="50"
     186           0 :  *  wins="25" losses="25" /><command>foobar</command>
     187             :  */
     188           0 : ProfileQuery::ProfileQuery(const glooxwrapper::Tag* tag)
     189           0 :     : StanzaExtension(EXTPROFILEQUERY)
     190           0 : {
     191           0 :     if (!tag || tag->name() != "query" || tag->xmlns() != XMLNS_PROFILE)
     192           0 :         return;
     193             : 
     194             :     const glooxwrapper::Tag* c = tag->findTag_clone("query/command");
     195             :     if (c)
     196             :         m_Command = c->cdata();
     197           0 :     glooxwrapper::Tag::free(c);
     198           0 : 
     199             :     for (const glooxwrapper::Tag* const& t : tag->findTagList_clone("query/profile"))
     200           0 :         m_StanzaProfile.emplace_back(t);
     201           0 : }
     202           0 : 
     203           0 : /**
     204             :  * Required by gloox, used to find the Profile element in a received IQ.
     205             :  */
     206             : const glooxwrapper::string& ProfileQuery::filterString() const
     207             : {
     208             :     static const glooxwrapper::string filter = "/iq/query[@xmlns='" XMLNS_PROFILE "']";
     209             :     return filter;
     210             : }
     211             : 
     212           0 : /**
     213           0 :  * Required by gloox, used to serialize the Profile request into XML for sending.
     214             :  */
     215           0 : glooxwrapper::Tag* ProfileQuery::tag() const
     216             : {
     217             :     glooxwrapper::Tag* t = glooxwrapper::Tag::allocate("query");
     218           0 :     t->setXmlns(XMLNS_PROFILE);
     219           0 : 
     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             :     return t;
     227             : }
     228             : 
     229             : glooxwrapper::StanzaExtension* ProfileQuery::clone() const
     230           0 : {
     231             :     ProfileQuery* q = new ProfileQuery();
     232           0 :     return q;
     233           0 : }
     234             : 
     235             : ProfileQuery::~ProfileQuery()
     236             : {
     237             :     for (const glooxwrapper::Tag* const& t : m_StanzaProfile)
     238             :         glooxwrapper::Tag::free(t);
     239           0 :     m_StanzaProfile.clear();
     240             : }
     241           0 : 
     242           0 : /******************************************************
     243             :  * LobbyAuth, a custom IQ Stanza, used to send and
     244           0 :  * receive a security token for hosting authentication.
     245           0 :  */
     246             : LobbyAuth::LobbyAuth(const glooxwrapper::Tag* tag)
     247           0 :     : StanzaExtension(EXTLOBBYAUTH)
     248           0 : {
     249             :     if (!tag || tag->name() != "auth" || tag->xmlns() != XMLNS_LOBBYAUTH)
     250           0 :         return;
     251             : 
     252             :     const glooxwrapper::Tag* c = tag->findTag_clone("auth/token");
     253           0 :     if (c)
     254             :         m_Token = c->cdata();
     255           0 : 
     256           0 :     glooxwrapper::Tag::free(c);
     257             : }
     258             : 
     259           0 : /**
     260             :  * Required by gloox, used to find the LobbyAuth element in a received IQ.
     261           0 :  */
     262           0 : const glooxwrapper::string& LobbyAuth::filterString() const
     263           0 : {
     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           0 :  */
     271           0 : glooxwrapper::Tag* LobbyAuth::tag() const
     272             : {
     273           0 :     glooxwrapper::Tag* t = glooxwrapper::Tag::allocate("auth");
     274           0 :     t->setXmlns(XMLNS_LOBBYAUTH);
     275           0 : 
     276           0 :     // Check for the auth token
     277             :     if (!m_Token.empty())
     278             :         t->addChild(glooxwrapper::Tag::allocate("token", m_Token));
     279             :     return t;
     280             : }
     281             : 
     282           0 : glooxwrapper::StanzaExtension* LobbyAuth::clone() const
     283           0 : {
     284             :     return new LobbyAuth();
     285           0 : }
     286             : 
     287             : /******************************************************
     288           0 :  * ConnectionData, a custom IQ Stanza, used to send and
     289           0 :  * receive a ip and port of the server.
     290           0 :  */
     291             : ConnectionData::ConnectionData(const glooxwrapper::Tag* tag)
     292           0 :     : StanzaExtension(EXTCONNECTIONDATA)
     293             : {
     294             :     if (!tag || tag->name() != "connectiondata" || tag->xmlns() != XMLNS_CONNECTIONDATA)
     295             :         return;
     296             : 
     297             :     const glooxwrapper::Tag* c = tag->findTag_clone("connectiondata/ip");
     298           0 :     if (c)
     299             :         m_Ip = c->cdata();
     300           0 :     const glooxwrapper::Tag* p= tag->findTag_clone("connectiondata/port");
     301           0 :     if (p)
     302             :         m_Port = p->cdata();
     303             :     const glooxwrapper::Tag* pip = tag->findTag_clone("connectiondata/isLocalIP");
     304             :     if (pip)
     305             :         m_IsLocalIP = pip->cdata();
     306             :     const glooxwrapper::Tag* s = tag->findTag_clone("connectiondata/useSTUN");
     307           0 :     if (s)
     308             :         m_UseSTUN = s->cdata();
     309           0 :     const glooxwrapper::Tag* pw = tag->findTag_clone("connectiondata/password");
     310           0 :     if (pw)
     311             :         m_Password = pw->cdata();
     312             :     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             :     if (e)
     317             :         m_Error= e->cdata();
     318           0 : 
     319             :     glooxwrapper::Tag::free(c);
     320           0 :     glooxwrapper::Tag::free(p);
     321             :     glooxwrapper::Tag::free(pip);
     322             :     glooxwrapper::Tag::free(s);
     323             :     glooxwrapper::Tag::free(pw);
     324             :     glooxwrapper::Tag::free(cs);
     325             :     glooxwrapper::Tag::free(e);
     326             : }
     327           0 : 
     328           0 : /**
     329             :  * Required by gloox, used to find the LobbyAuth element in a received IQ.
     330           0 :  */
     331             : const glooxwrapper::string& ConnectionData::filterString() const
     332             : {
     333           0 :     static const glooxwrapper::string filter = "/iq/connectiondata[@xmlns='" XMLNS_CONNECTIONDATA "']";
     334           0 :     return filter;
     335           0 : }
     336           0 : 
     337           0 : /**
     338           0 :  * Required by gloox, used to serialize the auth object into XML for sending.
     339           0 :  */
     340           0 : glooxwrapper::Tag* ConnectionData::tag() const
     341           0 : {
     342           0 :     glooxwrapper::Tag* t = glooxwrapper::Tag::allocate("connectiondata");
     343           0 :     t->setXmlns(XMLNS_CONNECTIONDATA);
     344           0 : 
     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             :         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           0 : }
     361           0 : 
     362             : glooxwrapper::StanzaExtension* ConnectionData::clone() const
     363             : {
     364             :     return new ConnectionData();
     365             : }

Generated by: LCOV version 1.13