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;
72 geturl << g_settings->get("serverlist_url") <<
73 "/list?proto_version_min=" << CLIENT_PROTOCOL_VERSION_MIN <<
74 "&proto_version_max=" << CLIENT_PROTOCOL_VERSION_MAX;
75 Json::Value root = fetchJsonValue(geturl.str(), NULL);
77 std::vector<ServerListSpec> server_list;
79 if (!root.isObject()) {
84 if (!root.isArray()) {
88 for (unsigned int i = 0; i < root.size(); i++) {
89 if (root[i].isObject()) {
90 server_list.push_back(root[i]);
98 // Delete a server from the local favorites list
99 bool deleteEntry(const ServerListSpec &server)
101 std::vector<ServerListSpec> serverlist = ServerList::getLocal();
102 for (std::vector<ServerListSpec>::iterator it = serverlist.begin();
103 it != serverlist.end();) {
104 if ((*it)["address"] == server["address"] &&
105 (*it)["port"] == server["port"]) {
106 it = serverlist.erase(it);
112 std::string path = ServerList::getFilePath();
113 std::ostringstream ss(std::ios_base::binary);
114 ss << ServerList::serialize(serverlist);
115 if (!fs::safeWriteToFile(path, ss.str()))
120 // Insert a server to the local favorites list
121 bool insert(const ServerListSpec &server)
124 ServerList::deleteEntry(server);
126 std::vector<ServerListSpec> serverlist = ServerList::getLocal();
128 // Insert new server at the top of the list
129 serverlist.insert(serverlist.begin(), server);
131 std::string path = ServerList::getFilePath();
132 std::ostringstream ss(std::ios_base::binary);
133 ss << ServerList::serialize(serverlist);
134 if (!fs::safeWriteToFile(path, ss.str()))
140 std::vector<ServerListSpec> deSerialize(const std::string &liststring)
142 std::vector<ServerListSpec> serverlist;
143 std::istringstream stream(liststring);
144 std::string line, tmp;
145 while (std::getline(stream, line)) {
146 std::transform(line.begin(), line.end(), line.begin(), ::toupper);
147 if (line == "[SERVER]") {
148 ServerListSpec server;
149 std::getline(stream, tmp);
150 server["name"] = tmp;
151 std::getline(stream, tmp);
152 server["address"] = tmp;
153 std::getline(stream, tmp);
154 server["port"] = tmp;
155 std::getline(stream, tmp);
156 server["description"] = tmp;
157 serverlist.push_back(server);
163 const std::string serialize(const std::vector<ServerListSpec> &serverlist)
165 std::string liststring;
166 for (std::vector<ServerListSpec>::const_iterator it = serverlist.begin();
167 it != serverlist.end();
169 liststring += "[server]\n";
170 liststring += (*it)["name"].asString() + '\n';
171 liststring += (*it)["address"].asString() + '\n';
172 liststring += (*it)["port"].asString() + '\n';
173 liststring += (*it)["description"].asString() + '\n';
179 const std::string serializeJson(const std::vector<ServerListSpec> &serverlist)
182 Json::Value list(Json::arrayValue);
183 for (std::vector<ServerListSpec>::const_iterator it = serverlist.begin();
184 it != serverlist.end();
189 Json::FastWriter writer;
190 return writer.write(root);
195 void sendAnnounce(const std::string &action,
197 const std::vector<std::string> &clients_names,
201 const std::string &gameid,
202 const std::string &mg_name,
203 const std::vector<ModSpec> &mods)
206 server["action"] = action;
207 server["port"] = port;
208 if (g_settings->exists("server_address")) {
209 server["address"] = g_settings->get("server_address");
211 if (action != "delete") {
212 bool strict_checking = g_settings->getBool("strict_protocol_version_checking");
213 server["name"] = g_settings->get("server_name");
214 server["description"] = g_settings->get("server_description");
215 server["version"] = g_version_string;
216 server["proto_min"] = strict_checking ? LATEST_PROTOCOL_VERSION : SERVER_PROTOCOL_VERSION_MIN;
217 server["proto_max"] = strict_checking ? LATEST_PROTOCOL_VERSION : SERVER_PROTOCOL_VERSION_MAX;
218 server["url"] = g_settings->get("server_url");
219 server["creative"] = g_settings->getBool("creative_mode");
220 server["damage"] = g_settings->getBool("enable_damage");
221 server["password"] = g_settings->getBool("disallow_empty_password");
222 server["pvp"] = g_settings->getBool("enable_pvp");
223 server["uptime"] = (int) uptime;
224 server["game_time"] = game_time;
225 server["clients"] = (int) clients_names.size();
226 server["clients_max"] = g_settings->getU16("max_users");
227 server["clients_list"] = Json::Value(Json::arrayValue);
228 for (std::vector<std::string>::const_iterator it = clients_names.begin();
229 it != clients_names.end();
231 server["clients_list"].append(*it);
233 if (gameid != "") server["gameid"] = gameid;
236 if (action == "start") {
237 server["dedicated"] = g_settings->getBool("server_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 (std::vector<ModSpec>::const_iterator it = mods.begin();
246 server["mods"].append(it->name);
248 actionstream << "Announcing to " << g_settings->get("serverlist_url") << std::endl;
254 Json::FastWriter writer;
255 HTTPFetchRequest fetch_request;
256 fetch_request.url = g_settings->get("serverlist_url") + std::string("/announce");
257 fetch_request.post_fields["json"] = writer.write(server);
258 fetch_request.multipart = true;
259 httpfetch_async(fetch_request);
263 } //namespace ServerList