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 161 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 30 0.0 %

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

Generated by: LCOV version 1.16