access.cpp

Go to the documentation of this file.
00001 /*
00002  *
00003  * (C) 2003-2012 Anope Team
00004  * Contact us at team@anope.org
00005  *
00006  * Please read COPYING and README for further details.
00007  *
00008  * Based on the original code of Epona by Lara.
00009  * Based on the original code of Services by Andy Church.
00010  */
00011 
00012 #include "service.h"
00013 #include "access.h"
00014 #include "regchannel.h"
00015 #include "users.h"
00016 #include "account.h"
00017 
00018 Privilege::Privilege(const Anope::string &n, const Anope::string &d, int r) : name(n), desc(d), rank(r)
00019 {
00020 }
00021 
00022 bool Privilege::operator==(const Privilege &other) const
00023 {
00024         return this->name == other.name;
00025 }
00026 
00027 std::vector<Privilege> PrivilegeManager::privs;
00028 
00029 void PrivilegeManager::AddPrivilege(Privilege p)
00030 {
00031         unsigned i;
00032         for (i = 0; i < privs.size(); ++i)
00033         {
00034                 Privilege &priv = privs[i];
00035 
00036                 if (priv.rank > p.rank)
00037                         break;
00038         }
00039         
00040         privs.insert(privs.begin() + i, p);
00041 }
00042 
00043 void PrivilegeManager::RemovePrivilege(Privilege &p)
00044 {
00045         std::vector<Privilege>::iterator it = std::find(privs.begin(), privs.end(), p);
00046         if (it != privs.end())
00047                 privs.erase(it);
00048 
00049         for (registered_channel_map::const_iterator cit = RegisteredChannelList->begin(), cit_end = RegisteredChannelList->end(); cit != cit_end; ++cit)
00050         {
00051                 cit->second->QueueUpdate();
00052                 cit->second->RemoveLevel(p.name);
00053         }
00054 }
00055 
00056 Privilege *PrivilegeManager::FindPrivilege(const Anope::string &name)
00057 {
00058         for (unsigned i = privs.size(); i > 0; --i)
00059                 if (privs[i - 1].name.equals_ci(name))
00060                         return &privs[i - 1];
00061         return NULL;
00062 }
00063 
00064 std::vector<Privilege> &PrivilegeManager::GetPrivileges()
00065 {
00066         return privs;
00067 }
00068 
00069 void PrivilegeManager::ClearPrivileges()
00070 {
00071         privs.clear();
00072 }
00073 
00074 AccessProvider::AccessProvider(Module *o, const Anope::string &n) : Service(o, "AccessProvider", n)
00075 {
00076 }
00077 
00078 AccessProvider::~AccessProvider()
00079 {
00080 }
00081 
00082 ChanAccess::ChanAccess(AccessProvider *p) : provider(p)
00083 {
00084 }
00085 
00086 ChanAccess::~ChanAccess()
00087 {
00088 }
00089 
00090 const Anope::string ChanAccess::serialize_name() const
00091 {
00092         return "ChanAccess";
00093 }
00094 
00095 Serialize::Data ChanAccess::serialize() const
00096 {
00097         Serialize::Data data;
00098 
00099         data["provider"] << this->provider->name;
00100         data["ci"] << this->ci->name;
00101         data["mask"] << this->mask;
00102         data["creator"] << this->creator;
00103         data["last_seen"].setType(Serialize::DT_INT) << this->last_seen;
00104         data["created"].setType(Serialize::DT_INT) << this->created;
00105         data["data"] << this->Serialize();
00106 
00107         return data;
00108 }
00109 
00110 Serializable* ChanAccess::unserialize(Serializable *obj, Serialize::Data &data)
00111 {
00112         service_reference<AccessProvider> aprovider("AccessProvider", data["provider"].astr());
00113         ChannelInfo *ci = cs_findchan(data["ci"].astr());
00114         if (!aprovider || !ci)
00115                 return NULL;
00116 
00117         ChanAccess *access;
00118         if (obj)
00119                 access = anope_dynamic_static_cast<ChanAccess *>(obj);
00120         else
00121                 access = aprovider->Create();
00122         access->ci = ci;
00123         data["mask"] >> access->mask;
00124         data["creator"] >> access->creator;
00125         data["last_seen"] >> access->last_seen;
00126         data["created"] >> access->created;
00127         access->Unserialize(data["data"].astr());
00128 
00129         if (!obj)
00130                 ci->AddAccess(access);
00131         return access;
00132 }
00133 
00134 bool ChanAccess::Matches(const User *u, const NickCore *nc) const
00135 {
00136         bool is_mask = this->mask.find_first_of("!@?*") != Anope::string::npos;
00137         if (u && is_mask && Anope::Match(u->nick, this->mask))
00138                 return true;
00139         else if (u && Anope::Match(u->GetDisplayedMask(), this->mask))
00140                 return true;
00141         else if (nc)
00142                 for (std::list<serialize_obj<NickAlias> >::const_iterator it = nc->aliases.begin(); it != nc->aliases.end();)
00143                 {
00144                         const NickAlias *na = *it++;
00145                         if (na && Anope::Match(na->nick, this->mask))
00146                                 return true;
00147                 }
00148         return false;
00149 }
00150 
00151 bool ChanAccess::operator>(const ChanAccess &other) const
00152 {
00153         const std::vector<Privilege> &privs = PrivilegeManager::GetPrivileges();
00154         for (unsigned i = privs.size(); i > 0; --i)
00155         {
00156                 bool this_p = this->HasPriv(privs[i - 1].name),
00157                         other_p = other.HasPriv(privs[i - 1].name);
00158 
00159                 if (!this_p && !other_p)
00160                         continue;
00161                 else if (this_p && !other_p)
00162                         return true;
00163                 else
00164                         return false;
00165         }
00166 
00167         return false;
00168 }
00169 
00170 bool ChanAccess::operator<(const ChanAccess &other) const
00171 {
00172         const std::vector<Privilege> &privs = PrivilegeManager::GetPrivileges();
00173         for (unsigned i = privs.size(); i > 0; --i)
00174         {
00175                 bool this_p = this->HasPriv(privs[i - 1].name),
00176                         other_p = other.HasPriv(privs[i - 1].name);
00177 
00178                 if (!this_p && !other_p)
00179                         continue;
00180                 else if (!this_p && other_p)
00181                         return true;
00182                 else
00183                         return false;
00184         }       
00185 
00186         return false;
00187 }
00188 
00189 bool ChanAccess::operator>=(const ChanAccess &other) const
00190 {
00191         const std::vector<Privilege> &privs = PrivilegeManager::GetPrivileges();
00192         for (unsigned i = privs.size(); i > 0; --i)
00193         {
00194                 bool this_p = this->HasPriv(privs[i - 1].name),
00195                         other_p = other.HasPriv(privs[i - 1].name);
00196 
00197                 if (!this_p && !other_p)
00198                         continue;
00199                 else if (!this_p && other_p)
00200                         return false;
00201                 else
00202                         return true;
00203         }
00204 
00205         return true;
00206 }
00207 
00208 bool ChanAccess::operator<=(const ChanAccess &other) const
00209 {
00210         const std::vector<Privilege> &privs = PrivilegeManager::GetPrivileges();
00211         for (unsigned i = privs.size(); i > 0; --i)
00212         {
00213                 bool this_p = this->HasPriv(privs[i - 1].name),
00214                         other_p = other.HasPriv(privs[i - 1].name);
00215 
00216                 if (!this_p && !other_p)
00217                         continue;
00218                 else if (this_p && !other_p)
00219                         return false;
00220                 else
00221                         return true;
00222         }
00223 
00224         return true;
00225 }
00226 
00227 AccessGroup::AccessGroup() : std::vector<ChanAccess *>()
00228 {
00229         this->ci = NULL;
00230         this->nc = NULL;
00231         this->SuperAdmin = this->Founder = false;
00232 }
00233 
00234 bool AccessGroup::HasPriv(const Anope::string &name) const
00235 {
00236         if (this->SuperAdmin)
00237                 return true;
00238         else if (ci->GetLevel(name) == ACCESS_INVALID)
00239                 return false;
00240         else if (this->Founder)
00241                 return true;
00242         EventReturn MOD_RESULT;
00243         FOREACH_RESULT(I_OnGroupCheckPriv, OnGroupCheckPriv(this, name));
00244         if (MOD_RESULT != EVENT_CONTINUE)
00245                 return MOD_RESULT == EVENT_ALLOW;
00246         for (unsigned i = this->size(); i > 0; --i)
00247         {
00248                 ChanAccess *access = this->at(i - 1);
00249                 FOREACH_RESULT(I_OnCheckPriv, OnCheckPriv(access, name));
00250                 if (MOD_RESULT == EVENT_ALLOW || access->HasPriv(name))
00251                         return true;
00252         }
00253         return false;
00254 }
00255 
00256 const ChanAccess *AccessGroup::Highest() const
00257 {
00258         const std::vector<Privilege> &privs = PrivilegeManager::GetPrivileges();
00259         for (unsigned i = privs.size(); i > 0; --i)
00260                 for (unsigned j = this->size(); j > 0; --j)
00261                         if (this->at(j - 1)->HasPriv(privs[i - 1].name))
00262                                 return this->at(j - 1);
00263         return NULL;
00264 }
00265 
00266 bool AccessGroup::operator>(const AccessGroup &other) const
00267 {
00268         if (this->SuperAdmin)
00269                 return true;
00270         else if (other.SuperAdmin)
00271                 return false;
00272         else if (this->Founder && !other.Founder)
00273                 return true;
00274         else if (!this->Founder && other.Founder)
00275                 return false;
00276         const std::vector<Privilege> &privs = PrivilegeManager::GetPrivileges();
00277         for (unsigned i = privs.size(); i > 0; --i)
00278         {
00279                 bool this_p = this->HasPriv(privs[i - 1].name),
00280                         other_p = other.HasPriv(privs[i - 1].name);
00281 
00282                 if (!this_p && !other_p)
00283                         continue;
00284                 else if (this_p && !other_p)
00285                         return true;
00286                 else
00287                         return false;
00288         }
00289 
00290         return false;
00291 }
00292 
00293 bool AccessGroup::operator<(const AccessGroup &other) const
00294 {
00295         if (other.SuperAdmin)
00296                 return true;
00297         else if (this->SuperAdmin)
00298                 return false;
00299         else if (other.Founder && !this->Founder)
00300                 return true;
00301         else if (this->Founder && !other.Founder)
00302                 return false;
00303         const std::vector<Privilege> &privs = PrivilegeManager::GetPrivileges();
00304         for (unsigned i = privs.size(); i > 0; --i)
00305         {
00306                 bool this_p = this->HasPriv(privs[i - 1].name),
00307                         other_p = other.HasPriv(privs[i - 1].name);
00308 
00309                 if (!this_p && !other_p)
00310                         continue;
00311                 else if (!this_p && other_p)
00312                         return true;
00313                 else
00314                         return false;
00315         }
00316 
00317         return false;
00318 }
00319 
00320 bool AccessGroup::operator>=(const AccessGroup &other) const
00321 {
00322         if (this->SuperAdmin)
00323                 return true;
00324         else if (other.SuperAdmin)
00325                 return false;
00326         else if (this->Founder)
00327                 return true;
00328         else if (other.Founder)
00329                 return false;
00330         const std::vector<Privilege> &privs = PrivilegeManager::GetPrivileges();
00331         for (unsigned i = privs.size(); i > 0; --i)
00332         {
00333                 bool this_p = this->HasPriv(privs[i - 1].name),
00334                         other_p = other.HasPriv(privs[i - 1].name);
00335 
00336                 if (!this_p && !other_p)
00337                         continue;
00338                 else if (other_p && !this_p)
00339                         return false;
00340                 else
00341                         return true;
00342         }
00343 
00344         return true;
00345 }
00346 
00347 bool AccessGroup::operator<=(const AccessGroup &other) const
00348 {
00349         if (other.SuperAdmin)
00350                 return true;
00351         else if (this->SuperAdmin)
00352                 return false;
00353         else if (other.Founder)
00354                 return true;
00355         else if (this->Founder)
00356                 return false;
00357         const std::vector<Privilege> &privs = PrivilegeManager::GetPrivileges();
00358         for (unsigned i = privs.size(); i > 0; --i)
00359         {
00360                 bool this_p = this->HasPriv(privs[i - 1].name),
00361                         other_p = other.HasPriv(privs[i - 1].name);
00362 
00363                 if (!this_p && !other_p)
00364                         continue;
00365                 else if (this_p && !other_p)
00366                         return false;
00367                 else
00368                         return true;
00369         }
00370 
00371         return true;
00372 }
00373