Fixed trailing whitespaces in all(?) source files of supertux, also fixed some svn...
[supertux.git] / src / addon_manager.cpp
1 //  $Id$
2 //
3 //  SuperTux - Add-on Manager
4 //  Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 //  02111-1307, USA.
20 //
21
22 #include <stdexcept>
23 #include <list>
24 #include <physfs.h>
25 #include <sys/stat.h>
26 #include "addon_manager.hpp"
27 #include "config.h"
28 #include "log.hpp"
29
30 #ifdef HAVE_LIBCURL
31 #include <curl/curl.h>
32 #include <curl/types.h>
33 #include <curl/easy.h>
34 #endif
35
36 #ifdef HAVE_LIBCURL
37 namespace {
38
39   size_t my_curl_string_append(void *ptr, size_t size, size_t nmemb, void *string_ptr)
40   {
41     std::string& s = *static_cast<std::string*>(string_ptr);
42     std::string buf(static_cast<char*>(ptr), size * nmemb);
43     s += buf;
44     log_debug << "read " << size * nmemb << " bytes of data..." << std::endl;
45     return size * nmemb;
46   }
47
48   size_t my_curl_physfs_write(void *ptr, size_t size, size_t nmemb, void *f_p)
49   {
50     PHYSFS_file* f = static_cast<PHYSFS_file*>(f_p);
51     PHYSFS_sint64 written = PHYSFS_write(f, ptr, size, nmemb);
52     log_debug << "read " << size * nmemb << " bytes of data..." << std::endl;
53     return size * written;
54   }
55
56 }
57 #endif
58
59 AddonManager&
60 AddonManager::get_instance()
61 {
62   static AddonManager instance;
63   return instance;
64 }
65
66 AddonManager::AddonManager()
67 {
68 #ifdef HAVE_LIBCURL
69   curl_global_init(CURL_GLOBAL_ALL);
70 #endif
71 }
72
73 AddonManager::~AddonManager()
74 {
75 #ifdef HAVE_LIBCURL
76   curl_global_cleanup();
77 #endif
78 }
79
80 std::vector<Addon>
81 AddonManager::get_addons() const
82 {
83   std::vector<Addon> addons;
84
85   // first step: search for installed addons
86
87   // iterate over complete search path (i.e. directories and archives)
88   char **i = PHYSFS_getSearchPath();
89   if (!i) throw std::runtime_error("Could not query physfs search path");
90   for (; *i != NULL; i++) {
91
92     // get filename of potential archive
93     std::string fileName = *i;
94
95     // make sure it's in the writeDir
96     static const std::string writeDir = PHYSFS_getWriteDir();
97     if (fileName.compare(0, writeDir.length(), writeDir) != 0) continue;
98
99     // make sure it looks like an archive
100     static const std::string archiveExt = ".zip";
101     if (fileName.compare(fileName.length()-archiveExt.length(), archiveExt.length(), archiveExt) != 0) continue;
102
103     // make sure it exists
104     struct stat stats;
105     if (stat(fileName.c_str(), &stats) != 0) continue;
106
107     // make sure it's an actual file
108     if (!S_ISREG(stats.st_mode)) continue;
109
110     // extract nice title
111     static const char* dirSep = PHYSFS_getDirSeparator();
112     std::string::size_type n = fileName.rfind(dirSep) + 1;
113     if (n == std::string::npos) n = 0;
114     std::string title = fileName.substr(n, fileName.length() - n - archiveExt.length());
115
116     Addon addon;
117     addon.title = title;
118     addon.fname = fileName;
119     addon.isInstalled = true;
120
121     addons.push_back(addon);
122   }
123
124 #ifdef HAVE_LIBCURL
125   // second step: search for available addons
126
127   // FIXME: This URL is just for testing!
128   const char* baseUrl = "http://www.deltadevelopment.de/users/christoph/supertux/addons/";
129   std::string html = "";
130
131   CURL *curl_handle;
132   curl_handle = curl_easy_init();
133   curl_easy_setopt(curl_handle, CURLOPT_URL, baseUrl);
134   curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "SuperTux/" PACKAGE_VERSION " libcURL");
135   curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, my_curl_string_append);
136   curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &html);
137   curl_easy_perform(curl_handle);
138   curl_easy_cleanup(curl_handle);
139
140   //std::string html = "Blubb<a href=\"http://www.deltadevelopment.de/users/christoph/supertux/addons/coconut_fortress.zip\">Coconut Fortress</a>\nFoobar<a href=\"http://www.deltadevelopment.de/users/christoph/supertux/addons/in_the_spring.zip\">Another</a>Baz";
141   static const std::string startToken = "href=\"";
142   static const std::string endToken = "\"";
143
144   // extract urls: for each startToken found...
145   std::string::size_type n = 0;
146   while ((n = html.find(startToken)) != std::string::npos) {
147
148     // strip everything up to and including token
149     html.erase(0, n + startToken.length());
150
151     // find end token
152     std::string::size_type n2 = html.find(endToken);
153     if (n2 == std::string::npos) break;
154
155     // extract url: it's the string inbetween
156     std::string url = html.substr(0, n2);
157
158     // strip everything up to and including endToken
159     html.erase(0, n2 + endToken.length());
160
161     // make absolute url
162     url = std::string(baseUrl) + url;
163
164     // make sure url looks like it points to an archive
165     static const std::string archiveExt = ".zip";
166     if (url.compare(url.length()-archiveExt.length(), archiveExt.length(), archiveExt) != 0) continue;
167
168     // extract nice title
169     std::string::size_type n = url.rfind('/') + 1;
170     if (n == std::string::npos) n = 0;
171     std::string title = url.substr(n, url.length() - n - archiveExt.length());
172
173     // construct file name
174     std::string fname = url.substr(n);
175
176     // make sure it does not contain weird characters
177     if (fname.find_first_not_of("match.quiz-proxy_gwenblvdjfks0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos) continue;
178
179     Addon addon;
180     addon.title = title;
181     addon.fname = fname;
182     addon.url = url;
183     addon.isInstalled = false;
184
185     addons.push_back(addon);
186   }
187 #endif
188
189   return addons;
190 }
191
192
193 void
194 AddonManager::install(const Addon&
195                 #ifdef HAVE_LIBCURL
196                 addon
197                 #endif
198                 )
199 {
200
201 #ifdef HAVE_LIBCURL
202
203   char* url = (char*)malloc(addon.url.length() + 1);
204   strncpy(url, addon.url.c_str(), addon.url.length() + 1);
205
206   PHYSFS_file* f = PHYSFS_openWrite(addon.fname.c_str());
207
208   log_debug << "Downloading \"" << url << "\"" << std::endl;
209
210   CURL *curl_handle;
211   curl_handle = curl_easy_init();
212   curl_easy_setopt(curl_handle, CURLOPT_URL, url);
213   curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "SuperTux/" PACKAGE_VERSION " libcURL");
214   curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, my_curl_physfs_write);
215   curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, f);
216   curl_easy_perform(curl_handle);
217   curl_easy_cleanup(curl_handle);
218
219   PHYSFS_close(f);
220
221   free(url);
222
223   static const std::string writeDir = PHYSFS_getWriteDir();
224   static const std::string dirSep = PHYSFS_getDirSeparator();
225   std::string fullFilename = writeDir + dirSep + addon.fname;
226   log_debug << "Finished downloading \"" << fullFilename << "\"" << std::endl;
227   PHYSFS_addToSearchPath(fullFilename.c_str(), 1);
228 #endif
229
230 }
231
232 void
233 AddonManager::remove(const Addon& addon)
234 {
235   PHYSFS_removeFromSearchPath(addon.fname.c_str());
236   unlink(addon.fname.c_str());
237 }