anope- supernets anope source code & configuration |
git clone git://git.acid.vegas/anope.git |
Log | Files | Refs | Archive | README |
webcpanel.h (5484B)
1 /* 2 * (C) 2003-2022 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 16 extern Anope::string provider_name, template_name, template_base, page_title; 17 18 struct SubSection 19 { 20 Anope::string name; 21 Anope::string url; 22 }; 23 24 struct Section 25 { 26 Anope::string name; 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 38 NickAlias *GetNickFromSession(HTTPClient *client, HTTPMessage &msg) 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 74 class WebPanelProtectedPage : public WebPanelPage 75 { 76 Anope::string category; 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 95 TemplateFileServer::Replacements replacements; 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 137 virtual bool OnRequest(HTTPProvider *, const Anope::string &, HTTPClient *, HTTPMessage &, HTTPReply &, NickAlias *, TemplateFileServer::Replacements &) = 0; 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 { 145 /** Run a command 146 * @param client HTTP client command is being issued for 147 * @param User name to run command as, probably nc->display unless nc == NULL 148 * @param nc Nick core to run command from 149 * @param service Service for source.owner and source.service 150 * @param c Command to run (as a service name) 151 * @param params Command parameters 152 * @param r Replacements, reply from command goes back here into key 153 * @param key The key to put the replies into r 154 */ 155 extern void RunCommand(HTTPClient *client, const Anope::string &user, NickCore *nc, const Anope::string &service, const Anope::string &c, std::vector<Anope::string> ¶ms, TemplateFileServer::Replacements &r, const Anope::string &key = "MESSAGES"); 156 157 extern void RunCommandWithName(HTTPClient *client, NickCore *nc, const Anope::string &service, const Anope::string &c, const Anope::string &cmdname, std::vector<Anope::string> ¶ms, TemplateFileServer::Replacements &r, const Anope::string &key = "MESSAGES"); 158 } 159 160 #include "pages/index.h" 161 #include "pages/logout.h" 162 #include "pages/register.h" 163 #include "pages/confirm.h" 164 165 #include "pages/nickserv/info.h" 166 #include "pages/nickserv/cert.h" 167 #include "pages/nickserv/access.h" 168 #include "pages/nickserv/alist.h" 169 #include "pages/nickserv/confirm.h" 170 171 #include "pages/chanserv/info.h" 172 #include "pages/chanserv/set.h" 173 #include "pages/chanserv/access.h" 174 #include "pages/chanserv/akick.h" 175 #include "pages/chanserv/modes.h" 176 #include "pages/chanserv/drop.h" 177 178 #include "pages/memoserv/memos.h" 179 180 #include "pages/hostserv/request.h" 181 182 #include "pages/operserv/akill.h"