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/>.
34 #include "exitcodes.h"
36 std::string InspIRCd::GetServerDescription(const std::string& servername)
38 std::string description;
40 FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
42 if (!description.empty())
48 // not a remote server that can be found, it must be me.
49 return Config->ServerDesc;
53 /* Find a user record by nickname and return a pointer to it */
54 User* InspIRCd::FindNick(const std::string &nick)
56 if (!nick.empty() && isdigit(*nick.begin()))
57 return FindUUID(nick);
59 user_hash::iterator iter = this->Users->clientlist->find(nick);
61 if (iter == this->Users->clientlist->end())
62 /* Couldn't find it */
68 User* InspIRCd::FindNick(const char* nick)
71 return FindUUID(nick);
73 user_hash::iterator iter = this->Users->clientlist->find(nick);
75 if (iter == this->Users->clientlist->end())
81 User* InspIRCd::FindNickOnly(const std::string &nick)
83 user_hash::iterator iter = this->Users->clientlist->find(nick);
85 if (iter == this->Users->clientlist->end())
91 User* InspIRCd::FindNickOnly(const char* nick)
93 user_hash::iterator iter = this->Users->clientlist->find(nick);
95 if (iter == this->Users->clientlist->end())
101 User *InspIRCd::FindUUID(const std::string &uid)
103 return FindUUID(uid.c_str());
106 User *InspIRCd::FindUUID(const char *uid)
108 user_hash::iterator finduuid = this->Users->uuidlist->find(uid);
110 if (finduuid == this->Users->uuidlist->end())
113 return finduuid->second;
116 /* find a channel record by channel name and return a pointer to it */
117 Channel* InspIRCd::FindChan(const char* chan)
119 chan_hash::iterator iter = chanlist->find(chan);
121 if (iter == chanlist->end())
122 /* Couldn't find it */
128 Channel* InspIRCd::FindChan(const std::string &chan)
130 chan_hash::iterator iter = chanlist->find(chan);
132 if (iter == chanlist->end())
133 /* Couldn't find it */
139 /* Send an error notice to all users, registered or not */
140 void InspIRCd::SendError(const std::string &s)
142 for (std::vector<LocalUser*>::const_iterator i = this->Users->local_users.begin(); i != this->Users->local_users.end(); i++)
145 if (u->registered == REG_ALL)
147 u->WriteServ("NOTICE %s :%s",u->nick.c_str(),s.c_str());
151 /* Unregistered connections receive ERROR, not a NOTICE */
152 u->Write("ERROR :" + s);
157 /* return channel count */
158 long InspIRCd::ChannelCount()
160 return chanlist->size();
163 bool InspIRCd::IsValidMask(const std::string &mask)
165 const char* dest = mask.c_str();
169 for (const char* i = dest; *i; i++)
171 /* out of range character, bad mask */
172 if (*i < 32 || *i > 126)
188 /* valid masks only have 1 ! and @ */
189 if (exclamation != 1 || atsign != 1)
192 if (mask.length() > 250)
198 /* true for valid channel name, false else */
199 bool IsChannelHandler::Call(const char *chname, size_t max)
201 const char *c = chname + 1;
203 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
204 if (!chname || *chname != '#')
222 size_t len = c - chname;
223 /* too long a name - note funky pointer arithmetic here. */
232 /* true for valid nickname, false else */
233 bool IsNickHandler::Call(const char* n, size_t max)
239 for (const char* i = n; *i; i++, p++)
241 if ((*i >= 'A') && (*i <= '}'))
243 /* "A"-"}" can occur anywhere in a nickname */
247 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
249 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
253 /* invalid character! abort */
257 /* too long? or not -- pointer arithmetic rocks */
261 /* return true for good ident, false else */
262 bool IsIdentHandler::Call(const char* n)
267 for (const char* i = n; *i; i++)
269 if ((*i >= 'A') && (*i <= '}'))
274 if (((*i >= '0') && (*i <= '9')) || (*i == '-') || (*i == '.'))
285 bool IsSIDHandler::Call(const std::string &str)
287 /* Returns true if the string given is exactly 3 characters long,
288 * starts with a digit, and the other two characters are A-Z or digits
290 return ((str.length() == 3) && isdigit(str[0]) &&
291 ((str[1] >= 'A' && str[1] <= 'Z') || isdigit(str[1])) &&
292 ((str[2] >= 'A' && str[2] <= 'Z') || isdigit(str[2])));
295 /* open the proper logfile */
296 bool InspIRCd::OpenLog(char**, int)
298 if (!Config->cmdline.writelog) return true; // Skip opening default log if -nolog
300 if (Config->cmdline.startup_log.empty())
301 Config->cmdline.startup_log = LOG_PATH "/startup.log";
302 FILE* startup = fopen(Config->cmdline.startup_log.c_str(), "a+");
309 FileWriter* fw = new FileWriter(startup);
310 FileLogStream *f = new FileLogStream((Config->cmdline.forcedebug ? DEBUG : DEFAULT), fw);
312 this->Logs->AddLogType("*", f, true);
317 void InspIRCd::CheckRoot()
322 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
323 this->Logs->Log("STARTUP",DEFAULT,"Cant start as root");
324 Exit(EXIT_STATUS_ROOT);
329 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const std::string &text)
331 std::string copy_text = text;
333 ModResult MOD_RESULT;
334 FIRST_MOD_RESULT(OnWhoisLine, MOD_RESULT, (user, dest, numeric, copy_text));
336 if (MOD_RESULT != MOD_RES_DENY)
337 user->WriteServ("%d %s", numeric, copy_text.c_str());
340 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const char* format, ...)
342 char textbuffer[MAXBUF];
344 va_start (argsPtr, format);
345 vsnprintf(textbuffer, MAXBUF, format, argsPtr);
348 this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));
351 /** Refactored by Brain, Jun 2009. Much faster with some clever O(1) array
352 * lookups and pointer maths.
354 long InspIRCd::Duration(const std::string &str)
356 unsigned char multiplier = 0;
361 /* Iterate each item in the string, looking for number or multiplier */
362 for (std::string::const_reverse_iterator i = str.rbegin(); i != str.rend(); ++i)
364 /* Found a number, queue it onto the current number */
365 if ((*i >= '0') && (*i <= '9'))
367 subtotal = subtotal + ((*i - '0') * times);
372 /* Found something thats not a number, find out how much
373 * it multiplies the built up number by, multiply the total
374 * and reset the built up number.
377 total += subtotal * duration_multi[multiplier];
379 /* Next subtotal please */
387 total += subtotal * duration_multi[multiplier];
390 /* Any trailing values built up are treated as raw seconds */
391 return total + subtotal;
394 bool InspIRCd::ULine(const std::string& sserver)
399 return (Config->ulines.find(sserver.c_str()) != Config->ulines.end());
402 bool InspIRCd::SilentULine(const std::string& sserver)
404 std::map<irc::string,bool>::iterator n = Config->ulines.find(sserver.c_str());
405 if (n != Config->ulines.end())
411 std::string InspIRCd::TimeString(time_t curtime)
413 return std::string(ctime(&curtime),24);
416 // You should only pass a single character to this.
417 void InspIRCd::AddExtBanChar(char c)
419 std::string &tok = Config->data005;
420 std::string::size_type ebpos = tok.find(" EXTBAN=,");
422 if (ebpos == std::string::npos)
424 tok.append(" EXTBAN=,");
430 while (isalpha(tok[ebpos]) && tok[ebpos] < c)
432 tok.insert(ebpos, 1, c);
436 std::string InspIRCd::GenRandomStr(int length, bool printable)
438 char* buf = new char[length];
439 GenRandom(buf, length);
442 for(int i=0; i < length; i++)
443 rv[i] = printable ? 0x3F + (buf[i] & 0x3F) : buf[i];
448 // NOTE: this has a slight bias for lower values if max is not a power of 2.
449 // Don't use it if that matters.
450 unsigned long InspIRCd::GenRandomInt(unsigned long max)
453 GenRandom((char*)&rv, sizeof(rv));
457 // This is overridden by a higher-quality algorithm when SSL support is loaded
458 void GenRandomHandler::Call(char *output, size_t max)
460 for(unsigned int i=0; i < max; i++)
464 if(rand_s(&uTemp) != 0)
470 output[i] = random();
474 ModResult OnCheckExemptionHandler::Call(User* user, Channel* chan, const std::string& restriction)
476 unsigned int mypfx = chan->GetPrefixValue(user);
480 irc::spacesepstream defaultstream(ServerInstance->Config->ConfValue("options")->getString("exemptchanops"));
482 while (defaultstream.GetToken(current))
484 std::string::size_type pos = current.find(':');
485 if (pos == std::string::npos)
487 if (current.substr(0,pos) == restriction)
488 minmode = current[pos+1];
491 ModeHandler* mh = ServerInstance->Modes->FindMode(minmode, MODETYPE_CHANNEL);
492 if (mh && mypfx >= mh->GetPrefixRank())
493 return MOD_RES_ALLOW;
494 if (mh || minmode == '*')
496 return MOD_RES_PASSTHRU;