Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
plugins_processor.cpp
1 
2 /***************************************************************************
3  * plugins_processor.cpp - Web request processor for plugin info
4  *
5  * Created: Thu Feb 12 13:00:37 2009
6  * Copyright 2006-2009 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "plugins_processor.h"
24 #include <webview/page_reply.h>
25 #include <webview/redirect_reply.h>
26 
27 #include <plugin/manager.h>
28 
29 #include <string>
30 #include <cstring>
31 #include <cstdlib>
32 
33 using namespace fawkes;
34 
35 /** @class WebviewPluginsRequestProcessor "plugins_processor.h"
36  * Plugins web request processor.
37  * Provides access to plugin lists and allows for loading/unloading plugins.
38  * @author Tim Niemueller
39  */
40 
41 /** Constructor.
42  * @param baseurl Base URL where processor is mounted
43  * @param manager PluginManager instance
44  */
46  PluginManager *manager)
47 {
48  __baseurl = strdup(baseurl);
49  __baseurl_len = strlen(__baseurl);
50  __manager = manager;
51 }
52 
53 
54 /** Destructor. */
56 {
57  free(__baseurl);
58 }
59 
60 
61 WebReply *
63  const char *method,
64  const char *version,
65  const char *upload_data,
66  size_t *upload_data_size,
67  void **session_data)
68 {
69  if ( strncmp(__baseurl, url, __baseurl_len) == 0 ) {
70  // It is in our URL prefix range
71  std::string subpath = std::string(url).substr(__baseurl_len);
72 
73  if (subpath.find("/load/") == 0) {
74  std::string plugin_name = subpath.substr(std::string("/load/").length());
75  try {
76  __manager->load(plugin_name.c_str());
77  return new WebRedirectReply(__baseurl);
78  } catch (Exception &e) {
79  WebPageReply *r = new WebPageReply("Loading plugin failed");
80  r->append_body("<h1>Loading plugin '%s' failed</h1>", plugin_name.c_str());
81  *r += "<p>The encountered error was:</p>";
82  for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
83  *r += std::string(*i) + "<br/>\n";
84  }
85  r->append_body("<p><a href=\"%s\">Back to overview</a> - "
86  "<a href=\"%s\">Retry</a></p>", __baseurl, url);
87  return r;
88  }
89  } else if (subpath.find("/unload/") == 0) {
90  std::string plugin_name = subpath.substr(std::string("/unload/").length());
91  try {
92  __manager->unload(plugin_name.c_str());
93  return new WebRedirectReply(__baseurl);
94  } catch (Exception &e) {
95  WebPageReply *r = new WebPageReply("Unloading plugin failed");
96  r->append_body("<h1>Unloading plugin '%s' failed</h1>",
97  plugin_name.c_str());
98  *r += "<p>The encountered error was:</p>";
99  for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
100  *r += std::string(*i) + "<br/>\n";
101  }
102  r->append_body("<p><a href=\"%s\">Back to overview</a> - "
103  "<a href=\"%s\">Retry</a></p>", __baseurl, url);
104  return r;
105  }
106  } else {
107  WebPageReply *r = new WebPageReply("Plugins");
108  *r += "<h2>Fawkes Plugins</h2>\n";
109 
110  *r += "<table>\n";
111  *r += "<tr><th>Name</th><th>Description</th><th>Loaded</th><th>Action</th></tr>\n";
112 
113  std::list<std::pair<std::string, std::string> > available_plugins;
114  std::list<std::pair<std::string, std::string> >::iterator i;
115 
116  available_plugins = __manager->get_available_plugins();
117 
118  for (i = available_plugins.begin(); i != available_plugins.end(); ++i) {
119  bool is_loaded = __manager->is_loaded(i->first.c_str());
120 
121  const char *loaded_color = is_loaded ? "green" : "red";
122  const char *loaded = is_loaded ? "Yes" : "No";
123  const char *action_link = is_loaded ? "unload" : "load";
124 
125  r->append_body("<tr><td>%s</td><td>%s</td>"
126  "<td><span style=\"color:%s\">%s<span></td>"
127  "<td><a href=\"%s/%s/%s\">%s</a></td>\n",
128  i->first.c_str(), i->second.c_str(), loaded_color, loaded,
129  __baseurl, action_link, i->first.c_str(), action_link);
130  }
131 
132  *r += "</table>\n";
133 
134  return r;
135  }
136  } else {
137  return NULL;
138  }
139 }