summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAttila Molnar <attilamolnar@hush.com>2013-06-05 17:52:39 -0700
committerAttila Molnar <attilamolnar@hush.com>2013-06-05 17:52:39 -0700
commitf00ac52c5d593fcb761fc316b2582bb06158035c (patch)
treebec88a8a69c4a912606636c9df6283c154962fef /src
parentd9d99cd02dadf34bfcc220734ba0c422f0acb3e6 (diff)
parent5d0b2b7cfccf057e7abab94c41065f94420899cd (diff)
Merge pull request #544 from SaberUK/master+kill-maxbuf
Purge MAXBUF in favour of a configuration option.
Diffstat (limited to 'src')
-rw-r--r--src/commands/cmd_commands.cpp9
-rw-r--r--src/commands/cmd_stats.cpp24
-rw-r--r--src/commands/cmd_whois.cpp2
-rw-r--r--src/configparser.cpp2
-rw-r--r--src/configreader.cpp39
-rw-r--r--src/logger.cpp4
-rw-r--r--src/mode.cpp2
-rw-r--r--src/modules/extra/m_ldapauth.cpp2
-rw-r--r--src/modules/extra/m_mssql.cpp4
-rw-r--r--src/modules/extra/m_ssl_gnutls.cpp4
-rw-r--r--src/modules/m_alias.cpp2
-rw-r--r--src/modules/m_blockcaps.cpp2
-rw-r--r--src/modules/m_check.cpp11
-rw-r--r--src/modules/m_ident.cpp4
-rw-r--r--src/modules/m_messageflood.cpp2
-rw-r--r--src/modules/m_permchannels.cpp74
-rw-r--r--src/modules/m_spanningtree/netburst.cpp70
-rw-r--r--src/modules/m_spanningtree/utils.cpp2
-rw-r--r--src/modules/m_xline_db.cpp78
-rw-r--r--src/socket.cpp24
-rw-r--r--src/users.cpp125
21 files changed, 185 insertions, 301 deletions
diff --git a/src/commands/cmd_commands.cpp b/src/commands/cmd_commands.cpp
index 36408b363..99acdfe96 100644
--- a/src/commands/cmd_commands.cpp
+++ b/src/commands/cmd_commands.cpp
@@ -53,12 +53,9 @@ CmdResult CommandCommands::Handle (const std::vector<std::string>&, User *user)
continue;
Module* src = i->second->creator;
- char buffer[MAXBUF];
- snprintf(buffer, MAXBUF, ":%s %03d %s :%s %s %d %d",
- ServerInstance->Config->ServerName.c_str(), RPL_COMMANDS, user->nick.c_str(),
- i->second->name.c_str(), src->ModuleSourceFile.c_str(),
- i->second->min_params, i->second->Penalty);
- list.push_back(buffer);
+ list.push_back(InspIRCd::Format(":%s %03d %s :%s %s %d %d", ServerInstance->Config->ServerName.c_str(),
+ RPL_COMMANDS, user->nick.c_str(), i->second->name.c_str(), src->ModuleSourceFile.c_str(),
+ i->second->min_params, i->second->Penalty));
}
sort(list.begin(), list.end());
for(unsigned int i=0; i < list.size(); i++)
diff --git a/src/commands/cmd_stats.cpp b/src/commands/cmd_stats.cpp
index 3145940be..9fef7bbd4 100644
--- a/src/commands/cmd_stats.cpp
+++ b/src/commands/cmd_stats.cpp
@@ -271,9 +271,9 @@ void CommandStats::DoStats(char statschar, User* user, string_list &results)
}
FILETIME CreationTime;
- FILETIME ExitTime;
- FILETIME KernelTime;
- FILETIME UserTime;
+ FILETIME ExitTime;
+ FILETIME KernelTime;
+ FILETIME UserTime;
LARGE_INTEGER ThisSample;
if(GetProcessTimes(GetCurrentProcess(), &CreationTime, &ExitTime, &KernelTime, &UserTime) &&
QueryPerformanceCounter(&ThisSample))
@@ -301,15 +301,13 @@ void CommandStats::DoStats(char statschar, User* user, string_list &results)
case 'T':
{
- char buffer[MAXBUF];
results.push_back(sn+" 249 "+user->nick+" :accepts "+ConvToStr(ServerInstance->stats->statsAccept)+" refused "+ConvToStr(ServerInstance->stats->statsRefused));
results.push_back(sn+" 249 "+user->nick+" :unknown commands "+ConvToStr(ServerInstance->stats->statsUnknown));
results.push_back(sn+" 249 "+user->nick+" :nick collisions "+ConvToStr(ServerInstance->stats->statsCollisions));
results.push_back(sn+" 249 "+user->nick+" :dns requests "+ConvToStr(ServerInstance->stats->statsDnsGood+ServerInstance->stats->statsDnsBad)+" succeeded "+ConvToStr(ServerInstance->stats->statsDnsGood)+" failed "+ConvToStr(ServerInstance->stats->statsDnsBad));
results.push_back(sn+" 249 "+user->nick+" :connection count "+ConvToStr(ServerInstance->stats->statsConnects));
- snprintf(buffer,MAXBUF," 249 %s :bytes sent %5.2fK recv %5.2fK",
- user->nick.c_str(),ServerInstance->stats->statsSent / 1024.0,ServerInstance->stats->statsRecv / 1024.0);
- results.push_back(sn+buffer);
+ results.push_back(InspIRCd::Format("%s 249 %s :bytes sent %5.2fK recv %5.2fK", sn.c_str(), user->nick.c_str(),
+ ServerInstance->stats->statsSent / 1024.0, ServerInstance->stats->statsRecv / 1024.0));
}
break;
@@ -382,15 +380,15 @@ void CommandStats::DoStats(char statschar, User* user, string_list &results)
* Craig suggested this, and it seemed a good idea so in it went */
if (stime->tm_year > 70)
{
- char buffer[MAXBUF];
- snprintf(buffer,MAXBUF," 242 %s :Server up %d years, %d days, %.2d:%.2d:%.2d",user->nick.c_str(),(stime->tm_year-70),stime->tm_yday,stime->tm_hour,stime->tm_min,stime->tm_sec);
- results.push_back(sn+buffer);
+ results.push_back(InspIRCd::Format("%s 242 %s :Server up %d years, %d days, %.2d:%.2d:%.2d",
+ sn.c_str(), user->nick.c_str(), stime->tm_year - 70, stime->tm_yday, stime->tm_hour,
+ stime->tm_min, stime->tm_sec));
}
else
{
- char buffer[MAXBUF];
- snprintf(buffer,MAXBUF," 242 %s :Server up %d days, %.2d:%.2d:%.2d",user->nick.c_str(),stime->tm_yday,stime->tm_hour,stime->tm_min,stime->tm_sec);
- results.push_back(sn+buffer);
+ results.push_back(InspIRCd::Format("%s 242 %s :Server up %d days, %.2d:%.2d:%.2d",
+ sn.c_str(), user->nick.c_str(), stime->tm_yday, stime->tm_hour, stime->tm_min,
+ stime->tm_sec));
}
}
break;
diff --git a/src/commands/cmd_whois.cpp b/src/commands/cmd_whois.cpp
index fea14f375..de3d71152 100644
--- a/src/commands/cmd_whois.cpp
+++ b/src/commands/cmd_whois.cpp
@@ -152,7 +152,7 @@ void CommandWhois::DoWhois(User* user, User* dest, unsigned long signon, unsigne
{
if (dest->IsModeSet('s') != 0)
{
- ServerInstance->SendWhoisLine(user, dest, 379, "%s %s :is using modes +%s +%s", user->nick.c_str(), dest->nick.c_str(), dest->FormatModes(), dest->FormatNoticeMasks());
+ ServerInstance->SendWhoisLine(user, dest, 379, "%s %s :is using modes +%s +%s", user->nick.c_str(), dest->nick.c_str(), dest->FormatModes(), dest->FormatNoticeMasks().c_str());
}
else
{
diff --git a/src/configparser.cpp b/src/configparser.cpp
index 61cdb55fa..31d314148 100644
--- a/src/configparser.cpp
+++ b/src/configparser.cpp
@@ -327,7 +327,7 @@ void ParseStack::DoReadFile(const std::string& key, const std::string& name, int
file_cache& cache = FilesOutput[key];
cache.clear();
- char linebuf[MAXBUF*10];
+ char linebuf[5120];
while (fgets(linebuf, sizeof(linebuf), file))
{
size_t len = strlen(linebuf);
diff --git a/src/configreader.cpp b/src/configreader.cpp
index a8c0abe89..905eb786a 100644
--- a/src/configreader.cpp
+++ b/src/configreader.cpp
@@ -367,10 +367,15 @@ void ServerConfig::Fill()
else
{
if (ServerName != ConfValue("server")->getString("name"))
- throw CoreException("You must restart to change the server name or SID");
+ throw CoreException("You must restart to change the server name");
+
std::string nsid = ConfValue("server")->getString("id");
if (!nsid.empty() && nsid != sid)
- throw CoreException("You must restart to change the server name or SID");
+ throw CoreException("You must restart to change the server id");
+
+ if (Limits.MaxLine != static_cast<size_t>(ConfValue("limits")->getInt("maxline", 512)))
+ throw CoreException("You must restart to change the maximum line length");
+
}
diepass = ConfValue("power")->getString("diepass");
restartpass = ConfValue("power")->getString("restartpass");
@@ -423,6 +428,7 @@ void ServerConfig::Fill()
Limits.MaxKick = ConfValue("limits")->getInt("maxkick", 255);
Limits.MaxGecos = ConfValue("limits")->getInt("maxgecos", 128);
Limits.MaxAway = ConfValue("limits")->getInt("maxaway", 200);
+ Limits.MaxLine = ConfValue("limits")->getInt("maxline", 512);
InvBypassModes = options->getBool("invitebypassmodes", true);
NoSnoticeStack = options->getBool("nosnoticestack", false);
@@ -767,14 +773,31 @@ bool ServerConfig::FileExists(const char* file)
if ((sb.st_mode & S_IFDIR) > 0)
return false;
- FILE *input = fopen(file, "r");
- if (input == NULL)
- return false;
- else
+ return !access(file, F_OK);
+}
+
+std::string ServerConfig::Escape(const std::string& str, bool xml)
+{
+ std::string escaped;
+ for (std::string::const_iterator it = str.begin(); it != str.end(); ++it)
{
- fclose(input);
- return true;
+ switch (*it)
+ {
+ case '"':
+ escaped += xml ? "&quot;" : "\"";
+ break;
+ case '&':
+ escaped += xml ? "&amp;" : "&";
+ break;
+ case '\\':
+ escaped += xml ? "\\" : "\\\\";
+ break;
+ default:
+ escaped += *it;
+ break;
+ }
}
+ return escaped;
}
const char* ServerConfig::CleanFilename(const char* name)
diff --git a/src/logger.cpp b/src/logger.cpp
index 1a63499d6..03960f4a1 100644
--- a/src/logger.cpp
+++ b/src/logger.cpp
@@ -116,10 +116,10 @@ void LogManager::OpenFileLogs()
std::map<std::string, FileWriter*>::iterator fwi = logmap.find(target);
if (fwi == logmap.end())
{
- char realtarget[MAXBUF];
+ char realtarget[256];
time_t time = ServerInstance->Time();
struct tm *mytime = gmtime(&time);
- strftime(realtarget, MAXBUF, target.c_str(), mytime);
+ strftime(realtarget, sizeof(realtarget), target.c_str(), mytime);
FILE* f = fopen(realtarget, "a");
fw = new FileWriter(f);
logmap.insert(std::make_pair(target, fw));
diff --git a/src/mode.cpp b/src/mode.cpp
index 578fc2c27..ac0b111b1 100644
--- a/src/mode.cpp
+++ b/src/mode.cpp
@@ -190,7 +190,7 @@ void ModeParser::DisplayCurrentModes(User *user, User* targetuser, Channel* targ
/* Display user's current mode string */
user->WriteNumeric(RPL_UMODEIS, "%s :+%s",targetuser->nick.c_str(),targetuser->FormatModes());
if ((targetuser->IsOper()))
- user->WriteNumeric(RPL_SNOMASKIS, "%s +%s :Server notice mask", targetuser->nick.c_str(), targetuser->FormatNoticeMasks());
+ user->WriteNumeric(RPL_SNOMASKIS, "%s +%s :Server notice mask", targetuser->nick.c_str(), targetuser->FormatNoticeMasks().c_str());
return;
}
else
diff --git a/src/modules/extra/m_ldapauth.cpp b/src/modules/extra/m_ldapauth.cpp
index 2d1551b43..0a0698fc2 100644
--- a/src/modules/extra/m_ldapauth.cpp
+++ b/src/modules/extra/m_ldapauth.cpp
@@ -212,7 +212,7 @@ public:
std::string> &replacements)
{
std::string result;
- result.reserve(MAXBUF);
+ result.reserve(text.length());
for (unsigned int i = 0; i < text.length(); ++i) {
char c = text[i];
diff --git a/src/modules/extra/m_mssql.cpp b/src/modules/extra/m_mssql.cpp
index 244aa0922..9d9622fde 100644
--- a/src/modules/extra/m_mssql.cpp
+++ b/src/modules/extra/m_mssql.cpp
@@ -472,8 +472,8 @@ class SQLConn : public classbase
if (sock->res_info->row_count > 0)
{
int cols = sock->res_info->num_cols;
- char** name = new char*[MAXBUF];
- char** data = new char*[MAXBUF];
+ char** name = new char*[512];
+ char** data = new char*[512];
for (int j=0; j<cols; j++)
{
TDSCOLUMN* col = sock->current_results->columns[j];
diff --git a/src/modules/extra/m_ssl_gnutls.cpp b/src/modules/extra/m_ssl_gnutls.cpp
index 8faee2da7..f9fddd5a1 100644
--- a/src/modules/extra/m_ssl_gnutls.cpp
+++ b/src/modules/extra/m_ssl_gnutls.cpp
@@ -803,8 +803,8 @@ class ModuleSSLGnuTLS : public Module
int ret;
unsigned int cert_list_size;
gnutls_x509_crt_t cert;
- char name[MAXBUF];
- unsigned char digest[MAXBUF];
+ char name[512];
+ unsigned char digest[512];
size_t digest_size = sizeof(digest);
size_t name_size = sizeof(name);
ssl_cert* certinfo = new ssl_cert;
diff --git a/src/modules/m_alias.cpp b/src/modules/m_alias.cpp
index 118cedeea..7343dd21b 100644
--- a/src/modules/m_alias.cpp
+++ b/src/modules/m_alias.cpp
@@ -309,7 +309,7 @@ class ModuleAlias : public Module
void DoCommand(const std::string& newline, User* user, Channel *chan, const std::string &original_line)
{
std::string result;
- result.reserve(MAXBUF);
+ result.reserve(newline.length());
for (unsigned int i = 0; i < newline.length(); i++)
{
char c = newline[i];
diff --git a/src/modules/m_blockcaps.cpp b/src/modules/m_blockcaps.cpp
index 0e1fa945f..c13549db8 100644
--- a/src/modules/m_blockcaps.cpp
+++ b/src/modules/m_blockcaps.cpp
@@ -119,7 +119,7 @@ public:
ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "<blockcaps:percent> out of range, setting to default of 100.");
percent = 100;
}
- if (minlen < 1 || minlen > MAXBUF-1)
+ if (minlen < 1 || minlen > ServerInstance->Config->Limits.MaxLine)
{
ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "<blockcaps:minlen> out of range, setting to default of 1.");
minlen = 1;
diff --git a/src/modules/m_check.cpp b/src/modules/m_check.cpp
index fda211cf8..734089c5b 100644
--- a/src/modules/m_check.cpp
+++ b/src/modules/m_check.cpp
@@ -194,12 +194,13 @@ class CommandCheck : public Command
/* note that unlike /names, we do NOT check +i vs in the channel */
for (UserMembCIter i = ulist->begin(); i != ulist->end(); i++)
{
- char tmpbuf[MAXBUF];
/*
- * Unlike Asuka, I define a clone as coming from the same host. --w00t
- */
- snprintf(tmpbuf, MAXBUF, "%-3lu %s%s (%s@%s) %s ", ServerInstance->Users->GlobalCloneCount(i->first), targchan->GetAllPrefixChars(i->first), i->first->nick.c_str(), i->first->ident.c_str(), i->first->dhost.c_str(), i->first->fullname.c_str());
- user->SendText(checkstr + " member " + tmpbuf);
+ * Unlike Asuka, I define a clone as coming from the same host. --w00t
+ */
+ user->SendText("%s member %-3lu %s%s (%s@%s) %s ",
+ checkstr.c_str(), ServerInstance->Users->GlobalCloneCount(i->first),
+ targchan->GetAllPrefixChars(i->first), i->first->nick.c_str(),
+ i->first->ident.c_str(), i->first->dhost.c_str(), i->first->fullname.c_str());
}
// We know that the mode handler for bans is in the core and is derived from ListModeBase
diff --git a/src/modules/m_ident.cpp b/src/modules/m_ident.cpp
index 00ff75f59..9f67a6242 100644
--- a/src/modules/m_ident.cpp
+++ b/src/modules/m_ident.cpp
@@ -213,8 +213,8 @@ class IdentRequestSocket : public EventHandler
/* We don't really need to buffer for incomplete replies here, since IDENT replies are
* extremely short - there is *no* sane reason it'd be in more than one packet
*/
- char ibuf[MAXBUF];
- int recvresult = ServerInstance->SE->Recv(this, ibuf, MAXBUF-1, 0);
+ char ibuf[256];
+ int recvresult = ServerInstance->SE->Recv(this, ibuf, sizeof(ibuf)-1, 0);
/* Close (but don't delete from memory) our socket
* and flag as done since the ident lookup has finished
diff --git a/src/modules/m_messageflood.cpp b/src/modules/m_messageflood.cpp
index 970087bef..65c3354a9 100644
--- a/src/modules/m_messageflood.cpp
+++ b/src/modules/m_messageflood.cpp
@@ -155,7 +155,7 @@ class ModuleMsgFlood : public Module
std::vector<std::string> parameters;
parameters.push_back(dest->name);
parameters.push_back("+b");
- parameters.push_back(user->MakeWildHost());
+ parameters.push_back("*!*@" + user->dhost);
ServerInstance->SendGlobalMode(parameters, ServerInstance->FakeClient);
}
diff --git a/src/modules/m_permchannels.cpp b/src/modules/m_permchannels.cpp
index 41477ba35..ff1a35ba9 100644
--- a/src/modules/m_permchannels.cpp
+++ b/src/modules/m_permchannels.cpp
@@ -19,6 +19,7 @@
#include "inspircd.h"
+#include <fstream>
/* $ModDesc: Provides support for channel mode +P to provide permanent channels */
@@ -26,79 +27,47 @@
static std::string permchannelsconf;
static bool WriteDatabase()
{
- FILE *f;
-
- if (permchannelsconf.empty())
- {
- // Fake success.
- return true;
- }
-
- std::string tempname = permchannelsconf + ".tmp";
-
/*
* We need to perform an atomic write so as not to fuck things up.
- * So, let's write to a temporary file, flush and sync the FD, then rename the file..
- * -- w00t
+ * So, let's write to a temporary file, flush it, then rename the file..
+ * -- w00t
*/
- f = fopen(tempname.c_str(), "w");
- if (!f)
+
+ // If the user has not specified a configuration file then we don't write one.
+ if (permchannelsconf.empty())
+ return true;
+
+ std::string permchannelsnewconf = permchannelsconf + ".tmp";
+ std::ofstream stream(permchannelsnewconf.c_str());
+ if (!stream.is_open())
{
ServerInstance->Logs->Log("m_permchannels", LOG_DEFAULT, "permchannels: Cannot create database! %s (%d)", strerror(errno), errno);
ServerInstance->SNO->WriteToSnoMask('a', "database: cannot create new db: %s (%d)", strerror(errno), errno);
return false;
}
+
+ stream << "# This file is automatically generated by m_permchannels. Any changes will be overwritten." << std::endl
+ << "<config format=\"xml\">" << std::endl;
- fputs("# Permchannels DB\n# This file is autogenerated; any changes will be overwritten!\n<config format=\"compat\">\n", f);
- // Now, let's write.
for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); i++)
{
Channel* chan = i->second;
if (!chan->IsModeSet('P'))
continue;
- char line[1024];
- const char* items[] =
- {
- "<permchannels channel=",
- chan->name.c_str(),
- " topic=",
- chan->topic.c_str(),
- " modes=",
- chan->ChanModes(true),
- ">\n"
- };
-
- int lpos = 0, item = 0, ipos = 0;
- while (lpos < 1022 && item < 7)
- {
- char c = items[item][ipos++];
- if (c == 0)
- {
- // end of this string; hop to next string, insert a quote
- item++;
- ipos = 0;
- c = '"';
- }
- else if (c == '\\' || c == '"')
- {
- line[lpos++] = '\\';
- }
- line[lpos++] = c;
- }
- line[--lpos] = 0;
- fputs(line, f);
+ stream << "<permchannels channel=\"" << ServerConfig::Escape(chan->name)
+ << "\" topic=\"" << ServerConfig::Escape(chan->topic)
+ << "\" modes=\"" << ServerConfig::Escape(chan->ChanModes(true))
+ << "\">" << std::endl;
}
- int write_error = 0;
- write_error = ferror(f);
- write_error |= fclose(f);
- if (write_error)
+ if (stream.fail())
{
ServerInstance->Logs->Log("m_permchannels", LOG_DEFAULT, "permchannels: Cannot write to new database! %s (%d)", strerror(errno), errno);
ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new db: %s (%d)", strerror(errno), errno);
return false;
}
+ stream.close();
#ifdef _WIN32
if (remove(permchannelsconf.c_str()))
@@ -109,7 +78,7 @@ static bool WriteDatabase()
}
#endif
// Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
- if (rename(tempname.c_str(), permchannelsconf.c_str()) < 0)
+ if (rename(permchannelsnewconf.c_str(), permchannelsconf.c_str()) < 0)
{
ServerInstance->Logs->Log("m_permchannels", LOG_DEFAULT, "permchannels: Cannot move new to old database! %s (%d)", strerror(errno), errno);
ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno);
@@ -120,7 +89,6 @@ static bool WriteDatabase()
}
-
/** Handles the +P channel mode
*/
class PermChannel : public ModeHandler
diff --git a/src/modules/m_spanningtree/netburst.cpp b/src/modules/m_spanningtree/netburst.cpp
index 8daaefd8a..89c8046f5 100644
--- a/src/modules/m_spanningtree/netburst.cpp
+++ b/src/modules/m_spanningtree/netburst.cpp
@@ -66,18 +66,14 @@ void TreeSocket::DoBurst(TreeServer* s)
*/
void TreeSocket::SendServers(TreeServer* Current, TreeServer* s)
{
- char command[MAXBUF];
for (unsigned int q = 0; q < Current->ChildCount(); q++)
{
TreeServer* recursive_server = Current->GetChild(q);
if (recursive_server != s)
{
- std::string recursive_servername = recursive_server->GetName();
- snprintf(command, MAXBUF, ":%s SERVER %s * 0 %s :%s", Current->GetID().c_str(), recursive_servername.c_str(),
- recursive_server->GetID().c_str(),
- recursive_server->GetDesc().c_str());
- this->WriteLine(command);
- this->WriteLine(":"+recursive_server->GetID()+" VERSION :"+recursive_server->GetVersion());
+ this->WriteLine(InspIRCd::Format(":%s SERVER %s * 0 %s :%s", Current->GetID().c_str(),
+ recursive_server->GetName().c_str(), recursive_server->GetID().c_str(), recursive_server->GetDesc().c_str()));
+ this->WriteLine(":" + recursive_server->GetID() + " VERSION :" + recursive_server->GetVersion());
/* down to next level */
this->SendServers(recursive_server, s);
}
@@ -119,8 +115,6 @@ void TreeSocket::SendFJoins(Channel* c)
/** Send all XLines we know about */
void TreeSocket::SendXLines()
{
- char data[MAXBUF];
-
std::vector<std::string> types = ServerInstance->XLines->GetAllTypes();
for (std::vector<std::string>::const_iterator it = types.begin(); it != types.end(); ++it)
@@ -139,15 +133,14 @@ void TreeSocket::SendXLines()
if (!i->second->IsBurstable())
break;
- snprintf(data, MAXBUF, ":%s ADDLINE %s %s %s %lu %lu :%s",
- ServerInstance->Config->GetSID().c_str(),
- it->c_str(),
- i->second->Displayable().c_str(),
- i->second->source.c_str(),
- (unsigned long)i->second->set_time,
- (unsigned long)i->second->duration,
- i->second->reason.c_str());
- this->WriteLine(data);
+ this->WriteLine(InspIRCd::Format(":%s ADDLINE %s %s %s %lu %lu :%s",
+ ServerInstance->Config->GetSID().c_str(),
+ it->c_str(),
+ i->second->Displayable().c_str(),
+ i->second->source.c_str(),
+ (unsigned long)i->second->set_time,
+ (unsigned long)i->second->duration,
+ i->second->reason.c_str()));
}
}
}
@@ -156,16 +149,15 @@ void TreeSocket::SendXLines()
/** Send channel topic, modes and metadata */
void TreeSocket::SyncChannel(Channel* chan)
{
- char data[MAXBUF];
-
SendFJoins(chan);
// If the topic was ever set, send it, even if it's empty now
// because a new empty topic should override an old non-empty topic
if (chan->topicset != 0)
{
- snprintf(data,MAXBUF,":%s FTOPIC %s %lu %lu %s :%s", ServerInstance->Config->GetSID().c_str(), chan->name.c_str(), (unsigned long) chan->age, (unsigned long)chan->topicset, chan->setby.c_str(), chan->topic.c_str());
- this->WriteLine(data);
+ this->WriteLine(InspIRCd::Format(":%s FTOPIC %s %lu %lu %s :%s", ServerInstance->Config->GetSID().c_str(),
+ chan->name.c_str(), (unsigned long)chan->age, (unsigned long)chan->topicset,
+ chan->setby.c_str(), chan->topic.c_str()));
}
for (Extensible::ExtensibleStore::const_iterator i = chan->GetExtList().begin(); i != chan->GetExtList().end(); i++)
@@ -182,7 +174,6 @@ void TreeSocket::SyncChannel(Channel* chan)
/** send all users and their oper state/modes */
void TreeSocket::SendUsers()
{
- char data[MAXBUF];
for (user_hash::iterator u = ServerInstance->Users->clientlist->begin(); u != ServerInstance->Users->clientlist->end(); u++)
{
if (u->second->registered == REG_ALL)
@@ -190,28 +181,27 @@ void TreeSocket::SendUsers()
TreeServer* theirserver = Utils->FindServer(u->second->server);
if (theirserver)
{
- snprintf(data,MAXBUF,":%s UID %s %lu %s %s %s %s %s %lu +%s :%s",
- theirserver->GetID().c_str(), /* Prefix: SID */
- u->second->uuid.c_str(), /* 0: UUID */
- (unsigned long)u->second->age, /* 1: TS */
- u->second->nick.c_str(), /* 2: Nick */
- u->second->host.c_str(), /* 3: Displayed Host */
- u->second->dhost.c_str(), /* 4: Real host */
- u->second->ident.c_str(), /* 5: Ident */
- u->second->GetIPString().c_str(), /* 6: IP string */
- (unsigned long)u->second->signon, /* 7: Signon time for WHOWAS */
- u->second->FormatModes(true), /* 8...n: Modes and params */
- u->second->fullname.c_str()); /* size-1: GECOS */
- this->WriteLine(data);
+ this->WriteLine(InspIRCd::Format(":%s UID %s %lu %s %s %s %s %s %lu +%s :%s",
+ theirserver->GetID().c_str(), // Prefix: SID
+ u->second->uuid.c_str(), // 0: UUID
+ (unsigned long)u->second->age, // 1: TS
+ u->second->nick.c_str(), // 2: Nick
+ u->second->host.c_str(), // 3: Real host
+ u->second->dhost.c_str(), // 4: Display host
+ u->second->ident.c_str(), // 5: Ident
+ u->second->GetIPString().c_str(), // 6: IP address
+ (unsigned long)u->second->signon, // 7: Signon time
+ u->second->FormatModes(true), // 8...n: User modes and params
+ u->second->fullname.c_str())); // size-1: GECOS
+
if (u->second->IsOper())
{
- snprintf(data,MAXBUF,":%s OPERTYPE :%s", u->second->uuid.c_str(), u->second->oper->name.c_str());
- this->WriteLine(data);
+ this->WriteLine(InspIRCd::Format(":%s OPERTYPE :%s", u->second->uuid.c_str(), u->second->oper->name.c_str()));
}
if (u->second->IsAway())
{
- snprintf(data,MAXBUF,":%s AWAY %ld :%s", u->second->uuid.c_str(), (long)u->second->awaytime, u->second->awaymsg.c_str());
- this->WriteLine(data);
+ this->WriteLine(InspIRCd::Format(":%s AWAY %ld :%s", u->second->uuid.c_str(), (long)u->second->awaytime,
+ u->second->awaymsg.c_str()));
}
}
diff --git a/src/modules/m_spanningtree/utils.cpp b/src/modules/m_spanningtree/utils.cpp
index 157679ed7..c469f41a2 100644
--- a/src/modules/m_spanningtree/utils.cpp
+++ b/src/modules/m_spanningtree/utils.cpp
@@ -197,7 +197,7 @@ void SpanningTreeUtilities::GetListOfServersForChannel(Channel* c, TreeServerLis
std::string SpanningTreeUtilities::ConstructLine(const std::string& prefix, const std::string& command, const parameterlist& params)
{
std::string FullLine;
- FullLine.reserve(MAXBUF);
+ FullLine.reserve(1024);
FullLine = ":" + prefix + " " + command;
for (parameterlist::const_iterator x = params.begin(); x != params.end(); ++x)
{
diff --git a/src/modules/m_xline_db.cpp b/src/modules/m_xline_db.cpp
index e325fc088..0b3361953 100644
--- a/src/modules/m_xline_db.cpp
+++ b/src/modules/m_xline_db.cpp
@@ -20,6 +20,7 @@
#include "inspircd.h"
#include "xline.h"
+#include <fstream>
/* $ModConfig: <xlinedb filename="data/xline.db">
* Specify the filename for the xline database here*/
@@ -85,18 +86,16 @@ class ModuleXLineDB : public Module
bool WriteDatabase()
{
- FILE *f;
-
/*
* We need to perform an atomic write so as not to fuck things up.
- * So, let's write to a temporary file, flush and sync the FD, then rename the file..
+ * So, let's write to a temporary file, flush it, then rename the file..
* Technically, that means that this can block, but I have *never* seen that.
- * -- w00t
+ * -- w00t
*/
ServerInstance->Logs->Log("m_xline_db", LOG_DEBUG, "xlinedb: Opening temporary database");
std::string xlinenewdbpath = xlinedbpath + ".new";
- f = fopen(xlinenewdbpath.c_str(), "w");
- if (!f)
+ std::ofstream stream(xlinenewdbpath.c_str());
+ if (!stream.is_open())
{
ServerInstance->Logs->Log("m_xline_db", LOG_DEBUG, "xlinedb: Cannot create database! %s (%d)", strerror(errno), errno);
ServerInstance->SNO->WriteToSnoMask('a', "database: cannot create new db: %s (%d)", strerror(errno), errno);
@@ -112,7 +111,7 @@ class ModuleXLineDB : public Module
* semblance of backwards compatibility for reading on startup..
* -- w00t
*/
- fprintf(f, "VERSION 1\n");
+ stream << "VERSION 1" << std::endl;
// Now, let's write.
std::vector<std::string> types = ServerInstance->XLines->GetAllTypes();
@@ -125,26 +124,21 @@ class ModuleXLineDB : public Module
for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
{
XLine* line = i->second;
- fprintf(f, "LINE %s %s %s %lu %lu :%s\n",
- line->type.c_str(),
- line->Displayable().c_str(),
- ServerInstance->Config->ServerName.c_str(),
- (unsigned long)line->set_time,
- (unsigned long)line->duration, line->reason.c_str());
+ stream << "LINE " << line->type << " " << line->Displayable() << " "
+ << ServerInstance->Config->ServerName << " " << line->set_time << " "
+ << line->duration << " " << line->reason << std::endl;
}
}
ServerInstance->Logs->Log("m_xline_db", LOG_DEBUG, "xlinedb: Finished writing XLines. Checking for error..");
- int write_error = 0;
- write_error = ferror(f);
- write_error |= fclose(f);
- if (write_error)
+ if (stream.fail())
{
ServerInstance->Logs->Log("m_xline_db", LOG_DEBUG, "xlinedb: Cannot write to new database! %s (%d)", strerror(errno), errno);
ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new db: %s (%d)", strerror(errno), errno);
return false;
}
+ stream.close();
#ifdef _WIN32
if (remove(xlinedbpath.c_str()))
@@ -167,42 +161,23 @@ class ModuleXLineDB : public Module
bool ReadDatabase()
{
- FILE *f;
- char linebuf[MAXBUF];
+ // If the xline database doesn't exist then we don't need to load it.
+ if (!ServerConfig::FileExists(xlinedbpath.c_str()))
+ return true;
- f = fopen(xlinedbpath.c_str(), "r");
- if (!f)
+ std::ifstream stream(xlinedbpath.c_str());
+ if (!stream.is_open())
{
- if (errno == ENOENT)
- {
- /* xline.db doesn't exist, fake good return value (we don't care about this) */
- return true;
- }
- else
- {
- /* this might be slightly more problematic. */
- ServerInstance->Logs->Log("m_xline_db", LOG_DEBUG, "xlinedb: Cannot read database! %s (%d)", strerror(errno), errno);
- ServerInstance->SNO->WriteToSnoMask('a', "database: cannot read db: %s (%d)", strerror(errno), errno);
- return false;
- }
+ ServerInstance->Logs->Log("m_xline_db", LOG_DEBUG, "xlinedb: Cannot read database! %s (%d)", strerror(errno), errno);
+ ServerInstance->SNO->WriteToSnoMask('a', "database: cannot read db: %s (%d)", strerror(errno), errno);
+ return false;
}
-
- while (fgets(linebuf, MAXBUF, f))
+
+ std::string line;
+ while (std::getline(stream, line))
{
- char *c = linebuf;
-
- while (c && *c)
- {
- if (*c == '\n')
- {
- *c = '\0';
- }
-
- c++;
- }
-
// Inspired by the command parser. :)
- irc::tokenstream tokens(linebuf);
+ irc::tokenstream tokens(line);
int items = 0;
std::string command_p[7];
std::string tmp;
@@ -213,7 +188,7 @@ class ModuleXLineDB : public Module
items++;
}
- ServerInstance->Logs->Log("m_xline_db", LOG_DEBUG, "xlinedb: Processing %s", linebuf);
+ ServerInstance->Logs->Log("m_xline_db", LOG_DEBUG, "xlinedb: Processing %s", line.c_str());
if (command_p[0] == "VERSION")
{
@@ -223,7 +198,7 @@ class ModuleXLineDB : public Module
}
else
{
- fclose(f);
+ stream.close();
ServerInstance->Logs->Log("m_xline_db", LOG_DEBUG, "xlinedb: I got database version %s - I don't understand it", command_p[1].c_str());
ServerInstance->SNO->WriteToSnoMask('a', "database: I got a database version (%s) I don't understand", command_p[1].c_str());
return false;
@@ -251,8 +226,7 @@ class ModuleXLineDB : public Module
delete xl;
}
}
-
- fclose(f);
+ stream.close();
return true;
}
diff --git a/src/socket.cpp b/src/socket.cpp
index 12fb632a7..9ec7544f2 100644
--- a/src/socket.cpp
+++ b/src/socket.cpp
@@ -221,24 +221,22 @@ static const char all_zero[16] = {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 };
std::string irc::sockets::sockaddrs::str() const
{
- char buffer[MAXBUF];
if (sa.sa_family == AF_INET)
{
- const uint8_t* bits = reinterpret_cast<const uint8_t*>(&in4.sin_addr);
- sprintf(buffer, "%d.%d.%d.%d:%u", bits[0], bits[1], bits[2], bits[3], ntohs(in4.sin_port));
+ char ipaddr[INET_ADDRSTRLEN];
+ inet_ntop(AF_INET, &in4.sin_addr, ipaddr, sizeof(ipaddr));
+ return InspIRCd::Format("%s:%u", ipaddr, ntohs(in4.sin_port));
}
- else if (sa.sa_family == AF_INET6)
+
+ if (sa.sa_family == AF_INET6)
{
- buffer[0] = '[';
- if (!inet_ntop(AF_INET6, &in6.sin6_addr, buffer+1, MAXBUF - 10))
- return "<unknown>"; // should never happen, buffer is large enough
- int len = strlen(buffer);
- // no need for snprintf, buffer has at least 9 chars left, max short len = 5
- sprintf(buffer + len, "]:%u", ntohs(in6.sin6_port));
+ char ipaddr[INET6_ADDRSTRLEN];
+ inet_ntop(AF_INET6, &in6.sin6_addr, ipaddr, sizeof(ipaddr));
+ return InspIRCd::Format("[%s]:%u", ipaddr, ntohs(in6.sin6_port));
}
- else
- return "<unknown>";
- return std::string(buffer);
+
+ // This should never happen.
+ return "<unknown>";
}
int irc::sockets::sockaddrs::sa_size() const
diff --git a/src/users.cpp b/src/users.cpp
index 44834330b..468c2aa36 100644
--- a/src/users.cpp
+++ b/src/users.cpp
@@ -122,18 +122,16 @@ void User::SetNoticeMask(unsigned char sm, bool value)
snomasks[sm-65] = value;
}
-const char* User::FormatNoticeMasks()
+std::string User::FormatNoticeMasks()
{
- static char data[MAXBUF];
- int offset = 0;
+ std::string data;
- for (int n = 0; n < 64; n++)
+ for (unsigned char n = 0; n < 64; n++)
{
if (snomasks[n])
- data[offset++] = n+65;
+ data.push_back(n + 65);
}
- data[offset] = 0;
return data;
}
@@ -220,18 +218,8 @@ const std::string& User::MakeHost()
if (!this->cached_makehost.empty())
return this->cached_makehost;
- char nhost[MAXBUF];
- /* This is much faster than snprintf */
- char* t = nhost;
- for(const char* n = ident.c_str(); *n; n++)
- *t++ = *n;
- *t++ = '@';
- for(const char* n = host.c_str(); *n; n++)
- *t++ = *n;
- *t = 0;
-
- this->cached_makehost.assign(nhost);
-
+ // XXX: Is there really a need to cache this?
+ this->cached_makehost = ident + "@" + host;
return this->cached_makehost;
}
@@ -240,18 +228,8 @@ const std::string& User::MakeHostIP()
if (!this->cached_hostip.empty())
return this->cached_hostip;
- char ihost[MAXBUF];
- /* This is much faster than snprintf */
- char* t = ihost;
- for(const char* n = ident.c_str(); *n; n++)
- *t++ = *n;
- *t++ = '@';
- for(const char* n = this->GetIPString().c_str(); *n; n++)
- *t++ = *n;
- *t = 0;
-
- this->cached_hostip = ihost;
-
+ // XXX: Is there really a need to cache this?
+ this->cached_hostip = ident + "@" + this->GetIPString();
return this->cached_hostip;
}
@@ -260,54 +238,18 @@ const std::string& User::GetFullHost()
if (!this->cached_fullhost.empty())
return this->cached_fullhost;
- char result[MAXBUF];
- char* t = result;
- for(const char* n = nick.c_str(); *n; n++)
- *t++ = *n;
- *t++ = '!';
- for(const char* n = ident.c_str(); *n; n++)
- *t++ = *n;
- *t++ = '@';
- for(const char* n = dhost.c_str(); *n; n++)
- *t++ = *n;
- *t = 0;
-
- this->cached_fullhost = result;
-
+ // XXX: Is there really a need to cache this?
+ this->cached_fullhost = nick + "!" + ident + "@" + dhost;
return this->cached_fullhost;
}
-char* User::MakeWildHost()
-{
- static char nresult[MAXBUF];
- char* t = nresult;
- *t++ = '*'; *t++ = '!';
- *t++ = '*'; *t++ = '@';
- for(const char* n = dhost.c_str(); *n; n++)
- *t++ = *n;
- *t = 0;
- return nresult;
-}
-
const std::string& User::GetFullRealHost()
{
if (!this->cached_fullrealhost.empty())
return this->cached_fullrealhost;
- char fresult[MAXBUF];
- char* t = fresult;
- for(const char* n = nick.c_str(); *n; n++)
- *t++ = *n;
- *t++ = '!';
- for(const char* n = ident.c_str(); *n; n++)
- *t++ = *n;
- *t++ = '@';
- for(const char* n = host.c_str(); *n; n++)
- *t++ = *n;
- *t = 0;
-
- this->cached_fullrealhost = fresult;
-
+ // XXX: Is there really a need to cache this?
+ this->cached_fullrealhost = nick + "!" + ident + "@" + host;
return this->cached_fullrealhost;
}
@@ -428,7 +370,7 @@ void UserIOHandler::OnDataReady()
while (user->CommandFloodPenalty < penaltymax && getSendQSize() < sendqmax)
{
std::string line;
- line.reserve(MAXBUF);
+ line.reserve(ServerInstance->Config->Limits.MaxLine);
std::string::size_type qpos = 0;
while (qpos < recvq.length())
{
@@ -443,7 +385,7 @@ void UserIOHandler::OnDataReady()
case '\n':
goto eol_found;
}
- if (line.length() < MAXBUF - 2)
+ if (line.length() < ServerInstance->Config->Limits.MaxLine - 2)
line.push_back(c);
}
// if we got here, the recvq ran out before we found a newline
@@ -995,10 +937,10 @@ void LocalUser::Write(const std::string& text)
if (!ServerInstance->SE->BoundsCheckFd(&eh))
return;
- if (text.length() > MAXBUF - 2)
+ if (text.length() > ServerInstance->Config->Limits.MaxLine - 2)
{
// this should happen rarely or never. Crop the string at 512 and try again.
- std::string try_again = text.substr(0, MAXBUF - 2);
+ std::string try_again = text.substr(0, ServerInstance->Config->Limits.MaxLine - 2);
Write(try_again);
return;
}
@@ -1051,16 +993,16 @@ void User::WriteNumeric(unsigned int numeric, const char* text, ...)
void User::WriteNumeric(unsigned int numeric, const std::string &text)
{
- char textbuffer[MAXBUF];
ModResult MOD_RESULT;
FIRST_MOD_RESULT(OnNumeric, MOD_RESULT, (this, numeric, text));
if (MOD_RESULT == MOD_RES_DENY)
return;
-
- snprintf(textbuffer,MAXBUF,":%s %03u %s",ServerInstance->Config->ServerName.c_str(), numeric, text.c_str());
- this->Write(std::string(textbuffer));
+
+ const std::string message = InspIRCd::Format(":%s %03u %s", ServerInstance->Config->ServerName.c_str(),
+ numeric, text.c_str());
+ this->Write(message);
}
void User::WriteFrom(User *user, const std::string &text)
@@ -1217,28 +1159,21 @@ void User::SendText(const char *text, ...)
SendText(line);
}
-void User::SendText(const std::string &LinePrefix, std::stringstream &TextStream)
+void User::SendText(const std::string& linePrefix, std::stringstream& textStream)
{
- char line[MAXBUF];
- int start_pos = LinePrefix.length();
- int pos = start_pos;
- memcpy(line, LinePrefix.data(), pos);
- std::string Word;
- while (TextStream >> Word)
+ std::string line;
+ std::string word;
+ while (textStream >> word)
{
- int len = Word.length();
- if (pos + len + 12 > MAXBUF)
+ size_t lineLength = linePrefix.length() + line.length() + word.length() + 3; // "\s\n\r"
+ if (lineLength > ServerInstance->Config->Limits.MaxLine)
{
- line[pos] = '\0';
- SendText(std::string(line));
- pos = start_pos;
+ SendText(linePrefix + line);
+ line.clear();
}
- line[pos] = ' ';
- memcpy(line + pos + 1, Word.data(), len);
- pos += len + 1;
+ line += " " + word;
}
- line[pos] = '\0';
- SendText(std::string(line));
+ SendText(linePrefix + line);
}
/* return 0 or 1 depending if users u and u2 share one or more common channels