PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/rcon/executors/irc2rcon-status.php

https://gitlab.com/mattia.basaglia/Melanobot
PHP | 236 lines | 178 code | 30 blank | 28 comment | 24 complexity | 15f83e08d7f5eb303c2971a75dc11938 MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php
  2. /**
  3. * \file
  4. * \author Mattia Basaglia
  5. * \copyright Copyright 2013-2014 Mattia Basaglia
  6. * \section License
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. class Irc2Rcon_Who extends Irc2Rcon_Executor
  21. {
  22. function __construct(Rcon $rcon, $trigger="who", $auth=null)
  23. {
  24. parent::__construct($rcon,$trigger,$auth,"$trigger","List players on {$rcon->read}");
  25. }
  26. function execute(MelanoBotCommand $cmd, MelanoBot $bot, BotData $data)
  27. {
  28. $player_manager = $this->data($data)->player;
  29. $players = array();
  30. foreach($player_manager->all() as $player)
  31. {
  32. $players[]= Color::dp2irc($player->name);
  33. }
  34. if ( !empty($players) )
  35. $bot->say($cmd->channel,$this->out_prefix()."\00304".count($players).
  36. "\xf/\00304{$player_manager->max}\xf: ".implode(", ",$players));
  37. else
  38. $bot->say($cmd->channel,$this->out_prefix()."Server is empty");
  39. }
  40. }
  41. class Irc2Rcon_Status extends Irc2Rcon_Executor
  42. {
  43. public $private;
  44. function __construct(Rcon $rcon, $private = true, $trigger="status", $auth='rcon-admin')
  45. {
  46. parent::__construct($rcon,$trigger,$auth,"$trigger","Show players and server information");
  47. $this->private = $private;
  48. }
  49. function settings($channel, MelanoBot $bot, $rcon_data)
  50. {
  51. $gametype = "unknown";
  52. if ( isset($rcon_data->gametype) )
  53. $gametype = Rcon_Communicator::gametype_name($rcon_data->gametype);
  54. $mutators = "";
  55. if ( !empty($rcon_data->mutators) )
  56. $mutators = ", Mutators: ".implode(", ",$rcon_data->mutators);
  57. $bot->say($channel,"Map: \00304{$rcon_data->map}\017, Game: \00304$gametype\017$mutators", 16);
  58. }
  59. function execute(MelanoBotCommand $cmd, MelanoBot $bot, BotData $data)
  60. {
  61. $channel = $this->private ? $cmd->from : $cmd->channel;
  62. if ( !$this->comm->is_connected() )
  63. {
  64. $bot->say($channel,$this->out_prefix()."Server not connected", 16);
  65. return;
  66. }
  67. $rcon_data = $this->data($data);
  68. $player_manager = $rcon_data->player;
  69. /// \todo status only on users matching the parameters
  70. if ( $player_manager->count_all() > 0 )
  71. {
  72. $bot->say($channel,sprintf("\002%-21s %2s %4s %5s %-4s %s\xf",
  73. "ip address", "pl", "ping", "frags", "slot", "name" ), 16);
  74. $spects = 0;
  75. $match = $cmd->param_string();
  76. foreach($player_manager->all() as $player)
  77. {
  78. if ( !$match || stripos(Color::dp2none($player->name),$match) !== false )
  79. {
  80. $frags = $player->frags;
  81. if ( $frags == "-666" )
  82. {
  83. $frags = "spect";
  84. if ( !$player->is_bot() )
  85. $spects++;
  86. }
  87. $bot->say($channel,
  88. sprintf("%-21s %2s %4s %5s #%-2s %s",
  89. $player->ip,
  90. $player->pl,
  91. $player->ping,
  92. $frags,
  93. $player->slot,
  94. Color::dp2irc($player->name)
  95. ), 16);
  96. }
  97. }
  98. $bot->say($channel,$this->out_prefix()."Players: \00304".($player_manager->count_players()-$spects).
  99. "\xf active, \00304$spects\xf spectators, \00304".$player_manager->count_bots().
  100. "\017 bots, \00304".$player_manager->count_all()."\xf/{$player_manager->max} total", 16);
  101. }
  102. else
  103. $bot->say($channel,$this->out_prefix()."No users in server", 16);
  104. $this->settings($channel,$bot,$rcon_data);
  105. }
  106. }
  107. /**
  108. * \note Requires Rcon2Irc_GetCvars or similar, g_maplist (slow) polling is reccommended
  109. */
  110. class Irc2Rcon_Maps extends Irc2Rcon_Executor
  111. {
  112. public $max_count;///< Maximum number of matches to show an actual list
  113. function __construct(Rcon $rcon, $max_count=6, $trigger="maps", $auth=null)
  114. {
  115. parent::__construct($rcon,$trigger,$auth,"$trigger [pattern]","Find maps matching [pattern]");
  116. $this->max_count = $max_count;
  117. }
  118. function execute(MelanoBotCommand $cmd, MelanoBot $bot, BotData $data)
  119. {
  120. $rcon_data = $this->data($data);
  121. if ( !isset($rcon_data->cvar) || !isset($rcon_data->cvar["g_maplist"]) )
  122. {
  123. $this->rcon->send("g_maplist");
  124. $bot->say($cmd->channel,$this->out_prefix()."Map list not initialized, try again in a few moments");
  125. }
  126. else
  127. {
  128. $maps = explode(" ",$rcon_data->cvar["g_maplist"]);
  129. $nmaps = count($maps);
  130. $pattern = str_replace(array('\\',"/"),array('\\\\',"\\/"),trim($cmd->param_string()));
  131. if ( $pattern )
  132. {
  133. $maps = preg_grep("/$pattern/",$maps);
  134. $nmatch = count($maps);
  135. $bot->say($cmd->channel,$this->out_prefix()."\00310$nmatch\xf/\00304$nmaps\xf maps match");
  136. if ( $nmatch <= $this->max_count )
  137. foreach($maps as $map)
  138. $bot->say($cmd->channel,"\00303$map",-$this->max_count);
  139. }
  140. else
  141. {
  142. $bot->say($cmd->channel,$this->out_prefix()."\00304$nmaps\xf maps");
  143. }
  144. }
  145. }
  146. }
  147. /**
  148. * \note requires Rcon2Irc_UpdateBans, banlist (slow) polling is reccommended
  149. */
  150. class Irc2Rcon_Banlist extends Irc2Rcon_Executor
  151. {
  152. function __construct(Rcon $rcon, $trigger="banlist", $auth='rcon-admin')
  153. {
  154. parent::__construct($rcon,$trigger,$auth,"$trigger [refresh]","Show active bans");
  155. }
  156. function execute(MelanoBotCommand $cmd, MelanoBot $bot, BotData $data)
  157. {
  158. $rcon_data = $this->data($data);
  159. if ( isset($cmd->params[0]) && $cmd->params[0] == 'refresh')
  160. $this->rcon->send("banlist");
  161. else if ( empty($rcon_data->bans) )
  162. $bot->say($cmd->channel,"No active bans");
  163. else
  164. foreach ( $rcon_data->bans as $id => $ban )
  165. $bot->say($cmd->channel,sprintf(
  166. "#\00304%-3s \00310%-21s\xf %s seconds",
  167. $id,$ban->ip,$ban->time));
  168. }
  169. }
  170. /**
  171. * \brief Show information about the server
  172. */
  173. class Irc2Rcon_Server extends Irc2Rcon_Executor
  174. {
  175. function __construct(Rcon $rcon, $trigger="server", $auth=null)
  176. {
  177. parent::__construct($rcon,$trigger,$auth,"$trigger [ip|stats|game|status]","Show server information");
  178. }
  179. function execute(MelanoBotCommand $cmd, MelanoBot $bot, BotData $data)
  180. {
  181. $rcon_data = $this->data($data);
  182. if ( count($cmd->params) == 0 )
  183. {
  184. $bot->say($cmd->channel,$this->out_prefix().$rcon_data->hostname);
  185. return;
  186. }
  187. switch ( $cmd->params[0] )
  188. {
  189. case 'ip':
  190. $bot->say($cmd->channel,$this->out_prefix().$this->comm->write_server);
  191. break;
  192. case 'stats':
  193. $bot->say($cmd->channel,$this->out_prefix().(!empty($rcon_data->stats)?$rcon_data->stats:"No stats are set for this server"));
  194. break;
  195. case 'game':
  196. $gametype = "unknown gametype";
  197. if ( isset($rcon_data->gametype) )
  198. $gametype = Rcon_Communicator::gametype_name($rcon_data->gametype);
  199. $bot->say($cmd->channel,$this->out_prefix()."Playing \00310$gametype\017 on \00304{$rcon_data->map}\017");
  200. break;
  201. case 'status':
  202. $bot->say($cmd->channel,$this->out_prefix().($this->comm->is_connected()?"\00303Server is running\xf":"\00304Server is not connected\xf"),16);
  203. break;
  204. }
  205. }
  206. }