2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2006-2008 Robin Burchell <robin+git@viroteck.net>
6 * Copyright (C) 2005-2008 Craig Edwards <craigedwards@brainbox.cc>
7 * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
8 * Copyright (C) 2006-2007 Oliver Lupton <oliverlupton@gmail.com>
9 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
11 * This file is part of InspIRCd. InspIRCd is free software: you can
12 * redistribute it and/or modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation, version 2.
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include "exitcodes.h"
35 /* Find a user record by nickname and return a pointer to it */
36 User* InspIRCd::FindNick(const std::string &nick)
38 if (!nick.empty() && isdigit(*nick.begin()))
39 return FindUUID(nick);
41 user_hash::iterator iter = this->Users->clientlist.find(nick);
43 if (iter == this->Users->clientlist.end())
44 /* Couldn't find it */
50 User* InspIRCd::FindNickOnly(const std::string &nick)
52 user_hash::iterator iter = this->Users->clientlist.find(nick);
54 if (iter == this->Users->clientlist.end())
60 User *InspIRCd::FindUUID(const std::string &uid)
62 user_hash::iterator finduuid = this->Users->uuidlist.find(uid);
64 if (finduuid == this->Users->uuidlist.end())
67 return finduuid->second;
69 /* find a channel record by channel name and return a pointer to it */
71 Channel* InspIRCd::FindChan(const std::string &chan)
73 chan_hash::iterator iter = chanlist.find(chan);
75 if (iter == chanlist.end())
76 /* Couldn't find it */
82 bool InspIRCd::IsValidMask(const std::string &mask)
84 const char* dest = mask.c_str();
88 for (const char* i = dest; *i; i++)
90 /* out of range character, bad mask */
91 if (*i < 32 || *i > 126)
107 /* valid masks only have 1 ! and @ */
108 if (exclamation != 1 || atsign != 1)
111 if (mask.length() > 250)
117 void InspIRCd::StripColor(std::string &sentence)
119 /* refactor this completely due to SQUIT bug since the old code would strip last char and replace with \0 --peavey */
122 for (std::string::iterator i = sentence.begin(); i != sentence.end();)
126 else if (seq && (( ((*i >= '0') && (*i <= '9')) || (*i == ',') ) ))
129 if ( (seq <= 4) && (*i == ',') )
137 if (seq || ((*i == 2) || (*i == 15) || (*i == 22) || (*i == 21) || (*i == 31)))
138 i = sentence.erase(i);
144 void InspIRCd::ProcessColors(file_cache& input)
147 * Replace all color codes from the special[] array to actual
148 * color code chars using C++ style escape sequences. You
149 * can append other chars to replace if you like -- Justasic
151 static struct special_chars
153 std::string character;
155 special_chars(const std::string &c, const std::string &r) : character(c), replace(r) { }
159 special_chars("\\002", "\002"), // Bold
160 special_chars("\\037", "\037"), // underline
161 special_chars("\\003", "\003"), // Color
162 special_chars("\\017", "\017"), // Stop colors
163 special_chars("\\u", "\037"), // Alias for underline
164 special_chars("\\b", "\002"), // Alias for Bold
165 special_chars("\\x", "\017"), // Alias for stop
166 special_chars("\\c", "\003"), // Alias for color
167 special_chars("", "")
170 for(file_cache::iterator it = input.begin(), it_end = input.end(); it != it_end; it++)
172 std::string ret = *it;
173 for(int i = 0; special[i].character.empty() == false; ++i)
175 std::string::size_type pos = ret.find(special[i].character);
176 if(pos == std::string::npos) // Couldn't find the character, skip this line
179 if((pos > 0) && (ret[pos-1] == '\\') && (ret[pos] == '\\'))
180 continue; // Skip double slashes.
182 // Replace all our characters in the array
183 while(pos != std::string::npos)
185 ret = ret.substr(0, pos) + special[i].replace + ret.substr(pos + special[i].character.size());
186 pos = ret.find(special[i].character, pos + special[i].replace.size());
190 // Replace double slashes with a single slash before we return
191 std::string::size_type pos = ret.find("\\\\");
192 while(pos != std::string::npos)
194 ret = ret.substr(0, pos) + "\\" + ret.substr(pos + 2);
195 pos = ret.find("\\\\", pos + 1);
201 /* true for valid channel name, false else */
202 bool IsChannelHandler::Call(const std::string& chname)
204 if (chname.empty() || chname.length() > ServerInstance->Config->Limits.ChanMax)
207 if (chname[0] != '#')
210 for (std::string::const_iterator i = chname.begin()+1; i != chname.end(); ++i)
224 /* true for valid nickname, false else */
225 bool IsNickHandler::Call(const std::string& n)
227 if (n.empty() || n.length() > ServerInstance->Config->Limits.NickMax)
230 for (std::string::const_iterator i = n.begin(); i != n.end(); ++i)
232 if ((*i >= 'A') && (*i <= '}'))
234 /* "A"-"}" can occur anywhere in a nickname */
238 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i != n.begin()))
240 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
244 /* invalid character! abort */
251 /* return true for good ident, false else */
252 bool IsIdentHandler::Call(const std::string& n)
257 for (std::string::const_iterator i = n.begin(); i != n.end(); ++i)
259 if ((*i >= 'A') && (*i <= '}'))
264 if (((*i >= '0') && (*i <= '9')) || (*i == '-') || (*i == '.'))
275 bool InspIRCd::IsSID(const std::string &str)
277 /* Returns true if the string given is exactly 3 characters long,
278 * starts with a digit, and the other two characters are A-Z or digits
280 return ((str.length() == 3) && isdigit(str[0]) &&
281 ((str[1] >= 'A' && str[1] <= 'Z') || isdigit(str[1])) &&
282 ((str[2] >= 'A' && str[2] <= 'Z') || isdigit(str[2])));
285 void InspIRCd::CheckRoot()
290 std::cout << "ERROR: You are running an irc server as root! DO NOT DO THIS!" << std::endl << std::endl;
291 this->Logs->Log("STARTUP", LOG_DEFAULT, "Can't start as root");
292 Exit(EXIT_STATUS_ROOT);
297 /** Refactored by Brain, Jun 2009. Much faster with some clever O(1) array
298 * lookups and pointer maths.
300 unsigned long InspIRCd::Duration(const std::string &str)
302 unsigned char multiplier = 0;
307 /* Iterate each item in the string, looking for number or multiplier */
308 for (std::string::const_reverse_iterator i = str.rbegin(); i != str.rend(); ++i)
310 /* Found a number, queue it onto the current number */
311 if ((*i >= '0') && (*i <= '9'))
313 subtotal = subtotal + ((*i - '0') * times);
318 /* Found something thats not a number, find out how much
319 * it multiplies the built up number by, multiply the total
320 * and reset the built up number.
323 total += subtotal * duration_multi[multiplier];
325 /* Next subtotal please */
333 total += subtotal * duration_multi[multiplier];
336 /* Any trailing values built up are treated as raw seconds */
337 return total + subtotal;
340 const char* InspIRCd::Format(va_list &vaList, const char* formatString)
342 static std::vector<char> formatBuffer(1024);
347 va_copy(dst, vaList);
349 int vsnret = vsnprintf(&formatBuffer[0], formatBuffer.size(), formatString, dst);
352 if (vsnret > 0 && static_cast<unsigned>(vsnret) < formatBuffer.size())
357 formatBuffer.resize(formatBuffer.size() * 2);
360 return &formatBuffer[0];
363 const char* InspIRCd::Format(const char* formatString, ...)
366 VAFORMAT(ret, formatString, formatString);
370 std::string InspIRCd::TimeString(time_t curtime, const char* format, bool utc)
377 struct tm* timeinfo = utc ? gmtime(&curtime) : localtime(&curtime);
381 timeinfo = localtime(&curtime);
384 // If the calculated year exceeds four digits or is less than the year 1000,
385 // the behavior of asctime() is undefined
386 if (timeinfo->tm_year + 1900 > 9999)
387 timeinfo->tm_year = 9999 - 1900;
388 else if (timeinfo->tm_year + 1900 < 1000)
389 timeinfo->tm_year = 0;
391 // This is the default format used by asctime without the terminating new line.
393 format = "%a %b %d %H:%M:%S %Y";
396 if (!strftime(buffer, sizeof(buffer), format, timeinfo))
402 std::string InspIRCd::GenRandomStr(int length, bool printable)
404 char* buf = new char[length];
405 GenRandom(buf, length);
408 for(int i=0; i < length; i++)
409 rv[i] = printable ? 0x3F + (buf[i] & 0x3F) : buf[i];
414 // NOTE: this has a slight bias for lower values if max is not a power of 2.
415 // Don't use it if that matters.
416 unsigned long InspIRCd::GenRandomInt(unsigned long max)
419 GenRandom((char*)&rv, sizeof(rv));
423 // This is overridden by a higher-quality algorithm when SSL support is loaded
424 void GenRandomHandler::Call(char *output, size_t max)
426 for(unsigned int i=0; i < max; i++)
430 if(rand_s(&uTemp) != 0)
436 output[i] = random();
440 ModResult OnCheckExemptionHandler::Call(User* user, Channel* chan, const std::string& restriction)
442 unsigned int mypfx = chan->GetPrefixValue(user);
446 irc::spacesepstream defaultstream(ServerInstance->Config->ConfValue("options")->getString("exemptchanops"));
448 while (defaultstream.GetToken(current))
450 std::string::size_type pos = current.find(':');
451 if (pos == std::string::npos)
453 if (!current.compare(0, pos, restriction))
454 minmode = current[pos+1];
457 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(minmode);
458 if (mh && mypfx >= mh->GetPrefixRank())
459 return MOD_RES_ALLOW;
460 if (mh || minmode == '*')
462 return MOD_RES_PASSTHRU;