Anope IRC Services  Version 2.0
access.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 #include "utils.h"
10 
12 {
13 }
14 
16 {
17  TemplateFileServer Page("chanserv/access.html");
18  const Anope::string &chname = message.get_data["channel"];
19 
20  BuildChanList(na, replacements);
21 
22  if (chname.empty())
23  {
24  Page.Serve(server, page_name, client, message, reply, replacements);
25  return true;
26  }
27 
28  ChannelInfo *ci = ChannelInfo::Find(chname);
29 
30  if (!ci)
31  {
32  replacements["MESSAGES"] = "Channel not registered.";
33  Page.Serve(server, page_name, client, message, reply, replacements);
34  return true;
35  }
36 
37  AccessGroup u_access = ci->AccessFor(na->nc);
38  bool has_priv = na->nc->IsServicesOper() && na->nc->o->ot->HasPriv("chanserv/access/modify");
39 
40  if (!u_access.HasPriv("ACCESS_LIST") && !has_priv)
41  {
42  replacements["MESSAGES"] = "Access denied.";
43  Page.Serve(server, page_name, client, message, reply, replacements);
44  return true;
45  }
46 
47  replacements["ACCESS_LIST"] = "YES";
48 
49  const ChanAccess *highest = u_access.Highest();
50 
51  if (u_access.HasPriv("ACCESS_CHANGE") || has_priv)
52  {
53  if (message.get_data["del"].empty() == false && message.get_data["mask"].empty() == false)
54  {
55  std::vector<Anope::string> params;
56  params.push_back(ci->name);
57  params.push_back("DEL");
58  params.push_back(message.get_data["mask"]);
59 
60  WebPanel::RunCommand(na->nc->display, na->nc, "ChanServ", "chanserv/access", params, replacements);
61  }
62  else if (message.post_data["mask"].empty() == false && message.post_data["access"].empty() == false && message.post_data["provider"].empty() == false)
63  {
64  // Generic access add code here, works with any provider (so we can't call a command exactly)
65  AccessProvider *a = NULL;
66  for (std::list<AccessProvider *>::const_iterator it = AccessProvider::GetProviders().begin(); it != AccessProvider::GetProviders().end(); ++it)
67  if ((*it)->name == message.post_data["provider"])
68  a = *it;
69 
70  if (a)
71  {
72  bool denied = false;
73 
74  for (unsigned i = 0, end = ci->GetAccessCount(); i < end; ++i)
75  {
76  ChanAccess *acc = ci->GetAccess(i);
77 
78  if (acc->Mask() == message.post_data["mask"])
79  {
80  if ((!highest || *acc >= *highest) && !u_access.founder && !has_priv)
81  {
82  replacements["MESSAGES"] = "Access denied";
83  denied = true;
84  }
85  else
86  delete acc;
87  break;
88  }
89  }
90 
91 
92  unsigned access_max = Config->GetModule("chanserv")->Get<unsigned>("accessmax", "1024");
93  if (access_max && ci->GetAccessCount() >= access_max)
94  replacements["MESSAGES"] = "Sorry, you can only have " + stringify(access_max) + " access entries on a channel.";
95  else if (!denied)
96  {
97  ChanAccess *new_acc = a->Create();
98  new_acc->SetMask(message.post_data["mask"], ci);
99  new_acc->creator = na->nc->display;
100  try
101  {
102  new_acc->AccessUnserialize(message.post_data["access"]);
103  }
104  catch (...)
105  {
106  replacements["MESSAGES"] = "Invalid access expression for the given type";
107  delete new_acc;
108  new_acc = NULL;
109  }
110  if (new_acc)
111  {
112  new_acc->last_seen = 0;
113  new_acc->created = Anope::CurTime;
114 
115  if ((!highest || *highest <= *new_acc) && !u_access.founder && !has_priv)
116  delete new_acc;
117  else if (new_acc->AccessSerialize().empty())
118  {
119  replacements["MESSAGES"] = "Invalid access expression for the given type";
120  delete new_acc;
121  }
122  else
123  {
124  ci->AddAccess(new_acc);
125  replacements["MESSAGES"] = "Access for " + new_acc->Mask() + " set to " + new_acc->AccessSerialize();
126  }
127  }
128  }
129  }
130  }
131  }
132 
133  replacements["ESCAPED_CHANNEL"] = HTTPUtils::URLEncode(chname);
134  replacements["ACCESS_CHANGE"] = u_access.HasPriv("ACCESS_CHANGE") ? "YES" : "NO";
135 
136  for (unsigned i = 0; i < ci->GetAccessCount(); ++i)
137  {
138  ChanAccess *access = ci->GetAccess(i);
139 
140  replacements["MASKS"] = HTTPUtils::Escape(access->Mask());
141  replacements["ACCESSES"] = HTTPUtils::Escape(access->AccessSerialize());
142  replacements["CREATORS"] = HTTPUtils::Escape(access->creator);
143  }
144 
145  for (std::list<AccessProvider *>::const_iterator it = AccessProvider::GetProviders().begin(); it != AccessProvider::GetProviders().end(); ++it)
146  {
147  const AccessProvider *a = *it;
148  replacements["PROVIDERS"] = a->name;
149  }
150 
151  Page.Serve(server, page_name, client, message, reply, replacements);
152  return true;
153 }
154 
155 std::set<Anope::string> WebCPanel::ChanServ::Access::GetData()
156 {
157  std::set<Anope::string> v;
158  v.insert("channel");
159  return v;
160 }
161 
Serialize::Reference< NickCore > nc
Definition: account.h:47
void Serve(HTTPProvider *, const Anope::string &, HTTPClient *, HTTPMessage &, HTTPReply &, Replacements &)
Definition: hashcomp.h:84
Anope::string Escape(const Anope::string &src)
Definition: httpd.h:206
Anope::string name
Definition: regchannel.h:63
void BuildChanList(NickAlias *na, TemplateFileServer::Replacements &replacements)
Definition: utils.cpp:24
Anope::string URLEncode(const Anope::string &url)
Definition: httpd.h:187
Anope::string creator
Definition: access.h:93
virtual Anope::string AccessSerialize() const =0
time_t created
Definition: access.h:95
bool founder
Definition: access.h:150
CoreExport time_t CurTime
Definition: main.cpp:41
virtual void AccessUnserialize(const Anope::string &data)=0
bool HasPriv(const Anope::string &priv) const
Definition: access.cpp:384
static ChannelInfo * Find(const Anope::string &name)
Definition: regchannel.cpp:630
Definition: Config.cs:26
time_t last_seen
Definition: access.h:94
const Anope::string & Mask() const
Definition: access.cpp:196
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
const ChanAccess * Highest() const
Definition: access.cpp:416
virtual ChanAccess * Create()=0
unsigned GetAccessCount() const
Definition: regchannel.cpp:474
bool empty() const
Definition: anope.h:126
void AddAccess(ChanAccess *access)
Definition: regchannel.cpp:398
static const std::list< AccessProvider * > & GetProviders()
Definition: access.cpp:137
Access(const Anope::string &cat, const Anope::string &u)
Definition: access.cpp:11
AccessGroup AccessFor(const User *u)
Definition: regchannel.cpp:413
Anope::string stringify(const T &x)
Definition: anope.h:710
std::map< Anope::string, Anope::string > post_data
Definition: httpd.h:81
Anope::string name
Definition: service.h:88
bool OnRequest(HTTPProvider *, const Anope::string &, HTTPClient *, HTTPMessage &, HTTPReply &, NickAlias *, TemplateFileServer::Replacements &) anope_override
Definition: access.cpp:15
void SetMask(const Anope::string &mask, ChannelInfo *ci)
Definition: access.cpp:165
std::map< Anope::string, Anope::string > get_data
Definition: httpd.h:80
std::set< Anope::string > GetData() anope_override
Definition: access.cpp:155
ChanAccess * GetAccess(unsigned index) const
Definition: regchannel.cpp:403