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