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

Function ConNetworkAuthorizedKey

src/console_cmds.cpp:2099–2178  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2097}
2098
2099static bool ConNetworkAuthorizedKey(std::span<std::string_view> argv)
2100{
2101 if (argv.size() <= 2) {
2102 IConsolePrint(CC_HELP, "List and update authorized keys. Usage: 'authorized_key list [type]|add [type] [key]|remove [type] [key]'.");
2103 IConsolePrint(CC_HELP, " list: list all the authorized keys of the given type.");
2104 IConsolePrint(CC_HELP, " add: add the given key to the authorized keys of the given type.");
2105 IConsolePrint(CC_HELP, " remove: remove the given key from the authorized keys of the given type; use 'all' to remove all authorized keys.");
2106 IConsolePrint(CC_HELP, "Instead of a key, use 'client:<id>' to add/remove the key of that given client.");
2107
2108 std::string buffer;
2109 for (auto [name, _] : _console_cmd_authorized_keys) format_append(buffer, ", {}", name);
2110 IConsolePrint(CC_HELP, "The supported types are: all{} and company:<id>.", buffer);
2111 return true;
2112 }
2113
2114 ConNetworkAuthorizedKeyAction action;
2115 std::string_view action_string = argv[1];
2116 if (StrEqualsIgnoreCase(action_string, "list")) {
2117 action = CNAKA_LIST;
2118 } else if (StrEqualsIgnoreCase(action_string, "add")) {
2119 action = CNAKA_ADD;
2120 } else if (StrEqualsIgnoreCase(action_string, "remove") || StrEqualsIgnoreCase(action_string, "delete")) {
2121 action = CNAKA_REMOVE;
2122 } else {
2123 IConsolePrint(CC_WARNING, "No valid action was given.");
2124 return false;
2125 }
2126
2127 std::string authorized_key;
2128 if (action != CNAKA_LIST) {
2129 if (argv.size() <= 3) {
2130 IConsolePrint(CC_ERROR, "You must enter the key.");
2131 return false;
2132 }
2133
2134 authorized_key = argv[3];
2135 if (StrStartsWithIgnoreCase(authorized_key, "client:")) {
2136 auto value = ParseInteger<uint32_t>(authorized_key.substr(7));
2137 if (value.has_value()) authorized_key = NetworkGetPublicKeyOfClient(static_cast<ClientID>(*value));
2138 if (!value.has_value() || authorized_key.empty()) {
2139 IConsolePrint(CC_ERROR, "You must enter a valid client id; see 'clients'.");
2140 return false;
2141 }
2142 }
2143
2144 if (authorized_key.size() != NETWORK_PUBLIC_KEY_LENGTH - 1) {
2145 IConsolePrint(CC_ERROR, "You must enter a valid authorized key.");
2146 return false;
2147 }
2148 }
2149
2150 std::string_view type = argv[2];
2151 if (StrEqualsIgnoreCase(type, "all")) {
2152 for (auto [name, authorized_keys] : _console_cmd_authorized_keys) PerformNetworkAuthorizedKeyAction(name, authorized_keys, action, authorized_key);
2153 for (Company *c : Company::Iterate()) PerformNetworkAuthorizedKeyAction(fmt::format("company:{}", c->index + 1), &c->allow_list, action, authorized_key, c->index);
2154 return true;
2155 }
2156

Callers

nothing calls this directly

Calls 10

format_appendFunction · 0.85
StrEqualsIgnoreCaseFunction · 0.85
StrStartsWithIgnoreCaseFunction · 0.85
substrMethod · 0.80
IConsolePrintFunction · 0.70
formatFunction · 0.50
sizeMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected