]> git.lizzy.rs Git - minetest.git/blob - src/serverlist.cpp
Translated using Weblate (German)
[minetest.git] / src / serverlist.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 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_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);
91
92                 res = curl_easy_perform(curl);
93                 if (res != CURLE_OK)
94                         errorstream<<"Serverlist at url "<<g_settings->get("serverlist_url")<<" not found (internet connection?)"<<std::endl;
95                 curl_easy_cleanup(curl);
96         }
97         return ServerList::deSerializeJson(liststring);
98 }
99
100 #endif
101
102 /*
103         Delete a server fromt he local favorites list
104 */
105 bool deleteEntry (ServerListSpec server)
106 {
107         std::vector<ServerListSpec> serverlist = ServerList::getLocal();
108         for(unsigned i = 0; i < serverlist.size(); i++)
109         {
110                 if  (serverlist[i]["address"] == server["address"]
111                 &&   serverlist[i]["port"]    == server["port"])
112                 {
113                         serverlist.erase(serverlist.begin() + i);
114                 }
115         }
116
117         std::string path = ServerList::getFilePath();
118         std::ofstream stream (path.c_str());
119         if (stream.is_open())
120         {
121                 stream<<ServerList::serialize(serverlist);
122                 return true;
123         }
124         return false;
125 }
126
127 /*
128         Insert a server to the local favorites list
129 */
130 bool insert (ServerListSpec server)
131 {
132         // Remove duplicates
133         ServerList::deleteEntry(server);
134
135         std::vector<ServerListSpec> serverlist = ServerList::getLocal();
136
137         // Insert new server at the top of the list
138         serverlist.insert(serverlist.begin(), server);
139
140         std::string path = ServerList::getFilePath();
141         std::ofstream stream (path.c_str());
142         if (stream.is_open())
143         {
144                 stream<<ServerList::serialize(serverlist);
145         }
146
147         return false;
148 }
149
150 std::vector<ServerListSpec> deSerialize(std::string liststring)
151 {
152         std::vector<ServerListSpec> serverlist;
153         std::istringstream stream(liststring);
154         std::string line, tmp;
155         while (std::getline(stream, line))
156         {
157                 std::transform(line.begin(), line.end(),line.begin(), ::toupper);
158                 if (line == "[SERVER]")
159                 {
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);
170                 }
171         }
172         return serverlist;
173 }
174
175 std::string serialize(std::vector<ServerListSpec> serverlist)
176 {
177         std::string liststring;
178         for(std::vector<ServerListSpec>::iterator i = serverlist.begin(); i != serverlist.end(); i++)
179         {
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";
185                 liststring += "\n";
186         }
187         return liststring;
188 }
189
190 std::vector<ServerListSpec> deSerializeJson(std::string liststring)
191 {
192         std::vector<ServerListSpec> serverlist;
193         Json::Value root;
194         Json::Reader reader;
195         std::istringstream stream(liststring);
196         if (!liststring.size()) {
197                 return serverlist;
198         }
199         if (!reader.parse( stream, root ) )
200         {
201                 errorstream  << "Failed to parse server list " << reader.getFormattedErrorMessages();
202                 return serverlist;
203         }
204         if (root["list"].isArray())
205             for (unsigned int i = 0; i < root["list"].size(); i++)
206         {
207                 if (root["list"][i].isObject()) {
208                         serverlist.push_back(root["list"][i]);
209                 }
210         }
211         return serverlist;
212 }
213
214 std::string serializeJson(std::vector<ServerListSpec> serverlist)
215 {
216         Json::Value root;
217         Json::Value list(Json::arrayValue);
218         for(std::vector<ServerListSpec>::iterator i = serverlist.begin(); i != serverlist.end(); i++)
219         {
220                 list.append(*i);
221         }
222         root["list"] = list;
223         Json::StyledWriter writer;
224         return writer.write( root );
225 }
226
227
228 #if USE_CURL
229 static size_t ServerAnnounceCallback(void *contents, size_t size, size_t nmemb, void *userp)
230 {
231     return 0;
232     //((std::string*)userp)->append((char*)contents, size * nmemb);
233     //return size * nmemb;
234 }
235 void sendAnnounce(std::string action, u16 clients, double uptime, std::string gameid) {
236         Json::Value server;
237         if (action.size())
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");
253                 if (uptime >=1) server["uptime"] = (int)uptime;
254                 if (gameid!="") server["gameid"] = gameid;
255                 
256         }
257         if(server["action"] == "start")
258                 actionstream << "announcing to " << g_settings->get("serverlist_url") << std::endl;
259         Json::StyledWriter writer;
260         CURL *curl;
261         curl = curl_easy_init();
262         if (curl)
263         {
264                 CURLcode res;
265                 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
266                 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());
267                 //curl_easy_setopt(curl, CURLOPT_USERAGENT, "minetest");
268                 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ServerList::ServerAnnounceCallback);
269                 //curl_easy_setopt(curl, CURLOPT_WRITEDATA, &liststring);
270                 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1);
271                 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 1);
272                 res = curl_easy_perform(curl);
273                 //if (res != CURLE_OK)
274                 //      errorstream<<"Serverlist at url "<<g_settings->get("serverlist_url")<<" not found (internet connection?)"<<std::endl;
275                 curl_easy_cleanup(curl);
276         }
277
278 }
279 #endif
280
281 } //namespace ServerList