Anope IRC Services  Version 2.0
index.cpp
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 "../webcpanel.h"
9 
11 {
18 
19  public:
20  WebpanelRequest(Module *o, HTTPReply &r, HTTPMessage &m, HTTPProvider *s, const Anope::string &p_n, HTTPClient *c, TemplateFileServer::Replacements &re, const Anope::string &user, const Anope::string &pass) : IdentifyRequest(o, user, pass), reply(r), message(m), server(s), page_name(p_n), client(c), replacements(re) { }
21 
23  {
24  if (!client || !server)
25  return;
26  NickAlias *na = NickAlias::Find(this->GetAccount());
27  if (!na)
28  {
29  this->OnFail();
30  return;
31  }
32 
33  Anope::string id;
34  for (int i = 0; i < 64; ++i)
35  {
36  char c;
37  do
38  c = 48 + (rand() % 75);
39  while (!isalnum(c));
40  id += c;
41  }
42 
43  na->Extend<Anope::string>("webcpanel_id", id);
44  na->Extend<Anope::string>("webcpanel_ip", client->GetIP());
45 
46  {
48  c.push_back(std::make_pair("account", na->nick));
49  c.push_back(std::make_pair("Path", "/"));
50  reply.cookies.push_back(c);
51  }
52 
53  {
55  c.push_back(std::make_pair("id", id));
56  c.push_back(std::make_pair("Path", "/"));
57  reply.cookies.push_back(c);
58  }
59 
60  reply.error = HTTP_FOUND;
61  reply.headers["Location"] = Anope::string("http") + (server->IsSSL() ? "s" : "") + "://" + message.headers["Host"] + "/nickserv/info";
62 
63  client->SendReply(&reply);
64  }
65 
67  {
68  if (!client || !server)
69  return;
70  replacements["INVALID_LOGIN"] = "Invalid username or password";
71  TemplateFileServer page("login.html");
72  page.Serve(server, page_name, client, message, reply, replacements);
73 
74  client->SendReply(&reply);
75  }
76 };
77 
78 bool WebCPanel::Index::OnRequest(HTTPProvider *server, const Anope::string &page_name, HTTPClient *client, HTTPMessage &message, HTTPReply &reply)
79 {
81  const Anope::string &user = message.post_data["username"], &pass = message.post_data["password"];
82 
83  replacements["TITLE"] = page_title;
84 
85  if (!user.empty() && !pass.empty())
86  {
87  // Rate limit check.
88 
89  WebpanelRequest *req = new WebpanelRequest(me, reply, message, server, page_name, client, replacements, user, pass);
90  FOREACH_MOD(OnCheckAuthentication, (NULL, req));
91  req->Dispatch();
92  return false;
93  }
94 
95  TemplateFileServer page("login.html");
96  page.Serve(server, page_name, client, message, reply, replacements);
97  return true;
98 }
99 
static NickAlias * Find(const Anope::string &nick)
Definition: nickalias.cpp:121
Reference< HTTPProvider > server
Definition: index.cpp:14
void OnSuccess() anope_override
Definition: index.cpp:22
std::map< Anope::string, Anope::string > headers
Definition: httpd.h:78
void Serve(HTTPProvider *, const Anope::string &, HTTPClient *, HTTPMessage &, HTTPReply &, Replacements &)
HTTPMessage message
Definition: index.cpp:13
void push_back(char c)
Definition: anope.h:142
Anope::string nick
Definition: account.h:37
WebpanelRequest(Module *o, HTTPReply &r, HTTPMessage &m, HTTPProvider *s, const Anope::string &p_n, HTTPClient *c, TemplateFileServer::Replacements &re, const Anope::string &user, const Anope::string &pass)
Definition: index.cpp:20
void Dispatch()
Definition: account.cpp:57
T * Extend(const Anope::string &name, const T &what)
Definition: extensible.h:224
bool IsSSL() const
Definition: httpd.h:151
virtual void SendReply(HTTPReply *)=0
void OnFail() anope_override
Definition: index.cpp:66
std::list< std::pair< Anope::string, Anope::string > > cookie
Definition: httpd.h:19
HTTPReply reply
Definition: index.cpp:12
bool OnRequest(HTTPProvider *, const Anope::string &, HTTPClient *, HTTPMessage &, HTTPReply &) anope_override
Definition: index.cpp:78
#define FOREACH_MOD(ename, args)
Definition: modules.h:62
static Module * me
Definition: bs_kick.cpp:18
Anope::string page_name
Definition: index.cpp:15
std::map< Anope::string, Anope::string > headers
Definition: httpd.h:18
HTTPError error
Definition: httpd.h:16
#define anope_override
Definition: services.h:56
std::vector< cookie > cookies
Definition: httpd.h:20
bool empty() const
Definition: anope.h:126
std::basic_string< char, ci_char_traits, std::allocator< char > > string
Definition: hashcomp.h:133
std::map< Anope::string, Anope::string > post_data
Definition: httpd.h:81
Reference< HTTPClient > client
Definition: index.cpp:16
Anope::string page_title
Definition: webcpanel.cpp:11
virtual const Anope::string GetIP()
Definition: httpd.h:121
TemplateFileServer::Replacements replacements
Definition: index.cpp:17
const Anope::string & GetAccount() const
Definition: account.h:250