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"
31 #include "convert_json.h"
33 #include <curl/curl.h>
38 std::string getFilePath()
40 std::string serverlist_file = g_settings->get("serverlist_file");
42 std::string dir_path = std::string("client") + DIR_DELIM
43 + "serverlist" + DIR_DELIM;
44 fs::CreateDir(porting::path_user + DIR_DELIM + "client");
45 fs::CreateDir(porting::path_user + DIR_DELIM + dir_path);
46 std::string rel_path = dir_path + serverlist_file;
47 std::string path = porting::path_user + DIR_DELIM + rel_path;
51 std::vector<ServerListSpec> getLocal()
53 std::string path = ServerList::getFilePath();
54 std::string liststring;
55 if(fs::PathExists(path))
57 std::ifstream istream(path.c_str());
60 std::ostringstream ostream;
61 ostream << istream.rdbuf();
62 liststring = ostream.str();
67 return ServerList::deSerialize(liststring);
72 std::vector<ServerListSpec> getOnline()
74 Json::Value root = fetchJsonValue((g_settings->get("serverlist_url")+"/list").c_str(),0);
76 std::vector<ServerListSpec> serverlist;
79 for (unsigned int i = 0; i < root.size(); i++)
81 if (root[i].isObject()) {
82 serverlist.push_back(root[i]);
93 Delete a server fromt he local favorites list
95 bool deleteEntry (ServerListSpec server)
97 std::vector<ServerListSpec> serverlist = ServerList::getLocal();
98 for(unsigned i = 0; i < serverlist.size(); i++)
100 if (serverlist[i]["address"] == server["address"]
101 && serverlist[i]["port"] == server["port"])
103 serverlist.erase(serverlist.begin() + i);
107 std::string path = ServerList::getFilePath();
108 std::ofstream stream (path.c_str());
109 if (stream.is_open())
111 stream<<ServerList::serialize(serverlist);
118 Insert a server to the local favorites list
120 bool insert (ServerListSpec server)
123 ServerList::deleteEntry(server);
125 std::vector<ServerListSpec> serverlist = ServerList::getLocal();
127 // Insert new server at the top of the list
128 serverlist.insert(serverlist.begin(), server);
130 std::string path = ServerList::getFilePath();
131 std::ofstream stream (path.c_str());
132 if (stream.is_open())
134 stream<<ServerList::serialize(serverlist);
140 std::vector<ServerListSpec> deSerialize(std::string liststring)
142 std::vector<ServerListSpec> serverlist;
143 std::istringstream stream(liststring);
144 std::string line, tmp;
145 while (std::getline(stream, line))
147 std::transform(line.begin(), line.end(),line.begin(), ::toupper);
148 if (line == "[SERVER]")
150 ServerListSpec thisserver;
151 std::getline(stream, tmp);
152 thisserver["name"] = tmp;
153 std::getline(stream, tmp);
154 thisserver["address"] = tmp;
155 std::getline(stream, tmp);
156 thisserver["port"] = tmp;
157 std::getline(stream, tmp);
158 thisserver["description"] = tmp;
159 serverlist.push_back(thisserver);
165 std::string serialize(std::vector<ServerListSpec> serverlist)
167 std::string liststring;
168 for(std::vector<ServerListSpec>::iterator i = serverlist.begin(); i != serverlist.end(); i++)
170 liststring += "[server]\n";
171 liststring += (*i)["name"].asString() + "\n";
172 liststring += (*i)["address"].asString() + "\n";
173 liststring += (*i)["port"].asString() + "\n";
174 liststring += (*i)["description"].asString() + "\n";
180 std::string serializeJson(std::vector<ServerListSpec> serverlist)
183 Json::Value list(Json::arrayValue);
184 for(std::vector<ServerListSpec>::iterator i = serverlist.begin(); i != serverlist.end(); i++)
189 Json::StyledWriter writer;
190 return writer.write( root );
195 static size_t ServerAnnounceCallback(void *contents, size_t size, size_t nmemb, void *userp)
197 //((std::string*)userp)->append((char*)contents, size * nmemb);
200 void sendAnnounce(std::string action, u16 clients, double uptime, std::string gameid, std::vector<ModSpec> m_mods) {
203 server["action"] = action;
204 server["port"] = g_settings->get("port");
205 server["address"] = g_settings->get("server_address");
206 if (action != "delete") {
207 server["name"] = g_settings->get("server_name");
208 server["description"] = g_settings->get("server_description");
209 server["version"] = VERSION_STRING;
210 server["url"] = g_settings->get("server_url");
211 server["creative"] = g_settings->get("creative_mode");
212 server["damage"] = g_settings->get("enable_damage");
213 server["dedicated"] = g_settings->get("server_dedicated");
214 server["rollback"] = g_settings->getBool("enable_rollback_recording");
215 server["password"] = g_settings->getBool("disallow_empty_password");
216 server["pvp"] = g_settings->getBool("enable_pvp");
217 server["clients"] = clients;
218 server["clients_max"] = g_settings->get("max_users");
219 if (uptime >=1) server["uptime"] = (int)uptime;
220 if (gameid!="") server["gameid"] = gameid;
223 if(server["action"] == "start") {
224 server["mods"] = Json::Value(Json::arrayValue);
225 for(std::vector<ModSpec>::iterator m = m_mods.begin(); m != m_mods.end(); m++) {
226 server["mods"].append(m->name);
228 actionstream << "announcing to " << g_settings->get("serverlist_url") << std::endl;
231 Json::StyledWriter writer;
233 curl = curl_easy_init();
237 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
238 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());
239 //curl_easy_setopt(curl, CURLOPT_USERAGENT, "minetest");
240 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ServerList::ServerAnnounceCallback);
241 //curl_easy_setopt(curl, CURLOPT_WRITEDATA, &liststring);
242 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1);
243 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 1);
244 res = curl_easy_perform(curl);
246 errorstream<<"Serverlist at url "<<g_settings->get("serverlist_url")<<" error ("<<curl_easy_strerror(res)<<")"<<std::endl;
247 curl_easy_cleanup(curl);
253 } //namespace ServerList