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