Fix bone-attached entities (#10015)
[oweals/minetest.git] / src / serverlist.cpp
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 <fstream>
21 #include <iostream>
22 #include <sstream>
23 #include <algorithm>
24
25 #include "version.h"
26 #include "settings.h"
27 #include "serverlist.h"
28 #include "filesys.h"
29 #include "porting.h"
30 #include "log.h"
31 #include "network/networkprotocol.h"
32 #include <json/json.h>
33 #include "convert_json.h"
34 #include "httpfetch.h"
35 #include "util/string.h"
36
37 namespace ServerList
38 {
39
40 std::string getFilePath()
41 {
42         std::string serverlist_file = g_settings->get("serverlist_file");
43
44         std::string dir_path = "client" DIR_DELIM "serverlist" DIR_DELIM;
45         fs::CreateDir(porting::path_user + DIR_DELIM  "client");
46         fs::CreateDir(porting::path_user + DIR_DELIM + dir_path);
47         return porting::path_user + DIR_DELIM + dir_path + serverlist_file;
48 }
49
50
51 std::vector<ServerListSpec> getLocal()
52 {
53         std::string path = ServerList::getFilePath();
54         std::string liststring;
55         if (fs::PathExists(path)) {
56                 std::ifstream istream(path.c_str());
57                 if (istream.is_open()) {
58                         std::ostringstream ostream;
59                         ostream << istream.rdbuf();
60                         liststring = ostream.str();
61                         istream.close();
62                 }
63         }
64
65         return deSerialize(liststring);
66 }
67
68
69 std::vector<ServerListSpec> getOnline()
70 {
71         std::ostringstream geturl;
72
73         u16 proto_version_min = CLIENT_PROTOCOL_VERSION_MIN;
74
75         geturl << g_settings->get("serverlist_url") <<
76                 "/list?proto_version_min=" << proto_version_min <<
77                 "&proto_version_max=" << CLIENT_PROTOCOL_VERSION_MAX;
78         Json::Value root = fetchJsonValue(geturl.str(), NULL);
79
80         std::vector<ServerListSpec> server_list;
81
82         if (!root.isObject()) {
83                 return server_list;
84         }
85
86         root = root["list"];
87         if (!root.isArray()) {
88                 return server_list;
89         }
90
91         for (const Json::Value &i : root) {
92                 if (i.isObject()) {
93                         server_list.push_back(i);
94                 }
95         }
96
97         return server_list;
98 }
99
100
101 // Delete a server from the local favorites list
102 bool deleteEntry(const ServerListSpec &server)
103 {
104         std::vector<ServerListSpec> serverlist = ServerList::getLocal();
105         for (std::vector<ServerListSpec>::iterator it = serverlist.begin();
106                         it != serverlist.end();) {
107                 if ((*it)["address"] == server["address"] &&
108                                 (*it)["port"] == server["port"]) {
109                         it = serverlist.erase(it);
110                 } else {
111                         ++it;
112                 }
113         }
114
115         std::string path = ServerList::getFilePath();
116         std::ostringstream ss(std::ios_base::binary);
117         ss << ServerList::serialize(serverlist);
118         if (!fs::safeWriteToFile(path, ss.str()))
119                 return false;
120         return true;
121 }
122
123 // Insert a server to the local favorites list
124 bool insert(const ServerListSpec &server)
125 {
126         // Remove duplicates
127         ServerList::deleteEntry(server);
128
129         std::vector<ServerListSpec> serverlist = ServerList::getLocal();
130
131         // Insert new server at the top of the list
132         serverlist.insert(serverlist.begin(), server);
133
134         std::string path = ServerList::getFilePath();
135         std::ostringstream ss(std::ios_base::binary);
136         ss << ServerList::serialize(serverlist);
137         if (!fs::safeWriteToFile(path, ss.str()))
138                 return false;
139
140         return true;
141 }
142
143 std::vector<ServerListSpec> deSerialize(const std::string &liststring)
144 {
145         std::vector<ServerListSpec> serverlist;
146         std::istringstream stream(liststring);
147         std::string line, tmp;
148         while (std::getline(stream, line)) {
149                 std::transform(line.begin(), line.end(), line.begin(), ::toupper);
150                 if (line == "[SERVER]") {
151                         ServerListSpec server;
152                         std::getline(stream, tmp);
153                         server["name"] = tmp;
154                         std::getline(stream, tmp);
155                         server["address"] = tmp;
156                         std::getline(stream, tmp);
157                         server["port"] = tmp;
158                         bool unique = true;
159                         for (const ServerListSpec &added : serverlist) {
160                                 if (server["name"] == added["name"]
161                                                 && server["port"] == added["port"]) {
162                                         unique = false;
163                                         break;
164                                 }
165                         }
166                         if (!unique)
167                                 continue;
168                         std::getline(stream, tmp);
169                         server["description"] = tmp;
170                         serverlist.push_back(server);
171                 }
172         }
173         return serverlist;
174 }
175
176 const std::string serialize(const std::vector<ServerListSpec> &serverlist)
177 {
178         std::string liststring;
179         for (const ServerListSpec &it : serverlist) {
180                 liststring += "[server]\n";
181                 liststring += it["name"].asString() + '\n';
182                 liststring += it["address"].asString() + '\n';
183                 liststring += it["port"].asString() + '\n';
184                 liststring += it["description"].asString() + '\n';
185                 liststring += '\n';
186         }
187         return liststring;
188 }
189
190 const std::string serializeJson(const std::vector<ServerListSpec> &serverlist)
191 {
192         Json::Value root;
193         Json::Value list(Json::arrayValue);
194         for (const ServerListSpec &it : serverlist) {
195                 list.append(it);
196         }
197         root["list"] = list;
198
199         return fastWriteJson(root);
200 }
201
202
203 #if USE_CURL
204 void sendAnnounce(AnnounceAction action,
205                 const u16 port,
206                 const std::vector<std::string> &clients_names,
207                 const double uptime,
208                 const u32 game_time,
209                 const float lag,
210                 const std::string &gameid,
211                 const std::string &mg_name,
212                 const std::vector<ModSpec> &mods,
213                 bool dedicated)
214 {
215         static const char *aa_names[] = {"start", "update", "delete"};
216         Json::Value server;
217         server["action"] = aa_names[action];
218         server["port"] = port;
219         if (g_settings->exists("server_address")) {
220                 server["address"] = g_settings->get("server_address");
221         }
222         if (action != AA_DELETE) {
223                 bool strict_checking = g_settings->getBool("strict_protocol_version_checking");
224                 server["name"]         = g_settings->get("server_name");
225                 server["description"]  = g_settings->get("server_description");
226                 server["version"]      = g_version_string;
227                 server["proto_min"]    = strict_checking ? LATEST_PROTOCOL_VERSION : SERVER_PROTOCOL_VERSION_MIN;
228                 server["proto_max"]    = strict_checking ? LATEST_PROTOCOL_VERSION : SERVER_PROTOCOL_VERSION_MAX;
229                 server["url"]          = g_settings->get("server_url");
230                 server["creative"]     = g_settings->getBool("creative_mode");
231                 server["damage"]       = g_settings->getBool("enable_damage");
232                 server["password"]     = g_settings->getBool("disallow_empty_password");
233                 server["pvp"]          = g_settings->getBool("enable_pvp");
234                 server["uptime"]       = (int) uptime;
235                 server["game_time"]    = game_time;
236                 server["clients"]      = (int) clients_names.size();
237                 server["clients_max"]  = g_settings->getU16("max_users");
238                 server["clients_list"] = Json::Value(Json::arrayValue);
239                 for (const std::string &clients_name : clients_names) {
240                         server["clients_list"].append(clients_name);
241                 }
242                 if (!gameid.empty())
243                         server["gameid"] = gameid;
244         }
245
246         if (action == AA_START) {
247                 server["dedicated"]         = dedicated;
248                 server["rollback"]          = g_settings->getBool("enable_rollback_recording");
249                 server["mapgen"]            = mg_name;
250                 server["privs"]             = g_settings->get("default_privs");
251                 server["can_see_far_names"] = g_settings->getS16("player_transfer_distance") <= 0;
252                 server["mods"]              = Json::Value(Json::arrayValue);
253                 for (const ModSpec &mod : mods) {
254                         server["mods"].append(mod.name);
255                 }
256                 actionstream << "Announcing to " << g_settings->get("serverlist_url") << std::endl;
257         } else if (action == AA_UPDATE) {
258                 if (lag)
259                         server["lag"] = lag;
260         }
261
262         HTTPFetchRequest fetch_request;
263         fetch_request.url = g_settings->get("serverlist_url") + std::string("/announce");
264         fetch_request.post_fields["json"] = fastWriteJson(server);
265         fetch_request.multipart = true;
266         httpfetch_async(fetch_request);
267 }
268 #endif
269
270 } // namespace ServerList
271