Anope IRC Services  Version 2.0
webcpanel.h
Go to the documentation of this file.
1 /*
2  * (C) 2003-2014 Anope Team
3  * Contact us at team@anope.org
4  *
5  * Please read COPYING and README for further details.
6  */
7 
8 #include "module.h"
9 #include "modules/httpd.h"
10 
11 #include "static_fileserver.h"
12 #include "template_fileserver.h"
13 
14 extern Module *me;
15 
17 
18 struct SubSection
19 {
22 };
23 
24 struct Section
25 {
27  std::vector<SubSection> subsections;
28 };
29 
30 /* An interface for this webpanel used by other modules */
31 class Panel : public Section, public Service
32 {
33  public:
34  Panel(Module *c, const Anope::string &n) : Service(c, "Panel", n) { }
35 
36  std::vector<Section> sections;
37 
39  {
40  if (!client)
41  return NULL;
42 
43  const Anope::string &acc = msg.cookies["account"], &id = msg.cookies["id"];
44 
45  if (acc.empty() || id.empty())
46  return NULL;
47 
48  NickAlias *na = NickAlias::Find(acc);
49  if (na == NULL)
50  return NULL;
51 
52  Anope::string *n_id = na->GetExt<Anope::string>("webcpanel_id"), *n_ip = na->GetExt<Anope::string>("webcpanel_ip");
53  if (n_id == NULL || n_ip == NULL)
54  return NULL;
55  else if (id != *n_id)
56  return NULL;
57  else if (client->GetIP() != *n_ip)
58  return NULL;
59  else
60  return na;
61  }
62 };
63 
64 class WebPanelPage : public HTTPPage
65 {
66  public:
67  WebPanelPage(const Anope::string &u, const Anope::string &ct = "text/html") : HTTPPage(u, ct)
68  {
69  }
70 
71  virtual bool OnRequest(HTTPProvider *, const Anope::string &, HTTPClient *, HTTPMessage &, HTTPReply &) = 0;
72 };
73 
75 {
77 
78  public:
79  WebPanelProtectedPage(const Anope::string &cat, const Anope::string &u, const Anope::string &ct = "text/html") : WebPanelPage(u, ct), category(cat)
80  {
81  }
82 
83  bool OnRequest(HTTPProvider *provider, const Anope::string &page_name, HTTPClient *client, HTTPMessage &message, HTTPReply &reply) anope_override anope_final
84  {
85  ServiceReference<Panel> panel("Panel", "webcpanel");
86  NickAlias *na;
87 
88  if (!panel || !(na = panel->GetNickFromSession(client, message)))
89  {
90  reply.error = HTTP_FOUND;
91  reply.headers["Location"] = Anope::string("http") + (provider->IsSSL() ? "s" : "") + "://" + message.headers["Host"] + "/";
92  return true; // Access denied
93  }
94 
96 
97  replacements["TITLE"] = page_title;
98  replacements["ACCOUNT"] = na->nc->display;
99  replacements["PAGE_NAME"] = page_name;
100  replacements["CATEGORY"] = category;
101  if (na->nc->IsServicesOper())
102  replacements["IS_OPER"];
103 
104  Anope::string sections, get;
105 
106  for (std::map<Anope::string, Anope::string>::iterator it = message.get_data.begin(), it_end = message.get_data.end(); it != it_end; ++it)
107  if (this->GetData().count(it->first) > 0)
108  get += "&" + it->first + "=" + HTTPUtils::URLEncode(it->second);
109  if (get.empty() == false)
110  get = "?" + get.substr(1);
111 
112  Section *ns = NULL;
113  for (unsigned i = 0; i < panel->sections.size(); ++i)
114  {
115  Section& s = panel->sections[i];
116  if (s.name == this->category)
117  ns = &s;
118  replacements["CATEGORY_URLS"] = s.subsections[0].url;
119  replacements["CATEGORY_NAMES"] = s.name;
120  }
121 
122  if (ns)
123  {
124  sections = "";
125  for (unsigned i = 0; i < ns->subsections.size(); ++i)
126  {
127  SubSection& ss = ns->subsections[i];
128  replacements["SUBCATEGORY_URLS"] = ss.url;
129  replacements["SUBCATEGORY_GETS"] = get;
130  replacements["SUBCATEGORY_NAMES"] = ss.name;
131  }
132  }
133 
134  return this->OnRequest(provider, page_name, client, message, reply, na, replacements);
135  }
136 
138 
139  /* What get data should be appended to links in the navbar */
140  virtual std::set<Anope::string> GetData() { return std::set<Anope::string>(); }
141 };
142 
143 namespace WebPanel
144 {
154  extern void RunCommand(const Anope::string &user, NickCore *nc, const Anope::string &service, const Anope::string &c, const std::vector<Anope::string> &params, TemplateFileServer::Replacements &r, const Anope::string &key = "MESSAGES");
155 }
156 
157 #include "pages/index.h"
158 #include "pages/logout.h"
159 #include "pages/register.h"
160 #include "pages/confirm.h"
161 
162 #include "pages/nickserv/info.h"
163 #include "pages/nickserv/cert.h"
164 #include "pages/nickserv/access.h"
165 #include "pages/nickserv/alist.h"
166 
167 #include "pages/chanserv/info.h"
168 #include "pages/chanserv/set.h"
169 #include "pages/chanserv/access.h"
170 #include "pages/chanserv/akick.h"
171 #include "pages/chanserv/modes.h"
172 #include "pages/chanserv/drop.h"
173 
174 #include "pages/memoserv/memos.h"
175 
176 #include "pages/hostserv/request.h"
177 
178 #include "pages/operserv/akill.h"
Serialize::Reference< NickCore > nc
Definition: account.h:47
static NickAlias * Find(const Anope::string &nick)
Definition: nickalias.cpp:121
bool OnRequest(HTTPProvider *provider, const Anope::string &page_name, HTTPClient *client, HTTPMessage &message, HTTPReply &reply) anope_override anope_final
Definition: webcpanel.h:83
Anope::string name
Definition: webcpanel.h:26
Anope::string provider_name
Definition: webcpanel.cpp:11
NickAlias * GetNickFromSession(HTTPClient *client, HTTPMessage &msg)
Definition: webcpanel.h:38
Anope::string URLEncode(const Anope::string &url)
Definition: httpd.h:187
Anope::string category
Definition: webcpanel.h:76
virtual std::set< Anope::string > GetData()
Definition: webcpanel.h:140
Anope::string url
Definition: webcpanel.h:21
Panel(Module *c, const Anope::string &n)
Definition: webcpanel.h:34
Anope::string template_name
Definition: webcpanel.cpp:11
#define anope_final
Definition: services.h:57
virtual bool OnRequest(HTTPProvider *, const Anope::string &, HTTPClient *, HTTPMessage &, HTTPReply &)=0
void RunCommand(const Anope::string &user, NickCore *nc, const Anope::string &service, const Anope::string &c, const std::vector< Anope::string > &params, TemplateFileServer::Replacements &r, const Anope::string &key)
Definition: webcpanel.cpp:234
std::map< Anope::string, Anope::string > cookies
Definition: httpd.h:79
Anope::string page_title
Definition: webcpanel.cpp:11
#define anope_override
Definition: services.h:56
bool empty() const
Definition: anope.h:126
WebPanelPage(const Anope::string &u, const Anope::string &ct="text/html")
Definition: webcpanel.h:67
std::basic_string< char, ci_char_traits, std::allocator< char > > string
Definition: hashcomp.h:133
Module * me
Definition: webcpanel.cpp:10
Anope::string name
Definition: webcpanel.h:20
virtual const Anope::string GetIP()
Definition: httpd.h:121
Anope::string template_base
Definition: webcpanel.cpp:11
Definition: httpd.h:88
T * GetExt(const Anope::string &name) const
Definition: extensible.h:213
WebPanelProtectedPage(const Anope::string &cat, const Anope::string &u, const Anope::string &ct="text/html")
Definition: webcpanel.h:79
std::vector< Section > sections
Definition: webcpanel.h:36
std::vector< SubSection > subsections
Definition: webcpanel.h:27