MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / ConKickOrBan

Function ConKickOrBan

src/console_cmds.cpp:676–721  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

674 **********************************/
675
676static bool ConKickOrBan(std::string_view arg, bool ban, std::string_view reason)
677{
678 uint n;
679
680 if (arg.find_first_of(".:") == std::string::npos) { // banning with ID
681 auto client_id = ParseType<ClientID>(arg);
682 if (!client_id.has_value()) {
683 IConsolePrint(CC_ERROR, "The given client-id is not a valid number.");
684 return true;
685 }
686
687 /* Don't kill the server, or the client doing the rcon. The latter can't be kicked because
688 * kicking frees closes and subsequently free the connection related instances, which we
689 * would be reading from and writing to after returning. So we would read or write data
690 * from freed memory up till the segfault triggers. */
691 if (*client_id == CLIENT_ID_SERVER || *client_id == _redirect_console_to_client) {
692 IConsolePrint(CC_ERROR, "You can not {} yourself!", ban ? "ban" : "kick");
693 return true;
694 }
695
696 NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(*client_id);
697 if (ci == nullptr) {
698 IConsolePrint(CC_ERROR, "Invalid client-id.");
699 return true;
700 }
701
702 if (!ban) {
703 /* Kick only this client, not all clients with that IP */
704 NetworkServerKickClient(*client_id, reason);
705 return true;
706 }
707
708 /* When banning, kick+ban all clients with that IP */
709 n = NetworkServerKickOrBanIP(*client_id, ban, reason);
710 } else {
711 n = NetworkServerKickOrBanIP(arg, ban, reason);
712 }
713
714 if (n == 0) {
715 IConsolePrint(CC_DEFAULT, ban ? "Client not online, address added to banlist." : "Client not found.");
716 } else {
717 IConsolePrint(CC_DEFAULT, "{}ed {} client(s).", ban ? "Bann" : "Kick", n);
718 }
719
720 return true;
721}
722
723static bool ConKick(std::span<std::string_view> argv)
724{

Callers 2

ConKickFunction · 0.85
ConBanFunction · 0.85

Calls 3

NetworkServerKickClientFunction · 0.85
NetworkServerKickOrBanIPFunction · 0.85
IConsolePrintFunction · 0.70

Tested by

no test coverage detected