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.
24 #include "main.h" // for g_settings
26 #include "serverlist.h"
30 #include "json/json.h"
32 #include <curl/curl.h>
37 std::string getFilePath()
39 std::string serverlist_file = g_settings->get("serverlist_file");
41 std::string rel_path = std::string("client") + DIR_DELIM
42 + "serverlist" + DIR_DELIM
44 std::string path = porting::path_share + DIR_DELIM + rel_path;
48 std::vector<ServerListSpec> getLocal()
50 std::string path = ServerList::getFilePath();
51 std::string liststring;
52 if(fs::PathExists(path))
54 std::ifstream istream(path.c_str(), std::ios::binary);
57 std::ostringstream ostream;
58 ostream << istream.rdbuf();
59 liststring = ostream.str();
64 return ServerList::deSerialize(liststring);
70 static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
72 ((std::string*)userp)->append((char*)contents, size * nmemb);
77 std::vector<ServerListSpec> getOnline()
79 std::string liststring;
82 curl = curl_easy_init();
87 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
88 curl_easy_setopt(curl, CURLOPT_URL, (g_settings->get("serverlist_url")+"/list").c_str());
89 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ServerList::WriteCallback);
90 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &liststring);
92 res = curl_easy_perform(curl);
94 errorstream<<"Serverlist at url "<<g_settings->get("serverlist_url")<<" not found (internet connection?)"<<std::endl;
95 curl_easy_cleanup(curl);
97 return ServerList::deSerializeJson(liststring);
103 Delete a server fromt he local favorites list
105 bool deleteEntry (ServerListSpec server)
107 std::vector<ServerListSpec> serverlist = ServerList::getLocal();
108 for(unsigned i = 0; i < serverlist.size(); i++)
110 if (serverlist[i]["address"] == server["address"]
111 && serverlist[i]["port"] == server["port"])
113 serverlist.erase(serverlist.begin() + i);
117 std::string path = ServerList::getFilePath();
118 std::ofstream stream (path.c_str());
119 if (stream.is_open())
121 stream<<ServerList::serialize(serverlist);
128 Insert a server to the local favorites list
130 bool insert (ServerListSpec server)
133 ServerList::deleteEntry(server);
135 std::vector<ServerListSpec> serverlist = ServerList::getLocal();
137 // Insert new server at the top of the list
138 serverlist.insert(serverlist.begin(), server);
140 std::string path = ServerList::getFilePath();
141 std::ofstream stream (path.c_str());
142 if (stream.is_open())
144 stream<<ServerList::serialize(serverlist);
150 std::vector<ServerListSpec> deSerialize(std::string liststring)
152 std::vector<ServerListSpec> serverlist;
153 std::istringstream stream(liststring);
154 std::string line, tmp;
155 while (std::getline(stream, line))
157 std::transform(line.begin(), line.end(),line.begin(), ::toupper);
158 if (line == "[SERVER]")
160 ServerListSpec thisserver;
161 std::getline(stream, tmp);
162 thisserver["name"] = tmp;
163 std::getline(stream, tmp);
164 thisserver["address"] = tmp;
165 std::getline(stream, tmp);
166 thisserver["port"] = tmp;
167 std::getline(stream, tmp);
168 thisserver["description"] = tmp;
169 serverlist.push_back(thisserver);
175 std::string serialize(std::vector<ServerListSpec> serverlist)
177 std::string liststring;
178 for(std::vector<ServerListSpec>::iterator i = serverlist.begin(); i != serverlist.end(); i++)
180 liststring += "[server]\n";
181 liststring += (*i)["name"].asString() + "\n";
182 liststring += (*i)["address"].asString() + "\n";
183 liststring += (*i)["port"].asString() + "\n";
184 liststring += (*i)["description"].asString() + "\n";
190 std::vector<ServerListSpec> deSerializeJson(std::string liststring)
192 std::vector<ServerListSpec> serverlist;
195 std::istringstream stream(liststring);
196 if (!liststring.size()) {
199 if (!reader.parse( stream, root ) )
201 errorstream << "Failed to parse server list " << reader.getFormattedErrorMessages();
204 if (root["list"].isArray())
205 for (unsigned int i = 0; i < root["list"].size(); i++)
207 if (root["list"][i].isObject()) {
208 serverlist.push_back(root["list"][i]);
214 std::string serializeJson(std::vector<ServerListSpec> serverlist)
217 Json::Value list(Json::arrayValue);
218 for(std::vector<ServerListSpec>::iterator i = serverlist.begin(); i != serverlist.end(); i++)
223 Json::StyledWriter writer;
224 return writer.write( root );
229 static size_t ServerAnnounceCallback(void *contents, size_t size, size_t nmemb, void *userp)
232 //((std::string*)userp)->append((char*)contents, size * nmemb);
233 //return size * nmemb;
235 void sendAnnounce(std::string action, u16 clients) {
238 server["action"] = action;
239 server["port"] = g_settings->get("port");
240 if (action != "del") {
241 server["name"] = g_settings->get("server_name");
242 server["description"] = g_settings->get("server_description");
243 server["address"] = g_settings->get("server_address");
244 server["version"] = VERSION_STRING;
245 server["url"] = g_settings->get("server_url");
246 server["creative"] = g_settings->get("creative_mode");
247 server["damage"] = g_settings->get("enable_damage");
248 server["dedicated"] = g_settings->get("server_dedicated");
249 server["password"] = g_settings->getBool("disallow_empty_password");
250 server["pvp"] = g_settings->getBool("enable_pvp");
251 server["clients"] = clients;
252 server["clients_max"] = g_settings->get("max_users");
254 if(server["action"] == "start")
255 actionstream << "announcing to " << g_settings->get("serverlist_url") << std::endl;
256 Json::StyledWriter writer;
258 curl = curl_easy_init();
262 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
263 curl_easy_setopt(curl, CURLOPT_URL, (g_settings->get("serverlist_url")+std::string("/announce?json=")+curl_easy_escape(curl, writer.write( server ).c_str(), 0)).c_str());
264 //curl_easy_setopt(curl, CURLOPT_USERAGENT, "minetest");
265 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ServerList::ServerAnnounceCallback);
266 //curl_easy_setopt(curl, CURLOPT_WRITEDATA, &liststring);
267 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1);
268 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 1);
269 res = curl_easy_perform(curl);
270 //if (res != CURLE_OK)
271 // errorstream<<"Serverlist at url "<<g_settings->get("serverlist_url")<<" not found (internet connection?)"<<std::endl;
272 curl_easy_cleanup(curl);
278 } //namespace ServerList