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"
37 std::string InspIRCd::GetServerDescription(const std::string& servername)
39 std::string description;
41 FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
43 if (!description.empty())
49 // not a remote server that can be found, it must be me.
50 return Config->ServerDesc;
54 /* Find a user record by nickname and return a pointer to it */
55 User* InspIRCd::FindNick(const std::string &nick)
57 if (!nick.empty() && isdigit(*nick.begin()))
58 return FindUUID(nick);
60 user_hash::iterator iter = this->Users->clientlist->find(nick);
62 if (iter == this->Users->clientlist->end())
63 /* Couldn't find it */
69 User* InspIRCd::FindNick(const char* nick)
72 return FindUUID(nick);
74 user_hash::iterator iter = this->Users->clientlist->find(nick);
76 if (iter == this->Users->clientlist->end())
82 User* InspIRCd::FindNickOnly(const std::string &nick)
84 user_hash::iterator iter = this->Users->clientlist->find(nick);
86 if (iter == this->Users->clientlist->end())
92 User* InspIRCd::FindNickOnly(const char* nick)
94 user_hash::iterator iter = this->Users->clientlist->find(nick);
96 if (iter == this->Users->clientlist->end())
102 User *InspIRCd::FindUUID(const std::string &uid)
104 return FindUUID(uid.c_str());
107 User *InspIRCd::FindUUID(const char *uid)
109 user_hash::iterator finduuid = this->Users->uuidlist->find(uid);
111 if (finduuid == this->Users->uuidlist->end())
114 return finduuid->second;
117 /* find a channel record by channel name and return a pointer to it */
118 Channel* InspIRCd::FindChan(const char* chan)
120 chan_hash::iterator iter = chanlist->find(chan);
122 if (iter == chanlist->end())
123 /* Couldn't find it */
129 Channel* InspIRCd::FindChan(const std::string &chan)
131 chan_hash::iterator iter = chanlist->find(chan);
133 if (iter == chanlist->end())
134 /* Couldn't find it */
140 /* Send an error notice to all users, registered or not */
141 void InspIRCd::SendError(const std::string &s)
143 for (std::vector<LocalUser*>::const_iterator i = this->Users->local_users.begin(); i != this->Users->local_users.end(); i++)
146 if (u->registered == REG_ALL)
148 u->WriteServ("NOTICE %s :%s",u->nick.c_str(),s.c_str());
152 /* Unregistered connections receive ERROR, not a NOTICE */
153 u->Write("ERROR :" + s);
158 /* return channel count */
159 long InspIRCd::ChannelCount()
161 return chanlist->size();
164 bool InspIRCd::IsValidMask(const std::string &mask)
166 const char* dest = mask.c_str();
170 for (const char* i = dest; *i; i++)
172 /* out of range character, bad mask */
173 if (*i < 32 || *i > 126)
189 /* valid masks only have 1 ! and @ */
190 if (exclamation != 1 || atsign != 1)
193 if (mask.length() > 250)
199 /* true for valid channel name, false else */
200 bool IsChannelHandler::Call(const char *chname, size_t max)
202 const char *c = chname + 1;
204 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
205 if (!chname || *chname != '#')
223 size_t len = c - chname;
224 /* too long a name - note funky pointer arithmetic here. */
233 /* true for valid nickname, false else */
234 bool IsNickHandler::Call(const char* n, size_t max)
240 for (const char* i = n; *i; i++, p++)
242 if ((*i >= 'A') && (*i <= '}'))
244 /* "A"-"}" can occur anywhere in a nickname */
248 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
250 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
254 /* invalid character! abort */
258 /* too long? or not -- pointer arithmetic rocks */
262 /* return true for good ident, false else */
263 bool IsIdentHandler::Call(const char* n)
268 for (const char* i = n; *i; i++)
270 if ((*i >= 'A') && (*i <= '}'))
275 if (((*i >= '0') && (*i <= '9')) || (*i == '-') || (*i == '.'))
286 bool IsSIDHandler::Call(const std::string &str)
288 /* Returns true if the string given is exactly 3 characters long,
289 * starts with a digit, and the other two characters are A-Z or digits
291 return ((str.length() == 3) && isdigit(str[0]) &&
292 ((str[1] >= 'A' && str[1] <= 'Z') || isdigit(str[1])) &&
293 ((str[2] >= 'A' && str[2] <= 'Z') || isdigit(str[2])));
296 /* open the proper logfile */
297 bool InspIRCd::OpenLog(char**, int)
299 if (!Config->cmdline.writelog) return true; // Skip opening default log if -nolog
301 if (Config->cmdline.startup_log.empty())
302 Config->cmdline.startup_log = LOG_PATH "/startup.log";
303 FILE* startup = fopen(Config->cmdline.startup_log.c_str(), "a+");
310 FileWriter* fw = new FileWriter(startup);
311 FileLogStream *f = new FileLogStream((Config->cmdline.forcedebug ? DEBUG : DEFAULT), fw);
313 this->Logs->AddLogType("*", f, true);
318 void InspIRCd::CheckRoot()
323 std::cout << "ERROR: You are running an irc server as root! DO NOT DO THIS!" << std::endl << std::endl;
324 this->Logs->Log("STARTUP",DEFAULT,"Can't start as root");
325 Exit(EXIT_STATUS_ROOT);
330 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const std::string &text)
332 std::string copy_text = text;
334 ModResult MOD_RESULT;
335 FIRST_MOD_RESULT(OnWhoisLine, MOD_RESULT, (user, dest, numeric, copy_text));
337 if (MOD_RESULT != MOD_RES_DENY)
338 user->WriteServ("%d %s", numeric, copy_text.c_str());
341 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const char* format, ...)
343 char textbuffer[MAXBUF];
345 va_start (argsPtr, format);
346 vsnprintf(textbuffer, MAXBUF, format, argsPtr);
349 this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));
352 /** Refactored by Brain, Jun 2009. Much faster with some clever O(1) array
353 * lookups and pointer maths.
355 long InspIRCd::Duration(const std::string &str)
357 unsigned char multiplier = 0;
362 /* Iterate each item in the string, looking for number or multiplier */
363 for (std::string::const_reverse_iterator i = str.rbegin(); i != str.rend(); ++i)
365 /* Found a number, queue it onto the current number */
366 if ((*i >= '0') && (*i <= '9'))
368 subtotal = subtotal + ((*i - '0') * times);
373 /* Found something thats not a number, find out how much
374 * it multiplies the built up number by, multiply the total
375 * and reset the built up number.
378 total += subtotal * duration_multi[multiplier];
380 /* Next subtotal please */
388 total += subtotal * duration_multi[multiplier];
391 /* Any trailing values built up are treated as raw seconds */
392 return total + subtotal;
395 bool InspIRCd::ULine(const std::string& sserver)
400 return (Config->ulines.find(sserver.c_str()) != Config->ulines.end());
403 bool InspIRCd::SilentULine(const std::string& sserver)
405 std::map<irc::string,bool>::iterator n = Config->ulines.find(sserver.c_str());
406 if (n != Config->ulines.end())
412 std::string InspIRCd::TimeString(time_t curtime)
414 return std::string(ctime(&curtime),24);
417 // You should only pass a single character to this.
418 void InspIRCd::AddExtBanChar(char c)
420 std::string &tok = Config->data005;
421 std::string::size_type ebpos = tok.find(" EXTBAN=,");
423 if (ebpos == std::string::npos)
425 tok.append(" EXTBAN=,");
431 while (isalpha(tok[ebpos]) && tok[ebpos] < c)
433 tok.insert(ebpos, 1, c);
437 std::string InspIRCd::GenRandomStr(int length, bool printable)
439 char* buf = new char[length];
440 GenRandom(buf, length);
443 for(int i=0; i < length; i++)
444 rv[i] = printable ? 0x3F + (buf[i] & 0x3F) : buf[i];
449 // NOTE: this has a slight bias for lower values if max is not a power of 2.
450 // Don't use it if that matters.
451 unsigned long InspIRCd::GenRandomInt(unsigned long max)
454 GenRandom((char*)&rv, sizeof(rv));
458 // This is overridden by a higher-quality algorithm when SSL support is loaded
459 void GenRandomHandler::Call(char *output, size_t max)
461 for(unsigned int i=0; i < max; i++)
465 if(rand_s(&uTemp) != 0)
471 output[i] = random();
475 ModResult OnCheckExemptionHandler::Call(User* user, Channel* chan, const std::string& restriction)
477 unsigned int mypfx = chan->GetPrefixValue(user);
481 irc::spacesepstream defaultstream(ServerInstance->Config->ConfValue("options")->getString("exemptchanops"));
483 while (defaultstream.GetToken(current))
485 std::string::size_type pos = current.find(':');
486 if (pos == std::string::npos)
488 if (current.substr(0,pos) == restriction)
489 minmode = current[pos+1];
492 ModeHandler* mh = ServerInstance->Modes->FindMode(minmode, MODETYPE_CHANNEL);
493 if (mh && mypfx >= mh->GetPrefixRank())
494 return MOD_RES_ALLOW;
495 if (mh || minmode == '*')
497 return MOD_RES_PASSTHRU;