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