Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
4.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: defines a RCon class used to send rcon commands to remote servers
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================
  7. #include "banlist.h"
  8. #include "Iresponse.h"
  9. #include "PlayerMsgHandler.h"
  10. #include "Socket.h"
  11. #include "proto_oob.h"
  12. #include "DialogGameInfo.h"
  13. #include "inetapi.h"
  14. #include "TokenLine.h"
  15. #include "dialogkickplayer.h"
  16. extern void v_strncpy(char *dest, const char *src, int bufsize);
  17. typedef enum
  18. {
  19. NONE = 0,
  20. INFO_REQUESTED,
  21. INFO_RECEIVED
  22. } RCONSTATUS;
  23. CBanList::CBanList(IResponse *target,serveritem_t &server, const char *rconPassword) {
  24. memcpy(&m_Server, &server,sizeof(serveritem_t));
  25. m_pResponseTarget=target;
  26. m_bIsRefreshing=false;
  27. m_bNewBanList=false;
  28. m_bRconFailed=false;
  29. m_bGotIPs = false;
  30. v_strncpy(m_szRconPassword,rconPassword,100);
  31. m_pRcon = new CRcon(this , server,rconPassword);
  32. m_BanList=NULL;
  33. }
  34. CBanList::~CBanList() {
  35. delete m_pRcon;
  36. }
  37. //-----------------------------------------------------------------------------
  38. // Purpose: sends a status query packet to a single server
  39. //-----------------------------------------------------------------------------
  40. void CBanList::SendQuery()
  41. {
  42. // now use "rcon status" to pull the list of bans
  43. m_pRcon->SendRcon("listid");
  44. m_bGotIPs=false;
  45. }
  46. //-----------------------------------------------------------------------------
  47. // Purpose:
  48. //-----------------------------------------------------------------------------
  49. void CBanList::RunFrame()
  50. {
  51. if(m_pRcon)
  52. {
  53. m_pRcon->RunFrame();
  54. }
  55. }
  56. void CBanList::ServerResponded()
  57. {
  58. if(m_bGotIPs == false)
  59. {
  60. m_BanList.RemoveAll();
  61. const char *rconResp = m_pRcon->RconResponse();
  62. const char *cur = strstr(rconResp,"UserID filter list:")
  63. + strlen("UserID filter list:");
  64. if( (unsigned int)cur == strlen("UserID filter list:"))
  65. // if strstr returned NULL and we added a strlen to it...
  66. {
  67. cur=NULL;
  68. }
  69. // listid format:
  70. // UserID filter list:
  71. // 1 23434 : 20.000 min
  72. // and on empty it produces:
  73. // IP filter list: empty
  74. while(cur!=NULL)
  75. {
  76. TokenLine banLine;
  77. cur++; // dodge the newline
  78. banLine.SetLine(cur); // need to add one here, to remove the "\n"
  79. if(banLine.CountToken() >= 4 )
  80. {
  81. Bans_t ban;
  82. memset(&ban,0x0,sizeof(Bans_t));
  83. v_strncpy(ban.name,banLine.GetToken(1),20);
  84. sscanf(banLine.GetToken(3),"%f",&ban.time);
  85. ban.type= ID;
  86. m_BanList.AddToTail(ban);
  87. }
  88. cur=strchr(cur,'\n');
  89. }
  90. m_bGotIPs=true;
  91. // now find out the list of banned ips
  92. m_pRcon->SendRcon("listip");
  93. }
  94. else
  95. {
  96. // listip format:
  97. // IP filter list:
  98. //192.168. 1. 66 : 20.000 min
  99. const char *rconResp = m_pRcon->RconResponse();
  100. const char *cur = strstr(rconResp,"IP filter list:")
  101. + strlen("IP filter list:");
  102. if( (unsigned int)cur == strlen("IP filter list:"))
  103. // if strstr returned NULL and we added a strlen to it...
  104. {
  105. cur=NULL;
  106. }
  107. while(cur!=NULL)
  108. {
  109. char tmpStr[512];
  110. Bans_t ban;
  111. cur++; // dodge past the newline
  112. v_strncpy(tmpStr,cur,512);
  113. memset(&ban,0x0,sizeof(Bans_t));
  114. if( strchr(tmpStr,':') != NULL )
  115. {
  116. char *time;
  117. time = strchr(tmpStr,':')+1;
  118. tmpStr[strchr(tmpStr,':')-tmpStr]=0;
  119. v_strncpy(ban.name,tmpStr,20);
  120. unsigned int i=0;
  121. while(i<strlen(ban.name)) // strip all the white space out...
  122. {
  123. if( ban.name[i]==' ')
  124. {
  125. strcpy(&ban.name[i],&ban.name[i+1]);
  126. }
  127. else
  128. {
  129. i++;
  130. }
  131. }
  132. sscanf(time,"%f",&ban.time);
  133. ban.type= IP;
  134. m_BanList.AddToTail(ban);
  135. }
  136. cur=strchr(cur,'\n');
  137. }
  138. m_bNewBanList=true;
  139. m_bIsRefreshing=false;
  140. // notify the UI of the new server info
  141. m_pResponseTarget->ServerResponded();
  142. }
  143. }
  144. void CBanList::ServerFailedToRespond()
  145. {
  146. m_bNewBanList=true;
  147. m_bIsRefreshing=false;
  148. if(m_bRconFailed==false)
  149. {
  150. m_bRconFailed=true;
  151. // CDialogKickPlayer *box = new CDialogKickPlayer();
  152. //box->addActionSignalTarget(this);
  153. // box->Activate("","Bad Rcon Password","badrcon");
  154. }
  155. // m_pResponseTarget->ServerFailedToRespond();
  156. }
  157. void CBanList::Refresh()
  158. {
  159. SendQuery();
  160. }
  161. bool CBanList::IsRefreshing()
  162. {
  163. return m_bIsRefreshing;
  164. }
  165. serveritem_t &CBanList::GetServer()
  166. {
  167. return m_Server;
  168. }
  169. bool CBanList::NewBanList()
  170. {
  171. return m_bNewBanList;
  172. }
  173. CUtlVector<Bans_t> *CBanList::GetBanList()
  174. {
  175. m_bNewBanList=false;
  176. return &m_BanList;
  177. }
  178. void CBanList::SetPassword(const char *newPass)
  179. {
  180. m_pRcon->SetPassword(newPass);
  181. m_bRconFailed=false;
  182. }