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