From 3ff8e96e14606c9251d52848ce7b21dc2abc8fa4 Mon Sep 17 00:00:00 2001 From: cyberbotx Date: Mon, 23 Mar 2009 00:06:40 +0000 Subject: Replace manual array for ChannelInfo's access list to use an std::vector instead, cleans up the code by a huge portion and fixes bug #1024 in the process. git-svn-id: http://anope.svn.sourceforge.net/svnroot/anope/trunk@2184 5417fbe8-f217-4b02-8779-1006273d7864 --- include/extern.h | 1 - include/regchannel.h | 101 +++++++++++++++++++++++++++++++++++++++++++++++---- include/services.h | 45 ++++++++++++----------- 3 files changed, 116 insertions(+), 31 deletions(-) (limited to 'include') diff --git a/include/extern.h b/include/extern.h index 0539593a0..ad79cf00c 100644 --- a/include/extern.h +++ b/include/extern.h @@ -185,7 +185,6 @@ E ChannelInfo *cs_findchan(const char *chan); E int check_access(User * user, ChannelInfo * ci, int what); E int is_founder(User * user, ChannelInfo * ci); E int get_access(User * user, ChannelInfo * ci); -E ChanAccess *get_access_entry(NickCore * nc, ChannelInfo * ci); E void update_cs_lastseen(User * user, ChannelInfo * ci); E int get_idealban(ChannelInfo * ci, User * u, char *ret, int retlen); E AutoKick *is_stuck(ChannelInfo * ci, const char *mask); diff --git a/include/regchannel.h b/include/regchannel.h index a295acb39..e3f9cd7fc 100644 --- a/include/regchannel.h +++ b/include/regchannel.h @@ -8,8 +8,8 @@ * $Id$ * */ - -class ChannelInfo : public Extensible + +class CoreExport ChannelInfo : public Extensible { public: ChannelInfo() @@ -20,9 +20,8 @@ class ChannelInfo : public Extensible desc = url = email = last_topic = forbidby = forbidreason = NULL; time_registered = last_used = last_topic_time = 0; flags = 0; - bantype = accesscount = akickcount = 0; + bantype = akickcount = 0; levels = NULL; - access = NULL; akick = NULL; mlock_on = mlock_off = mlock_limit = 0; mlock_key = mlock_flood = mlock_redirect = entry_message = NULL; @@ -36,7 +35,7 @@ class ChannelInfo : public Extensible floodlines = floodsecs = 0; repeattimes = 0; } - + ChannelInfo *next, *prev; char name[CHANMAX]; NickCore *founder; @@ -60,8 +59,7 @@ class ChannelInfo : public Extensible int16 bantype; int16 *levels; /* Access levels for commands */ - uint16 accesscount; - ChanAccess *access; /* List of authorized users */ + std::vector access; /* List of authorized users */ uint16 akickcount; AutoKick *akick; /* List of users to kickban */ @@ -89,5 +87,92 @@ class ChannelInfo : public Extensible int16 capsmin, capspercent; /* For CAPS kicker */ int16 floodlines, floodsecs; /* For FLOOD kicker */ int16 repeattimes; /* For REPEAT kicker */ -}; + /** Add an entry to the channel access list + * + * @param nc The NickCore of the user that the access entry should be tied to + * @param level The channel access level the user has on the channel + * @param last_seen When the user was last seen within the channel + * + * Creates a new access list entry and inserts it into the access list. + */ + void AddAccess(NickCore *nc, int16 level, int32 last_seen = 0) + { + ChanAccess *new_access = new ChanAccess; + new_access->in_use = 1; + new_access->nc = nc; + new_access->level = level; + new_access->last_seen = last_seen; + access.push_back(new_access); + } + + /** Get an entry from the channel access list by index + * + * @param index The index in the access list vector + * @return A ChanAccess struct corresponding to the index given, or NULL if outside the bounds + * + * Retrieves an entry from the access list that matches the given index. + */ + ChanAccess *GetAccess(unsigned index) + { + if (access.empty() || index >= access.size()) + return NULL; + + return access[index]; + } + + /** Get an entry from the channel access list by NickCore + * + * @param nc The NickCore to find within the access list vector + * @param level Optional channel access level to compare the access entries to + * @return A ChanAccess struct corresponding to the NickCore, or NULL if not found + * + * Retrieves an entry from the access list that matches the given NickCore, optionally also matching a certain level. + */ + ChanAccess *GetAccess(NickCore *nc, int16 level = 0) + { + if (access.empty()) + return NULL; + + for (unsigned i = 0; i < access.size(); i++) + if (access[i]->in_use && access[i]->nc == nc && (level ? access[i]->level == level : true)) + return access[i]; + + return NULL; + } + + /** Erase an entry from the channel access list + * + * @param index The index in the access list vector + * + * Clears the memory used by the given access entry and removes it from the vector. + */ + void EraseAccess(unsigned index) + { + if (access.empty() || index >= access.size()) + return; + delete access[index]; + access.erase(access.begin() + index); + } + + /** Cleans the channel access list + * + * Cleans up the access list so it no longer contains entries no longer in use. + */ + void CleanAccess() + { + for (unsigned j = access.size(); j > 0; --j) + if (!access[j - 1]->in_use) + EraseAccess(j - 1); + } + + /** Clear the entire channel access list + * + * Clears the entire access list by deleting every item and then clearing the vector. + */ + void ClearAccess() + { + while (access.begin() != access.end()) + EraseAccess(0); + } +}; diff --git a/include/services.h b/include/services.h index cd8c18d84..82631d883 100644 --- a/include/services.h +++ b/include/services.h @@ -597,38 +597,39 @@ typedef struct { /*************************************************************************/ +enum AccessLevel +{ + /* Note that these two levels also serve as exclusive boundaries for valid + * access levels. ACCESS_FOUNDER may be assumed to be strictly greater + * than any valid access level, and ACCESS_INVALID may be assumed to be + * strictly less than any valid access level. Also read below. + */ + ACCESS_FOUNDER = 10000, /* Numeric level indicating founder access */ + ACCESS_INVALID = -10000, /* Used in levels[] for disabled settings */ + /* There is one exception to the above access levels: SuperAdmins will have + * access level 10001. This level is never stored, however; it is only used + * in comparison and to let SuperAdmins win from founders where needed + */ + ACCESS_SUPERADMIN = 10001, + /* Levels for xOP */ + ACCESS_VOP = 3, + ACCESS_HOP = 4, + ACCESS_AOP = 5, + ACCESS_SOP = 10 +}; + /* Channel info structures. Stored similarly to the nicks, except that * the second character of the channel name, not the first, is used to * determine the list. (Hashing based on the first character of the name * wouldn't get very far. ;) ) */ /* Access levels for users. */ -typedef struct { +struct ChanAccess { uint16 in_use; /* 1 if this entry is in use, else 0 */ int16 level; NickCore *nc; /* Guaranteed to be non-NULL if in use, NULL if not */ time_t last_seen; -} ChanAccess; - -/* Note that these two levels also serve as exclusive boundaries for valid - * access levels. ACCESS_FOUNDER may be assumed to be strictly greater - * than any valid access level, and ACCESS_INVALID may be assumed to be - * strictly less than any valid access level. Also read below. - */ -#define ACCESS_FOUNDER 10000 /* Numeric level indicating founder access */ -#define ACCESS_INVALID -10000 /* Used in levels[] for disabled settings */ -/* There is one exception to the above access levels: SuperAdmins will have - * access level 10001. This level is never stored, however; it is only used - * in comparison and to let SuperAdmins win from founders where needed - */ -#define ACCESS_SUPERADMIN 10001 - -/* Levels for xOP */ - -#define ACCESS_VOP 3 -#define ACCESS_HOP 4 -#define ACCESS_AOP 5 -#define ACCESS_SOP 10 +}; /* AutoKick data. */ typedef struct { -- cgit