Graceful handling of situations when downloading the Add-on list fails
[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 <sstream>
23 #include <stdexcept>
24 #include <list>
25 #include <physfs.h>
26 #include <sys/stat.h>
27 #include <stdio.h>
28 #include "addon_manager.hpp"
29 #include "config.h"
30 #include "log.hpp"
31 #include "lisp/parser.hpp"
32 #include "lisp/lisp.hpp"
33 #include "lisp/list_iterator.hpp"
34
35 #ifdef HAVE_LIBCURL
36 #include <curl/curl.h>
37 #include <curl/types.h>
38 #include <curl/easy.h>
39 #endif
40
41 #ifdef HAVE_LIBCURL
42 namespace {
43
44   size_t my_curl_string_append(void *ptr, size_t size, size_t nmemb, void *string_ptr)
45   {
46     std::string& s = *static_cast<std::string*>(string_ptr);
47     std::string buf(static_cast<char*>(ptr), size * nmemb);
48     s += buf;
49     log_debug << "read " << size * nmemb << " bytes of data..." << std::endl;
50     return size * nmemb;
51   }
52
53   size_t my_curl_physfs_write(void *ptr, size_t size, size_t nmemb, void *f_p)
54   {
55     PHYSFS_file* f = static_cast<PHYSFS_file*>(f_p);
56     PHYSFS_sint64 written = PHYSFS_write(f, ptr, size, nmemb);
57     log_debug << "read " << size * nmemb << " bytes of data..." << std::endl;
58     return size * written;
59   }
60
61 }
62 #endif
63
64 AddonManager&
65 AddonManager::get_instance()
66 {
67   static AddonManager instance;
68   return instance;
69 }
70
71 AddonManager::AddonManager()
72 {
73 #ifdef HAVE_LIBCURL
74   curl_global_init(CURL_GLOBAL_ALL);
75 #endif
76 }
77
78 AddonManager::~AddonManager()
79 {
80 #ifdef HAVE_LIBCURL
81   curl_global_cleanup();
82 #endif
83 }
84
85 std::vector<Addon>
86 AddonManager::get_installed_addons() const
87 {
88   std::vector<Addon> addons;
89
90   // iterate over complete search path (i.e. directories and archives)
91   char **i = PHYSFS_getSearchPath();
92   if (!i) throw std::runtime_error("Could not query physfs search path");
93   for (; *i != NULL; i++) {
94
95     // get filename of potential archive
96     std::string fileName = *i;
97
98     // make sure it's in the writeDir
99     static const std::string writeDir = PHYSFS_getWriteDir();
100     if (fileName.compare(0, writeDir.length(), writeDir) != 0) continue;
101
102     // make sure it looks like an archive
103     static const std::string archiveExt = ".zip";
104     if (fileName.compare(fileName.length()-archiveExt.length(), archiveExt.length(), archiveExt) != 0) continue;
105
106     // make sure it exists
107     struct stat stats;
108     if (stat(fileName.c_str(), &stats) != 0) continue;
109
110     // make sure it's an actual file
111     if (!S_ISREG(stats.st_mode)) continue;
112
113     Addon addon;
114
115     // extract nice title as fallback for when the Add-on has no addoninfo file
116     static const char* dirSep = PHYSFS_getDirSeparator();
117     std::string::size_type n = fileName.rfind(dirSep) + 1;
118     if (n == std::string::npos) n = 0;
119     addon.title = fileName.substr(n, fileName.length() - n - archiveExt.length());
120     std::string shortFileName = fileName.substr(n, fileName.length() - n);
121     addon.file = shortFileName;
122    
123     // read an accompaining .nfo file, if it exists
124     static const std::string infoExt = ".nfo";
125     std::string infoFileName = fileName.substr(n, fileName.length() - n - archiveExt.length()) + infoExt;
126     if (PHYSFS_exists(infoFileName.c_str())) {
127       addon.parse(infoFileName);
128       if (addon.file != shortFileName) {
129         log_warning << "Add-on \"" << addon.title << "\", contained in file \"" << shortFileName << "\" is accompained by an addoninfo file that specifies \"" << addon.file << "\" as the Add-on's file name. Skipping." << std::endl;
130       }
131     }
132
133     addon.isInstalled = true;
134     addons.push_back(addon);
135   }
136
137   return addons;
138 }
139
140 std::vector<Addon>
141 AddonManager::get_available_addons() const
142 {
143   std::vector<Addon> addons;
144
145 #ifdef HAVE_LIBCURL
146
147   // FIXME: This URL is just for testing!
148   const char* baseUrl = "http://www.deltadevelopment.de/users/christoph/supertux/addons/index.nfo";
149   std::string addoninfos = "";
150
151   CURL *curl_handle;
152   curl_handle = curl_easy_init();
153   curl_easy_setopt(curl_handle, CURLOPT_URL, baseUrl);
154   curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "SuperTux/" PACKAGE_VERSION " libcURL");
155   curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, my_curl_string_append);
156   curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &addoninfos);
157   curl_easy_perform(curl_handle);
158   curl_easy_cleanup(curl_handle);
159
160   if (addoninfos == "") throw std::runtime_error("Add-on list download failed");
161
162   try {
163     lisp::Parser parser;
164     std::stringstream addoninfos_stream(addoninfos);
165     const lisp::Lisp* root = parser.parse(addoninfos_stream, "supertux-addons");
166
167     const lisp::Lisp* addons_lisp = root->get_lisp("supertux-addons");
168     if(!addons_lisp) throw std::runtime_error("file is not a supertux-addons file.");
169
170     lisp::ListIterator iter(addons_lisp);
171     while(iter.next()) {
172       const std::string& token = iter.item();
173       if(token == "supertux-addoninfo") {
174         Addon addon;
175         addon.parse(*(iter.lisp()));
176
177         // make sure the Add-on's file name does not contain weird characters
178         if (addon.file.find_first_not_of("match.quiz-proxy_gwenblvdjfks0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos) {
179           log_warning << "Add-on \"" << addon.title << "\" contains unsafe file name. Skipping." << std::endl;
180           continue;
181         }
182
183         addon.isInstalled = false;
184         addons.push_back(addon);
185       } else {
186         log_warning << "Unknown token '" << token << "' in supertux-addons file" << std::endl;
187       }
188     }
189   } catch(std::exception& e) {
190     std::stringstream msg;
191     msg << "Problem when reading addoninfo: " << e.what();
192     throw std::runtime_error(msg.str());
193   }
194
195 #endif
196
197   return addons;
198 }
199
200
201 void
202 AddonManager::install(const Addon& addon)
203 {
204
205 #ifdef HAVE_LIBCURL
206
207   char* url = (char*)malloc(addon.http_url.length() + 1);
208   strncpy(url, addon.http_url.c_str(), addon.http_url.length() + 1);
209
210   PHYSFS_file* f = PHYSFS_openWrite(addon.file.c_str());
211
212   log_debug << "Downloading \"" << url << "\"" << std::endl;
213
214   CURL *curl_handle;
215   curl_handle = curl_easy_init();
216   curl_easy_setopt(curl_handle, CURLOPT_URL, url);
217   curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "SuperTux/" PACKAGE_VERSION " libcURL");
218   curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, my_curl_physfs_write);
219   curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, f);
220   curl_easy_perform(curl_handle);
221   curl_easy_cleanup(curl_handle);
222
223   PHYSFS_close(f);
224
225   free(url);
226
227   // write an accompaining .nfo file
228   static const std::string archiveExt = ".zip";
229   static const std::string infoExt = ".nfo";
230   std::string infoFileName = addon.file.substr(0, addon.file.length()-archiveExt.length()) + infoExt;
231   addon.write(infoFileName);
232
233   static const std::string writeDir = PHYSFS_getWriteDir();
234   static const std::string dirSep = PHYSFS_getDirSeparator();
235   std::string fullFilename = writeDir + dirSep + addon.file;
236   log_debug << "Finished downloading \"" << fullFilename << "\"" << std::endl;
237   PHYSFS_addToSearchPath(fullFilename.c_str(), 1);
238 #else
239   (void) addon;
240 #endif
241
242 }
243
244 void
245 AddonManager::remove(const Addon& addon)
246 {
247   log_debug << "deleting file \"" << addon.file << "\"" << std::endl;
248   PHYSFS_removeFromSearchPath(addon.file.c_str());
249   PHYSFS_delete(addon.file.c_str());
250
251   // remove an accompaining .nfo file
252   static const std::string archiveExt = ".zip";
253   static const std::string infoExt = ".nfo";
254   std::string infoFileName = addon.file.substr(0, addon.file.length()-archiveExt.length()) + infoExt;
255   if (PHYSFS_exists(infoFileName.c_str())) {
256     log_debug << "deleting file \"" << infoFileName << "\"" << std::endl;
257     PHYSFS_delete(infoFileName.c_str());
258   }
259 }
260