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.
27 #include "serverlist.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"
40 std::string getFilePath()
42 std::string serverlist_file = g_settings->get("serverlist_file");
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;
51 std::vector<ServerListSpec> getLocal()
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();
65 return deSerialize(liststring);
69 std::vector<ServerListSpec> getOnline()
71 std::ostringstream geturl;
73 u16 proto_version_min = CLIENT_PROTOCOL_VERSION_MIN;
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);
80 std::vector<ServerListSpec> server_list;
82 if (!root.isObject()) {
87 if (!root.isArray()) {
91 for (const Json::Value &i : root) {
93 server_list.push_back(i);
101 // Delete a server from the local favorites list
102 bool deleteEntry(const ServerListSpec &server)
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);
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()))
123 // Insert a server to the local favorites list
124 bool insert(const ServerListSpec &server)
127 ServerList::deleteEntry(server);
129 std::vector<ServerListSpec> serverlist = ServerList::getLocal();
131 // Insert new server at the top of the list
132 serverlist.insert(serverlist.begin(), server);
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()))
143 std::vector<ServerListSpec> deSerialize(const std::string &liststring)
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 std::getline(stream, tmp);
159 server["description"] = tmp;
160 serverlist.push_back(server);
166 const std::string serialize(const std::vector<ServerListSpec> &serverlist)
168 std::string liststring;
169 for (const ServerListSpec &it : serverlist) {
170 liststring += "[server]\n";
171 liststring += it["name"].asString() + '\n';
172 liststring += it["address"].asString() + '\n';
173 liststring += it["port"].asString() + '\n';
174 liststring += it["description"].asString() + '\n';
180 const std::string serializeJson(const std::vector<ServerListSpec> &serverlist)
183 Json::Value list(Json::arrayValue);
184 for (const ServerListSpec &it : serverlist) {
189 return fastWriteJson(root);
194 void sendAnnounce(AnnounceAction action,
196 const std::vector<std::string> &clients_names,
200 const std::string &gameid,
201 const std::string &mg_name,
202 const std::vector<ModSpec> &mods,
205 static const char *aa_names[] = {"start", "update", "delete"};
207 server["action"] = aa_names[action];
208 server["port"] = port;
209 if (g_settings->exists("server_address")) {
210 server["address"] = g_settings->get("server_address");
212 if (action != AA_DELETE) {
213 bool strict_checking = g_settings->getBool("strict_protocol_version_checking");
214 server["name"] = g_settings->get("server_name");
215 server["description"] = g_settings->get("server_description");
216 server["version"] = g_version_string;
217 server["proto_min"] = strict_checking ? LATEST_PROTOCOL_VERSION : SERVER_PROTOCOL_VERSION_MIN;
218 server["proto_max"] = strict_checking ? LATEST_PROTOCOL_VERSION : SERVER_PROTOCOL_VERSION_MAX;
219 server["url"] = g_settings->get("server_url");
220 server["creative"] = g_settings->getBool("creative_mode");
221 server["damage"] = g_settings->getBool("enable_damage");
222 server["password"] = g_settings->getBool("disallow_empty_password");
223 server["pvp"] = g_settings->getBool("enable_pvp");
224 server["uptime"] = (int) uptime;
225 server["game_time"] = game_time;
226 server["clients"] = (int) clients_names.size();
227 server["clients_max"] = g_settings->getU16("max_users");
228 server["clients_list"] = Json::Value(Json::arrayValue);
229 for (const std::string &clients_name : clients_names) {
230 server["clients_list"].append(clients_name);
233 server["gameid"] = gameid;
236 if (action == AA_START) {
237 server["dedicated"] = dedicated;
238 server["rollback"] = g_settings->getBool("enable_rollback_recording");
239 server["mapgen"] = mg_name;
240 server["privs"] = g_settings->get("default_privs");
241 server["can_see_far_names"] = g_settings->getS16("player_transfer_distance") <= 0;
242 server["mods"] = Json::Value(Json::arrayValue);
243 for (const ModSpec &mod : mods) {
244 server["mods"].append(mod.name);
246 actionstream << "Announcing to " << g_settings->get("serverlist_url") << std::endl;
247 } else if (action == AA_UPDATE) {
252 HTTPFetchRequest fetch_request;
253 fetch_request.url = g_settings->get("serverlist_url") + std::string("/announce");
254 fetch_request.post_fields["json"] = fastWriteJson(server);
255 fetch_request.multipart = true;
256 httpfetch_async(fetch_request);
260 } // namespace ServerList