]> git.lizzy.rs Git - minetest.git/blob - src/serverlist.cpp
new auto masterserver
[minetest.git] / src / serverlist.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2011 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 <iostream>
21 #include <sstream>
22 #include <algorithm>
23
24 #include "main.h" // for g_settings
25 #include "settings.h"
26 #include "serverlist.h"
27 #include "filesys.h"
28 #include "porting.h"
29 #include "log.h"
30 #include "json/json.h"
31 #if USE_CURL
32 #include <curl/curl.h>
33 #endif
34
35 namespace ServerList
36 {
37 std::string getFilePath()
38 {
39         std::string serverlist_file = g_settings->get("serverlist_file");
40
41         std::string rel_path = std::string("client") + DIR_DELIM
42                 + "serverlist" + DIR_DELIM
43                 + serverlist_file;
44         std::string path = porting::path_share + DIR_DELIM + rel_path;
45         return path;
46 }
47
48 std::vector<ServerListSpec> getLocal()
49 {
50         std::string path = ServerList::getFilePath();
51         std::string liststring;
52         if(fs::PathExists(path))
53         {
54                 std::ifstream istream(path.c_str(), std::ios::binary);
55                 if(istream.is_open())
56                 {
57                         std::ostringstream ostream;
58                         ostream << istream.rdbuf();
59                         liststring = ostream.str();
60                         istream.close();
61                 }
62         }
63
64         return ServerList::deSerialize(liststring);
65 }
66
67
68 #if USE_CURL
69
70 static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
71 {
72     ((std::string*)userp)->append((char*)contents, size * nmemb);
73     return size * nmemb;
74 }
75
76
77 std::vector<ServerListSpec> getOnline()
78 {
79         std::string liststring;
80         CURL *curl;
81
82         curl = curl_easy_init();
83         if (curl)
84         {
85                 CURLcode res;
86
87                 curl_easy_setopt(curl, CURLOPT_URL, (g_settings->get("serverlist_url")+"/list").c_str());
88                 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ServerList::WriteCallback);
89                 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &liststring);
90
91                 res = curl_easy_perform(curl);
92                 if (res != CURLE_OK)
93                         errorstream<<"Serverlist at url "<<g_settings->get("serverlist_url")<<" not found (internet connection?)"<<std::endl;
94                 curl_easy_cleanup(curl);
95         }
96         return ServerList::deSerializeJson(liststring);
97 }
98
99 #endif
100
101 /*
102         Delete a server fromt he local favorites list
103 */
104 bool deleteEntry (ServerListSpec server)
105 {
106         std::vector<ServerListSpec> serverlist = ServerList::getLocal();
107         for(unsigned i = 0; i < serverlist.size(); i++)
108         {
109                 if  (serverlist[i]["address"] == server["address"]
110                 &&   serverlist[i]["port"]    == server["port"])
111                 {
112                         serverlist.erase(serverlist.begin() + i);
113                 }
114         }
115
116         std::string path = ServerList::getFilePath();
117         std::ofstream stream (path.c_str());
118         if (stream.is_open())
119         {
120                 stream<<ServerList::serialize(serverlist);
121                 return true;
122         }
123         return false;
124 }
125
126 /*
127         Insert a server to the local favorites list
128 */
129 bool insert (ServerListSpec server)
130 {
131         // Remove duplicates
132         ServerList::deleteEntry(server);
133
134         std::vector<ServerListSpec> serverlist = ServerList::getLocal();
135
136         // Insert new server at the top of the list
137         serverlist.insert(serverlist.begin(), server);
138
139         std::string path = ServerList::getFilePath();
140         std::ofstream stream (path.c_str());
141         if (stream.is_open())
142         {
143                 stream<<ServerList::serialize(serverlist);
144         }
145
146         return false;
147 }
148
149 std::vector<ServerListSpec> deSerialize(std::string liststring)
150 {
151         std::vector<ServerListSpec> serverlist;
152         std::istringstream stream(liststring);
153         std::string line, tmp;
154         while (std::getline(stream, line))
155         {
156                 std::transform(line.begin(), line.end(),line.begin(), ::toupper);
157                 if (line == "[SERVER]")
158                 {
159                         ServerListSpec thisserver;
160                         std::getline(stream, tmp);
161                         thisserver["name"] = tmp;
162                         std::getline(stream, tmp);
163                         thisserver["address"] = tmp;
164                         std::getline(stream, tmp);
165                         thisserver["port"] = tmp;
166                         std::getline(stream, tmp);
167                         thisserver["description"] = tmp;
168                         serverlist.push_back(thisserver);
169                 }
170         }
171         return serverlist;
172 }
173
174 std::string serialize(std::vector<ServerListSpec> serverlist)
175 {
176         std::string liststring;
177         for(std::vector<ServerListSpec>::iterator i = serverlist.begin(); i != serverlist.end(); i++)
178         {
179                 liststring += "[server]\n";
180                 liststring += (*i)["name"].asString() + "\n";
181                 liststring += (*i)["address"].asString() + "\n";
182                 liststring += (*i)["port"].asString() + "\n";
183                 liststring += (*i)["description"].asString() + "\n";
184                 liststring += "\n";
185         }
186         return liststring;
187 }
188
189 std::vector<ServerListSpec> deSerializeJson(std::string liststring)
190 {
191         std::vector<ServerListSpec> serverlist;
192         Json::Value root;
193         Json::Reader reader;
194         std::istringstream stream(liststring);
195         if (!liststring.size()) {
196                 return serverlist;
197         }
198         if (!reader.parse( stream, root ) )
199         {
200                 errorstream  << "Failed to parse server list " << reader.getFormattedErrorMessages();
201                 return serverlist;
202         }
203         if (root["list"].isArray())
204             for (unsigned int i = 0; i < root["list"].size(); i++)
205         {
206                 if (root["list"][i].isObject()) {
207                         serverlist.push_back(root["list"][i]);
208                 }
209         }
210         return serverlist;
211 }
212
213 std::string serializeJson(std::vector<ServerListSpec> serverlist)
214 {
215         Json::Value root;
216         Json::Value list(Json::arrayValue);
217         for(std::vector<ServerListSpec>::iterator i = serverlist.begin(); i != serverlist.end(); i++)
218         {
219                 list.append(*i);
220         }
221         root["list"] = list;
222         Json::StyledWriter writer;
223         return writer.write( root );
224 }
225
226
227 #if USE_CURL
228 static size_t ServerAnnounceCallback(void *contents, size_t size, size_t nmemb, void *userp)
229 {
230     return 0;
231     //((std::string*)userp)->append((char*)contents, size * nmemb);
232     //return size * nmemb;
233 }
234 void sendAnnounce(std::string action, u16 clients) {
235         Json::Value server;
236         if (action.size())
237                 server["action"]        = action;
238         server["port"] = g_settings->get("port");
239         if (action != "del") {
240                 server["name"]          = g_settings->get("server_name");
241                 server["description"]   = g_settings->get("server_description");
242                 server["address"]       = g_settings->get("server_address");
243                 server["version"]       = VERSION_STRING;
244                 server["url"]           = g_settings->get("server_url");
245                 server["creative"]      = g_settings->get("creative_mode");
246                 server["damage"]        = g_settings->get("enable_damage");
247                 server["dedicated"]     = g_settings->get("server_dedicated");
248                 server["password"]      = g_settings->getBool("disallow_empty_password");
249                 server["pvp"]           = g_settings->getBool("enable_pvp");
250                 server["clients"]       = clients;
251                 server["clients_max"]   = g_settings->get("max_users");
252         }
253         if(server["action"] == "start")
254                 actionstream << "announcing to " << g_settings->get("serverlist_url") << std::endl;
255         Json::StyledWriter writer;
256         CURL *curl;
257         curl = curl_easy_init();
258         if (curl)
259         {
260                 CURLcode res;
261                 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());
262                 //curl_easy_setopt(curl, CURLOPT_USERAGENT, "minetest");
263                 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ServerList::ServerAnnounceCallback);
264                 //curl_easy_setopt(curl, CURLOPT_WRITEDATA, &liststring);
265                 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1);
266                 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 1);
267                 res = curl_easy_perform(curl);
268                 //if (res != CURLE_OK)
269                 //      errorstream<<"Serverlist at url "<<g_settings->get("serverlist_url")<<" not found (internet connection?)"<<std::endl;
270                 curl_easy_cleanup(curl);
271         }
272
273 }
274 #endif
275
276 } //namespace ServerList