3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "serverlist.h"
26 #include "network/networkprotocol.h"
27 #include <json/json.h>
28 #include "convert_json.h"
29 #include "httpfetch.h"
34 void sendAnnounce(AnnounceAction action,
36 const std::vector<std::string> &clients_names,
40 const std::string &gameid,
41 const std::string &mg_name,
42 const std::vector<ModSpec> &mods,
45 static const char *aa_names[] = {"start", "update", "delete"};
47 server["action"] = aa_names[action];
48 server["port"] = port;
49 if (g_settings->exists("server_address")) {
50 server["address"] = g_settings->get("server_address");
52 if (action != AA_DELETE) {
53 bool strict_checking = g_settings->getBool("strict_protocol_version_checking");
54 server["name"] = g_settings->get("server_name");
55 server["description"] = g_settings->get("server_description");
56 server["version"] = g_version_string;
57 server["proto_min"] = strict_checking ? LATEST_PROTOCOL_VERSION : SERVER_PROTOCOL_VERSION_MIN;
58 server["proto_max"] = strict_checking ? LATEST_PROTOCOL_VERSION : SERVER_PROTOCOL_VERSION_MAX;
59 server["url"] = g_settings->get("server_url");
60 server["creative"] = g_settings->getBool("creative_mode");
61 server["damage"] = g_settings->getBool("enable_damage");
62 server["password"] = g_settings->getBool("disallow_empty_password");
63 server["pvp"] = g_settings->getBool("enable_pvp");
64 server["uptime"] = (int) uptime;
65 server["game_time"] = game_time;
66 server["clients"] = (int) clients_names.size();
67 server["clients_max"] = g_settings->getU16("max_users");
68 server["clients_list"] = Json::Value(Json::arrayValue);
69 for (const std::string &clients_name : clients_names) {
70 server["clients_list"].append(clients_name);
73 server["gameid"] = gameid;
76 if (action == AA_START) {
77 server["dedicated"] = dedicated;
78 server["rollback"] = g_settings->getBool("enable_rollback_recording");
79 server["mapgen"] = mg_name;
80 server["privs"] = g_settings->get("default_privs");
81 server["can_see_far_names"] = g_settings->getS16("player_transfer_distance") <= 0;
82 server["mods"] = Json::Value(Json::arrayValue);
83 for (const ModSpec &mod : mods) {
84 server["mods"].append(mod.name);
86 } else if (action == AA_UPDATE) {
91 if (action == AA_START) {
92 actionstream << "Announcing " << aa_names[action] << " to " <<
93 g_settings->get("serverlist_url") << std::endl;
95 infostream << "Announcing " << aa_names[action] << " to " <<
96 g_settings->get("serverlist_url") << std::endl;
99 HTTPFetchRequest fetch_request;
100 fetch_request.caller = HTTPFETCH_PRINT_ERR;
101 fetch_request.url = g_settings->get("serverlist_url") + std::string("/announce");
102 fetch_request.method = HTTP_POST;
103 fetch_request.fields["json"] = fastWriteJson(server);
104 fetch_request.multipart = true;
105 httpfetch_async(fetch_request);
109 } // namespace ServerList