1
0
mirror of https://github.com/anope/anope.git synced 2026-07-05 18:43:12 +02:00

Moved some global functions to be member functions and misc cleanup

This commit is contained in:
Adam
2011-04-25 03:16:57 -04:00
parent 6922bd239c
commit 076ebafa1b
30 changed files with 229 additions and 306 deletions
+1 -24
View File
@@ -30,7 +30,7 @@ bool bad_password(User *u)
u->invalid_pw_time = Anope::CurTime;
if (u->invalid_pw_count >= Config->BadPassLimit)
{
kill_user("", u, "Too many invalid passwords");
u->Kill(Config->ServerName, "Too many invalid passwords");
return true;
}
@@ -39,29 +39,6 @@ bool bad_password(User *u)
/*************************************************************************/
/**
* Remove a user from the IRC network.
* @param source is the nick which should generate the kill, or empty for a server-generated kill.
* @param user to remove
* @param reason for the kill
* @return void
*/
void kill_user(const Anope::string &source, User *user, const Anope::string &reason)
{
if (!user)
return;
Anope::string real_source = source.empty() ? Config->ServerName : source;
Anope::string buf = real_source + " (" + reason + ")";
ircdproto->SendSVSKill(findbot(source), user, "%s", buf.c_str());
if (!ircd->quitonkill)
do_kill(user, buf);
}
/*************************************************************************/
/**
* Unban the user from a channel
+2 -2
View File
@@ -77,7 +77,7 @@ static const char Pad64 = '=';
characters followed by one "=" padding character.
*/
void b64_encode(const Anope::string &src, Anope::string &target)
void Anope::B64Encode(const Anope::string &src, Anope::string &target)
{
size_t src_pos = 0, src_len = src.length();
unsigned char input[3];
@@ -118,7 +118,7 @@ void b64_encode(const Anope::string &src, Anope::string &target)
src from base - 64 numbers into three 8 bit bytes in the target area.
*/
void b64_decode(const Anope::string &src, Anope::string &target)
void Anope::B64Decode(const Anope::string &src, Anope::string &target)
{
target.clear();
-17
View File
@@ -1168,23 +1168,6 @@ void chan_set_correct_modes(User *user, Channel *c, int give_modes)
/*************************************************************************/
/** Set modes on every channel
* This overrides mlock on channels
* @param bi The bot to send the modes from
* @param modes The modes
*/
void MassChannelModes(BotInfo *bi, const Anope::string &modes)
{
for (channel_map::const_iterator it = ChannelList.begin(), it_end = ChannelList.end(); it != it_end; ++it)
{
Channel *c = it->second;
if (c->bouncy_modes)
return;
c->SetModes(bi, false, "%s", modes.c_str());
}
}
static const Anope::string EntryFlagString[] = { "ENTRYTYPE_NONE", "ENTRYTYPE_CIDR", "ENTRYTYPE_NICK_WILD", "ENTRYTYPE_NICK", "ENTRYTYPE_USER_WILD", "ENTRYTYPE_USER", "ENTRYTYPE_HOST_WILD", "ENTRYTYPE_HOST", "" };
/** Constructor
-35
View File
@@ -106,41 +106,6 @@ int levelinfo_maxwidth = 0;
/*************************************************************************/
/* Returns modes for mlock in a nice way. */
Anope::string get_mlock_modes(ChannelInfo *ci, int complete)
{
if (!ci)
return "";
Anope::string pos = "+", neg = "-", params;
for (std::multimap<ChannelModeName, ModeLock>::const_iterator it = ci->GetMLock().begin(), it_end = ci->GetMLock().end(); it != it_end; ++it)
{
const ModeLock &ml = it->second;
ChannelMode *cm = ModeManager::FindChannelModeByName(ml.name);
if (!cm || cm->Type == MODE_LIST || cm->Type == MODE_STATUS)
continue;
if (ml.set)
pos += cm->ModeChar;
else
neg += cm->ModeChar;
if (complete && !ml.param.empty() && cm->Type == MODE_PARAM)
params += " " + ml.param;
}
if (pos.length() == 1)
pos.clear();
if (neg.length() == 1)
neg.clear();
return pos + neg + params;
}
/*************************************************************************/
/* Check the current modes on a channel; if they conflict with a mode lock,
* fix them.
*/
-4
View File
@@ -209,10 +209,6 @@ static void write_pidfile()
/*************************************************************************/
/* Overall initialization routines. Return 0 on success, -1 on failure. */
int openlog_failed = 0, openlog_errno = 0;
void Init(int ac, char **av)
{
int started_from_term = isatty(0) && isatty(1) && isatty(2);
+2 -2
View File
@@ -205,7 +205,7 @@ static void services_shutdown()
FOREACH_MOD(I_OnShutdown, OnShutdown());
ModuleManager::UnloadAll();
/* just in case they weren't all removed at least run once */
ModuleRunTimeDirCleanUp();
ModuleManager::CleanupRuntimeDirectory();
}
/*************************************************************************/
@@ -402,7 +402,7 @@ int main(int ac, char **av, char **envp)
#endif
/* Clean out the module runtime directory prior to running, just in case files were left behind during a previous run */
ModuleRunTimeDirCleanUp();
ModuleManager::CleanupRuntimeDirectory();
/* General initialization first */
Init(ac, av);
-42
View File
@@ -454,28 +454,6 @@ Anope::string myStrGetTokenRemainder(const Anope::string &str, const char dilim,
/*************************************************************************/
/**
* Kill the user to enforce the sqline
* @param nick to kill
* @param killer whom is doing the killing
* @return void
*/
void EnforceQlinedNick(const Anope::string &nick, const Anope::string &killer)
{
if (findbot(nick))
return;
User *u2 = finduser(nick);
if (u2)
{
Log(LOG_NORMAL, "xline") << "Killed Q-lined nick: " << u2->GetMask();
kill_user(killer, u2, "This nick is reserved for Services. Please use a non Q-Lined nick.");
}
}
/*************************************************************************/
/**
* Is the given nick a network service
* @param nick to check
@@ -691,26 +669,6 @@ std::vector<Anope::string> BuildStringVector(const Anope::string &src, char deli
/*************************************************************************/
/**
* Change an unsigned string to a signed string, overwriting the original
* string.
* @param input string
* @return output string, same as input string.
*/
char *str_signed(unsigned char *str)
{
char *nstr = reinterpret_cast<char *>(str);
while (*str)
{
*nstr = static_cast<char>(*str);
++str;
++nstr;
}
return nstr;
}
bool Anope::Match(const Anope::string &str, const Anope::string &mask, bool case_sensitive)
{
size_t s = 0, m = 0, str_len = str.length(), mask_len = mask.length();
+86 -6
View File
@@ -12,6 +12,60 @@
std::map<Anope::string, Service *> ModuleManager::ServiceProviders;
std::vector<Module *> ModuleManager::EventHandlers[I_END];
void ModuleManager::CleanupRuntimeDirectory()
{
Anope::string dirbuf = services_dir + "/modules/runtime";
Log(LOG_DEBUG) << "Cleaning out Module run time directory (" << dirbuf << ") - this may take a moment please wait";
#ifndef _WIN32
DIR *dirp = opendir(dirbuf.c_str());
if (!dirp)
{
Log(LOG_DEBUG) << "Cannot open directory (" << dirbuf << ")";
return;
}
struct dirent *dp;
while ((dp = readdir(dirp)))
{
if (!dp->d_ino)
continue;
if (Anope::string(dp->d_name).equals_cs(".") || Anope::string(dp->d_name).equals_cs(".."))
continue;
Anope::string filebuf = dirbuf + "/" + dp->d_name;
DeleteFile(filebuf.c_str());
}
closedir(dirp);
#else
Anope::string szDir = dirbuf + "/*";
WIN32_FIND_DATA FileData;
HANDLE hList = FindFirstFile(szDir.c_str(), &FileData);
if (hList != INVALID_HANDLE_VALUE)
{
bool fFinished = false;
while (!fFinished)
{
if (!(FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
Anope::string filebuf = dirbuf + "/" + FileData.cFileName;
if (!DeleteFile(filebuf.c_str()))
Log(LOG_DEBUG) << "Error deleting file " << filebuf << " - GetLastError() reports " << Anope::LastError();
}
if (!FindNextFile(hList, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
fFinished = true;
}
}
}
else
Log(LOG_DEBUG) << "Invalid File Handle. GetLastError() reports "<< static_cast<int>(GetLastError());
FindClose(hList);
#endif
}
void ModuleManager::LoadModuleList(std::list<Anope::string> &ModuleList)
{
for (std::list<Anope::string>::iterator it = ModuleList.begin(), it_end = ModuleList.end(); it != it_end; ++it)
@@ -215,6 +269,35 @@ ModuleReturn ModuleManager::UnloadModule(Module *m, User *u)
return MOD_ERR_OK;
}
void ModuleManager::RequireVersion(int major, int minor, int patch, int build)
{
if (Anope::VersionMajor() > major)
return;
else if (Anope::VersionMajor() == major)
{
if (minor == -1)
return;
else if (Anope::VersionMinor() > minor)
return;
else if (Anope::VersionMinor() == minor)
{
if (patch == -1)
return;
else if (Anope::VersionPatch() > patch)
return;
else if (Anope::VersionPatch() == patch)
{
if (build == -1)
return;
else if (Anope::VersionBuild() >= build)
return;
}
}
}
throw ModuleException("This module requires version " + stringify(major) + "." + stringify(minor) + "." + stringify(patch) + "-" + build + " - this is " + Anope::Version());
}
void ModuleManager::DeleteModule(Module *m)
{
if (!m || !m->handle)
@@ -226,7 +309,7 @@ void ModuleManager::DeleteModule(Module *m)
ano_modclearerr();
void (*destroy_func)(Module *m) = function_cast<void (*)(Module *)>(dlsym(m->handle, "AnopeFini"));
const char *err = ano_moderr();
if (!destroy_func && err && *err)
if (!destroy_func || err)
{
Log() << "No destroy function found for " << m->name << ", chancing delete...";
delete m; /* we just have to chance they haven't overwrote the delete operator then... */
@@ -234,11 +317,8 @@ void ModuleManager::DeleteModule(Module *m)
else
destroy_func(m); /* Let the module delete it self, just in case */
if (handle)
{
if (dlclose(handle))
Log() << ano_moderr();
}
if (dlclose(handle))
Log() << ano_moderr();
if (!filename.empty())
DeleteFile(filename.c_str());
-100
View File
@@ -179,106 +179,6 @@ int Module::DelCommand(BotInfo *bi, Command *c)
return MOD_ERR_OK;
}
/*******************************************************************************
* Module Callback Functions
*******************************************************************************/
/* Check the current version of anope against a given version number
* Specifiying -1 for minor,patch or build
* @param major The major version of anope, the first part of the verison number
* @param minor The minor version of anope, the second part of the version number
* @param patch The patch version of anope, the third part of the version number
* @param build The build revision of anope from SVN
* @return True if the version newer than the version specified.
**/
bool moduleMinVersion(int major, int minor, int patch, int build)
{
bool ret = false;
if (Anope::VersionMajor() > major) /* Def. new */
ret = true;
else if (Anope::VersionMajor() == major) /* Might be newer */
{
if (minor == -1)
return true; /* They dont care about minor */
if (Anope::VersionMinor() > minor) /* Def. newer */
ret = true;
else if (Anope::VersionMinor() == minor) /* Might be newer */
{
if (patch == -1)
return true; /* They dont care about patch */
if (Anope::VersionPatch() > patch)
ret = true;
else if (Anope::VersionPatch() == patch)
{
#if 0
// XXX
if (build == -1)
return true; /* They dont care about build */
if (Anope::VersionBuild >= build)
ret = true;
#endif
}
}
}
return ret;
}
void ModuleRunTimeDirCleanUp()
{
Anope::string dirbuf = services_dir + "/modules/runtime";
Log(LOG_DEBUG) << "Cleaning out Module run time directory (" << dirbuf << ") - this may take a moment please wait";
#ifndef _WIN32
DIR *dirp = opendir(dirbuf.c_str());
if (!dirp)
{
Log(LOG_DEBUG) << "Cannot open directory (" << dirbuf << ")";
return;
}
struct dirent *dp;
while ((dp = readdir(dirp)))
{
if (!dp->d_ino)
continue;
if (Anope::string(dp->d_name).equals_cs(".") || Anope::string(dp->d_name).equals_cs(".."))
continue;
Anope::string filebuf = dirbuf + "/" + dp->d_name;
DeleteFile(filebuf.c_str());
}
closedir(dirp);
#else
Anope::string szDir = dirbuf + "/*";
WIN32_FIND_DATA FileData;
HANDLE hList = FindFirstFile(szDir.c_str(), &FileData);
if (hList != INVALID_HANDLE_VALUE)
{
bool fFinished = false;
while (!fFinished)
{
if (!(FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
Anope::string filebuf = dirbuf + "/" + FileData.cFileName;
if (!DeleteFile(filebuf.c_str()))
Log(LOG_DEBUG) << "Error deleting file " << filebuf << " - GetLastError() reports " << Anope::LastError();
}
if (!FindNextFile(hList, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
fFinished = true;
}
}
}
else
Log(LOG_DEBUG) << "Invalid File Handle. GetLastError() reports "<< static_cast<int>(GetLastError());
FindClose(hList);
#endif
Log(LOG_DEBUG) << "Module run time directory has been cleaned out";
}
void Module::SendMessage(CommandSource &source, const char *fmt, ...)
{
Anope::string language = (source.u && source.u->Account() ? source.u->Account()->language : "");
+7 -7
View File
@@ -374,7 +374,7 @@ void SGLineManager::Del(XLine *x)
void SGLineManager::OnMatch(User *u, XLine *x)
{
if (u)
kill_user(Config->s_OperServ, u, x->Reason);
u->Kill(Config->s_OperServ, x->Reason);
ircdproto->SendAkill(u, x);
}
@@ -413,7 +413,7 @@ XLine *SNLineManager::Add(const Anope::string &mask, const Anope::string &creato
++it;
if (!user->HasMode(UMODE_OPER) && user->server != Me && Anope::Match(user->realname, x->Mask))
kill_user(Config->ServerName, user, rreason);
user->Kill(Config->ServerName, rreason);
}
}
@@ -430,7 +430,7 @@ void SNLineManager::OnMatch(User *u, XLine *x)
if (u)
{
Anope::string reason = "G-Lined: " + x->Reason;
kill_user(Config->s_OperServ, u, reason);
u->Kill(Config->s_OperServ, reason);
}
this->Send(u, x);
}
@@ -516,7 +516,7 @@ XLine *SQLineManager::Add(const Anope::string &mask, const Anope::string &creato
++it;
if (!user->HasMode(UMODE_OPER) && user->server != Me && Anope::Match(user->nick, x->Mask))
kill_user(Config->ServerName, user, rreason);
user->Kill(Config->ServerName, rreason);
}
}
}
@@ -537,10 +537,10 @@ void SQLineManager::OnMatch(User *u, XLine *x)
if (u)
{
Anope::string reason = "Q-Lined: " + x->Reason;
kill_user(Config->s_OperServ, u, reason);
u->Kill(Config->s_OperServ, reason);
}
ircdproto->SendSQLine(u, x);
this->Send(u, x);
}
void SQLineManager::OnExpire(XLine *x)
@@ -600,7 +600,7 @@ void SZLineManager::OnMatch(User *u, XLine *x)
if (u)
{
Anope::string reason = "Z-Lined: " + x->Reason;
kill_user(Config->s_OperServ, u, reason);
u->Kill(Config->s_OperServ, reason);
}
ircdproto->SendSZLine(u, x);
+28
View File
@@ -736,6 +736,34 @@ ModeLock *ChannelInfo::GetMLock(ChannelModeName mname, const Anope::string &para
return NULL;
}
Anope::string ChannelInfo::GetMLockAsString(bool complete) const
{
Anope::string pos = "+", neg = "-", params;
for (std::multimap<ChannelModeName, ModeLock>::const_iterator it = this->GetMLock().begin(), it_end = this->GetMLock().end(); it != it_end; ++it)
{
const ModeLock &ml = it->second;
ChannelMode *cm = ModeManager::FindChannelModeByName(ml.name);
if (!cm || cm->Type == MODE_LIST || cm->Type == MODE_STATUS)
continue;
if (ml.set)
pos += cm->ModeChar;
else
neg += cm->ModeChar;
if (complete && !ml.param.empty() && cm->Type == MODE_PARAM)
params += " " + ml.param;
}
if (pos.length() == 1)
pos.clear();
if (neg.length() == 1)
neg.clear();
return pos + neg + params;
}
/** Check whether a user is permitted to be on this channel
* @param u The user
* @return true if they were banned, false if they are allowed
+13 -20
View File
@@ -261,7 +261,7 @@ void User::SendMessage(BotInfo *source, Anope::string msg)
* back to call do_nick. do_nick changes the nick of the use to the new one, then calls NickAlias::OnCancel
* with the users old nick's nickalias (if there is one).
*
* 2. Calls kill_user, which will either delete the user immediatly or kill them, wait for the QUIT,
* 2. Calls User::Kill, which will either delete the user immediatly or kill them, wait for the QUIT,
* then delete the user then. Users destructor then calls NickAlias::OnCancel
*
* NickAlias::OnCancel checks for NS_COLLIDED, it then does one of two things.
@@ -324,7 +324,7 @@ void User::Collide(NickAlias *na)
ircdproto->SendForceNickChange(this, guestnick, Anope::CurTime);
}
else
kill_user(Config->s_NickServ, this, "Services nickname-enforcer kill");
this->Kill(Config->s_NickServ, "Services nickname-enforcer kill");
}
/** Identify the user to the Nick
@@ -711,6 +711,17 @@ bool User::IsProtected() const
return false;
}
void User::Kill(const Anope::string &source, const Anope::string &reason)
{
Anope::string real_source = source.empty() ? Config->ServerName : source;
Anope::string real_reason = real_source + " (" + reason + ")";
ircdproto->SendSVSKill(findbot(source), this, "%s", real_reason.c_str());
if (!ircd->quitonkill)
do_kill(this, real_reason);
}
User *finduser(const Anope::string &nick)
{
if (isdigit(nick[0]) && ircd->ts6)
@@ -902,24 +913,6 @@ bool matches_list(Channel *c, User *user, ChannelModeName mode)
/*************************************************************************/
/* Is the given MASK ban-excepted? */
bool is_excepted_mask(ChannelInfo *ci, const Anope::string &mask)
{
if (!ci->c || !ModeManager::FindChannelModeByName(CMODE_EXCEPT))
return false;
std::pair<Channel::ModeList::iterator, Channel::ModeList::iterator> modes = ci->c->GetModeList(CMODE_EXCEPT);
for (; modes.first != modes.second; ++modes.first)
{
if (Anope::Match(modes.first->second, mask))
return true;
}
return false;
}
/*************************************************************************/
/* Given a user, return a mask that will most likely match any address the
* user will have from that location. For IP addresses, wildcards the
* appropriate subnet mask (e.g. 35.1.1.1 -> 35.*; 128.2.1.1 -> 128.2.*);