1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 
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.
9 
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.
14 
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.
18 */
19 
20 #include <iostream>
21 #include "version.h"
22 #include "settings.h"
23 #include "serverlist.h"
24 #include "filesys.h"
25 #include "log.h"
26 #include "network/networkprotocol.h"
27 #include <json/json.h>
28 #include "convert_json.h"
29 #include "httpfetch.h"
30 
31 namespace ServerList
32 {
33 #if USE_CURL
sendAnnounce(AnnounceAction action,const u16 port,const std::vector<std::string> & clients_names,const double uptime,const u32 game_time,const float lag,const std::string & gameid,const std::string & mg_name,const std::vector<ModSpec> & mods,bool dedicated)34 void sendAnnounce(AnnounceAction action,
35 		const u16 port,
36 		const std::vector<std::string> &clients_names,
37 		const double uptime,
38 		const u32 game_time,
39 		const float lag,
40 		const std::string &gameid,
41 		const std::string &mg_name,
42 		const std::vector<ModSpec> &mods,
43 		bool dedicated)
44 {
45 	static const char *aa_names[] = {"start", "update", "delete"};
46 	Json::Value server;
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");
51 	}
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);
71 		}
72 		if (!gameid.empty())
73 			server["gameid"] = gameid;
74 	}
75 
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);
85 		}
86 	} else if (action == AA_UPDATE) {
87 		if (lag)
88 			server["lag"] = lag;
89 	}
90 
91 	if (action == AA_START) {
92 		actionstream << "Announcing " << aa_names[action] << " to " <<
93 			g_settings->get("serverlist_url") << std::endl;
94 	} else {
95 		infostream << "Announcing " << aa_names[action] << " to " <<
96 			g_settings->get("serverlist_url") << std::endl;
97 	}
98 
99 	HTTPFetchRequest fetch_request;
100 	fetch_request.url = g_settings->get("serverlist_url") + std::string("/announce");
101 	fetch_request.method = HTTP_POST;
102 	fetch_request.fields["json"] = fastWriteJson(server);
103 	fetch_request.multipart = true;
104 	httpfetch_async(fetch_request);
105 }
106 #endif
107 
108 } // namespace ServerList
109