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/>.
29 #include "exitcodes.h"
31 std::string InspIRCd::GetServerDescription(const std::string& servername)
33 std::string description;
35 FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
37 if (!description.empty())
43 // not a remote server that can be found, it must be me.
44 return Config->ServerDesc;
48 /* Find a user record by nickname and return a pointer to it */
49 User* InspIRCd::FindNick(const std::string &nick)
51 if (!nick.empty() && isdigit(*nick.begin()))
52 return FindUUID(nick);
54 user_hash::iterator iter = this->Users->clientlist->find(nick);
56 if (iter == this->Users->clientlist->end())
57 /* Couldn't find it */
63 User* InspIRCd::FindNick(const char* nick)
66 return FindUUID(nick);
68 user_hash::iterator iter = this->Users->clientlist->find(nick);
70 if (iter == this->Users->clientlist->end())
76 User* InspIRCd::FindNickOnly(const std::string &nick)
78 user_hash::iterator iter = this->Users->clientlist->find(nick);
80 if (iter == this->Users->clientlist->end())
86 User* InspIRCd::FindNickOnly(const char* nick)
88 user_hash::iterator iter = this->Users->clientlist->find(nick);
90 if (iter == this->Users->clientlist->end())
96 User *InspIRCd::FindUUID(const std::string &uid)
98 return FindUUID(uid.c_str());
101 User *InspIRCd::FindUUID(const char *uid)
103 user_hash::iterator finduuid = this->Users->uuidlist->find(uid);
105 if (finduuid == this->Users->uuidlist->end())
108 return finduuid->second;
111 /* find a channel record by channel name and return a pointer to it */
112 Channel* InspIRCd::FindChan(const char* chan)
114 chan_hash::iterator iter = chanlist->find(chan);
116 if (iter == chanlist->end())
117 /* Couldn't find it */
123 Channel* InspIRCd::FindChan(const std::string &chan)
125 chan_hash::iterator iter = chanlist->find(chan);
127 if (iter == chanlist->end())
128 /* Couldn't find it */
134 /* Send an error notice to all users, registered or not */
135 void InspIRCd::SendError(const std::string &s)
137 for (std::vector<LocalUser*>::const_iterator i = this->Users->local_users.begin(); i != this->Users->local_users.end(); i++)
140 if (u->registered == REG_ALL)
142 u->WriteServ("NOTICE %s :%s",u->nick.c_str(),s.c_str());
146 /* Unregistered connections receive ERROR, not a NOTICE */
147 u->Write("ERROR :" + s);
152 /* return channel count */
153 long InspIRCd::ChannelCount()
155 return chanlist->size();
158 bool InspIRCd::IsValidMask(const std::string &mask)
160 const char* dest = mask.c_str();
164 for (const char* i = dest; *i; i++)
166 /* out of range character, bad mask */
167 if (*i < 32 || *i > 126)
183 /* valid masks only have 1 ! and @ */
184 if (exclamation != 1 || atsign != 1)
187 if (mask.length() > 250)
193 /* true for valid channel name, false else */
194 bool IsChannelHandler::Call(const char *chname, size_t max)
196 const char *c = chname + 1;
198 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
199 if (!chname || *chname != '#')
217 size_t len = c - chname;
218 /* too long a name - note funky pointer arithmetic here. */
227 /* true for valid nickname, false else */
228 bool IsNickHandler::Call(const char* n, size_t max)
234 for (const char* i = n; *i; i++, p++)
236 if ((*i >= 'A') && (*i <= '}'))
238 /* "A"-"}" can occur anywhere in a nickname */
242 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
244 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
248 /* invalid character! abort */
252 /* too long? or not -- pointer arithmetic rocks */
256 /* return true for good ident, false else */
257 bool IsIdentHandler::Call(const char* n)
262 for (const char* i = n; *i; i++)
264 if ((*i >= 'A') && (*i <= '}'))
269 if (((*i >= '0') && (*i <= '9')) || (*i == '-') || (*i == '.'))
280 bool IsSIDHandler::Call(const std::string &str)
282 /* Returns true if the string given is exactly 3 characters long,
283 * starts with a digit, and the other two characters are A-Z or digits
285 return ((str.length() == 3) && isdigit(str[0]) &&
286 ((str[1] >= 'A' && str[1] <= 'Z') || isdigit(str[1])) &&
287 ((str[2] >= 'A' && str[2] <= 'Z') || isdigit(str[2])));
290 /* open the proper logfile */
291 bool InspIRCd::OpenLog(char**, int)
293 if (!Config->cmdline.writelog) return true; // Skip opening default log if -nolog
295 if (Config->cmdline.startup_log.empty())
296 Config->cmdline.startup_log = "logs/startup.log";
297 FILE* startup = fopen(Config->cmdline.startup_log.c_str(), "a+");
304 FileWriter* fw = new FileWriter(startup);
305 FileLogStream *f = new FileLogStream((Config->cmdline.forcedebug ? DEBUG : DEFAULT), fw);
307 this->Logs->AddLogType("*", f, true);
312 void InspIRCd::CheckRoot()
316 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
317 this->Logs->Log("STARTUP",DEFAULT,"Cant start as root");
318 Exit(EXIT_STATUS_ROOT);
322 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const std::string &text)
324 std::string copy_text = text;
326 ModResult MOD_RESULT;
327 FIRST_MOD_RESULT(OnWhoisLine, MOD_RESULT, (user, dest, numeric, copy_text));
329 if (MOD_RESULT != MOD_RES_DENY)
330 user->WriteServ("%d %s", numeric, copy_text.c_str());
333 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const char* format, ...)
335 char textbuffer[MAXBUF];
337 va_start (argsPtr, format);
338 vsnprintf(textbuffer, MAXBUF, format, argsPtr);
341 this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));
344 /** Refactored by Brain, Jun 2009. Much faster with some clever O(1) array
345 * lookups and pointer maths.
347 long InspIRCd::Duration(const std::string &str)
349 unsigned char multiplier = 0;
354 /* Iterate each item in the string, looking for number or multiplier */
355 for (std::string::const_reverse_iterator i = str.rbegin(); i != str.rend(); ++i)
357 /* Found a number, queue it onto the current number */
358 if ((*i >= '0') && (*i <= '9'))
360 subtotal = subtotal + ((*i - '0') * times);
365 /* Found something thats not a number, find out how much
366 * it multiplies the built up number by, multiply the total
367 * and reset the built up number.
370 total += subtotal * duration_multi[multiplier];
372 /* Next subtotal please */
380 total += subtotal * duration_multi[multiplier];
383 /* Any trailing values built up are treated as raw seconds */
384 return total + subtotal;
387 bool InspIRCd::ULine(const std::string& sserver)
392 return (Config->ulines.find(sserver.c_str()) != Config->ulines.end());
395 bool InspIRCd::SilentULine(const std::string& sserver)
397 std::map<irc::string,bool>::iterator n = Config->ulines.find(sserver.c_str());
398 if (n != Config->ulines.end())
404 std::string InspIRCd::TimeString(time_t curtime)
406 return std::string(ctime(&curtime),24);
409 // You should only pass a single character to this.
410 void InspIRCd::AddExtBanChar(char c)
412 std::string &tok = Config->data005;
413 std::string::size_type ebpos = tok.find(" EXTBAN=,");
415 if (ebpos == std::string::npos)
417 tok.append(" EXTBAN=,");
423 while (isalpha(tok[ebpos]) && tok[ebpos] < c)
425 tok.insert(ebpos, 1, c);
429 std::string InspIRCd::GenRandomStr(int length, bool printable)
431 char* buf = new char[length];
432 GenRandom(buf, length);
435 for(int i=0; i < length; i++)
436 rv[i] = printable ? 0x3F + (buf[i] & 0x3F) : buf[i];
441 // NOTE: this has a slight bias for lower values if max is not a power of 2.
442 // Don't use it if that matters.
443 unsigned long InspIRCd::GenRandomInt(unsigned long max)
446 GenRandom((char*)&rv, sizeof(rv));
450 // This is overridden by a higher-quality algorithm when SSL support is loaded
451 void GenRandomHandler::Call(char *output, size_t max)
453 for(unsigned int i=0; i < max; i++)
454 output[i] = random();
457 ModResult OnCheckExemptionHandler::Call(User* user, Channel* chan, const std::string& restriction)
459 unsigned int mypfx = chan->GetPrefixValue(user);
463 irc::spacesepstream defaultstream(ServerInstance->Config->ConfValue("options")->getString("exemptchanops"));
465 while (defaultstream.GetToken(current))
467 std::string::size_type pos = current.find(':');
468 if (pos == std::string::npos)
470 if (current.substr(0,pos) == restriction)
471 minmode = current[pos+1];
474 ModeHandler* mh = ServerInstance->Modes->FindMode(minmode, MODETYPE_CHANNEL);
475 if (mh && mypfx >= mh->GetPrefixRank())
476 return MOD_RES_ALLOW;
477 if (mh || minmode == '*')
479 return MOD_RES_PASSTHRU;