1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
18 #include "exitcodes.h"
20 std::string InspIRCd::GetServerDescription(const std::string& servername)
22 std::string description;
24 FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
26 if (!description.empty())
32 // not a remote server that can be found, it must be me.
33 return Config->ServerDesc;
37 /* Find a user record by nickname and return a pointer to it */
38 User* InspIRCd::FindNick(const std::string &nick)
40 if (!nick.empty() && isdigit(*nick.begin()))
41 return FindUUID(nick);
43 user_hash::iterator iter = this->Users->clientlist->find(nick);
45 if (iter == this->Users->clientlist->end())
46 /* Couldn't find it */
52 User* InspIRCd::FindNick(const char* nick)
55 return FindUUID(nick);
57 user_hash::iterator iter = this->Users->clientlist->find(nick);
59 if (iter == this->Users->clientlist->end())
65 User* InspIRCd::FindNickOnly(const std::string &nick)
67 user_hash::iterator iter = this->Users->clientlist->find(nick);
69 if (iter == this->Users->clientlist->end())
75 User* InspIRCd::FindNickOnly(const char* nick)
77 user_hash::iterator iter = this->Users->clientlist->find(nick);
79 if (iter == this->Users->clientlist->end())
85 User *InspIRCd::FindUUID(const std::string &uid)
87 return FindUUID(uid.c_str());
90 User *InspIRCd::FindUUID(const char *uid)
92 user_hash::iterator finduuid = this->Users->uuidlist->find(uid);
94 if (finduuid == this->Users->uuidlist->end())
97 return finduuid->second;
100 /* find a channel record by channel name and return a pointer to it */
101 Channel* InspIRCd::FindChan(const char* chan)
103 chan_hash::iterator iter = chanlist->find(chan);
105 if (iter == chanlist->end())
106 /* Couldn't find it */
112 Channel* InspIRCd::FindChan(const std::string &chan)
114 chan_hash::iterator iter = chanlist->find(chan);
116 if (iter == chanlist->end())
117 /* Couldn't find it */
123 /* Send an error notice to all users, registered or not */
124 void InspIRCd::SendError(const std::string &s)
126 for (std::vector<LocalUser*>::const_iterator i = this->Users->local_users.begin(); i != this->Users->local_users.end(); i++)
129 if (u->registered == REG_ALL)
131 u->WriteServ("NOTICE %s :%s",u->nick.c_str(),s.c_str());
135 /* Unregistered connections receive ERROR, not a NOTICE */
136 u->Write("ERROR :" + s);
141 /* return channel count */
142 long InspIRCd::ChannelCount()
144 return chanlist->size();
147 bool InspIRCd::IsValidMask(const std::string &mask)
149 const char* dest = mask.c_str();
153 for (const char* i = dest; *i; i++)
155 /* out of range character, bad mask */
156 if (*i < 32 || *i > 126)
172 /* valid masks only have 1 ! and @ */
173 if (exclamation != 1 || atsign != 1)
176 if (mask.length() > 250)
182 /* true for valid channel name, false else */
183 bool IsChannelHandler::Call(const char *chname, size_t max)
185 const char *c = chname + 1;
187 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
188 if (!chname || *chname != '#')
206 size_t len = c - chname;
207 /* too long a name - note funky pointer arithmetic here. */
216 /* true for valid nickname, false else */
217 bool IsNickHandler::Call(const char* n, size_t max)
223 for (const char* i = n; *i; i++, p++)
225 if ((*i >= 'A') && (*i <= '}'))
227 /* "A"-"}" can occur anywhere in a nickname */
231 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
233 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
237 /* invalid character! abort */
241 /* too long? or not -- pointer arithmetic rocks */
245 /* return true for good ident, false else */
246 bool IsIdentHandler::Call(const char* n)
251 for (const char* i = n; *i; i++)
253 if ((*i >= 'A') && (*i <= '}'))
258 if (((*i >= '0') && (*i <= '9')) || (*i == '-') || (*i == '.'))
269 bool IsSIDHandler::Call(const std::string &str)
271 /* Returns true if the string given is exactly 3 characters long,
272 * starts with a digit, and the other two characters are A-Z or digits
274 return ((str.length() == 3) && isdigit(str[0]) &&
275 ((str[1] >= 'A' && str[1] <= 'Z') || isdigit(str[1])) &&
276 ((str[2] >= 'A' && str[2] <= 'Z') || isdigit(str[2])));
279 /* open the proper logfile */
280 bool InspIRCd::OpenLog(char**, int)
282 if (!Config->cmdline.writelog) return true; // Skip opening default log if -nolog
284 if (Config->cmdline.startup_log.empty())
285 Config->cmdline.startup_log = "logs/startup.log";
286 FILE* startup = fopen(Config->cmdline.startup_log.c_str(), "a+");
293 FileWriter* fw = new FileWriter(startup);
294 FileLogStream *f = new FileLogStream((Config->cmdline.forcedebug ? DEBUG : DEFAULT), fw);
296 this->Logs->AddLogType("*", f, true);
301 void InspIRCd::CheckRoot()
305 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
306 this->Logs->Log("STARTUP",DEFAULT,"Cant start as root");
307 Exit(EXIT_STATUS_ROOT);
311 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const std::string &text)
313 std::string copy_text = text;
315 ModResult MOD_RESULT;
316 FIRST_MOD_RESULT(OnWhoisLine, MOD_RESULT, (user, dest, numeric, copy_text));
318 if (MOD_RESULT != MOD_RES_DENY)
319 user->WriteServ("%d %s", numeric, copy_text.c_str());
322 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const char* format, ...)
324 char textbuffer[MAXBUF];
326 va_start (argsPtr, format);
327 vsnprintf(textbuffer, MAXBUF, format, argsPtr);
330 this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));
333 /** Refactored by Brain, Jun 2009. Much faster with some clever O(1) array
334 * lookups and pointer maths.
336 long InspIRCd::Duration(const std::string &str)
338 unsigned char multiplier = 0;
343 /* Iterate each item in the string, looking for number or multiplier */
344 for (std::string::const_reverse_iterator i = str.rbegin(); i != str.rend(); ++i)
346 /* Found a number, queue it onto the current number */
347 if ((*i >= '0') && (*i <= '9'))
349 subtotal = subtotal + ((*i - '0') * times);
354 /* Found something thats not a number, find out how much
355 * it multiplies the built up number by, multiply the total
356 * and reset the built up number.
359 total += subtotal * duration_multi[multiplier];
361 /* Next subtotal please */
369 total += subtotal * duration_multi[multiplier];
372 /* Any trailing values built up are treated as raw seconds */
373 return total + subtotal;
376 bool InspIRCd::ULine(const std::string& sserver)
381 return (Config->ulines.find(sserver.c_str()) != Config->ulines.end());
384 bool InspIRCd::SilentULine(const std::string& sserver)
386 std::map<irc::string,bool>::iterator n = Config->ulines.find(sserver.c_str());
387 if (n != Config->ulines.end())
393 std::string InspIRCd::TimeString(time_t curtime)
395 return std::string(ctime(&curtime),24);
398 // You should only pass a single character to this.
399 void InspIRCd::AddExtBanChar(char c)
401 std::string &tok = Config->data005;
402 std::string::size_type ebpos = tok.find(" EXTBAN=,");
404 if (ebpos == std::string::npos)
406 tok.append(" EXTBAN=,");
412 while (isalpha(tok[ebpos]) && tok[ebpos] < c)
414 tok.insert(ebpos, 1, c);
418 std::string InspIRCd::GenRandomStr(int length, bool printable)
420 char* buf = new char[length];
421 GenRandom(buf, length);
424 for(int i=0; i < length; i++)
425 rv[i] = printable ? 0x3F + (buf[i] & 0x3F) : buf[i];
430 // NOTE: this has a slight bias for lower values if max is not a power of 2.
431 // Don't use it if that matters.
432 unsigned long InspIRCd::GenRandomInt(unsigned long max)
435 GenRandom((char*)&rv, sizeof(rv));
439 // This is overridden by a higher-quality algorithm when SSL support is loaded
440 void GenRandomHandler::Call(char *output, size_t max)
442 for(unsigned int i=0; i < max; i++)
443 output[i] = random();
446 ModResult OnCheckExemptionHandler::Call(User* user, Channel* chan, const std::string& restriction)
448 unsigned int mypfx = chan->GetPrefixValue(user);
452 irc::spacesepstream defaultstream(ServerInstance->Config->ConfValue("options")->getString("exemptchanops"));
454 while (defaultstream.GetToken(current))
456 std::string::size_type pos = current.find(':');
457 if (pos == std::string::npos)
459 if (current.substr(0,pos) == restriction)
460 minmode = current[pos+1];
463 ModeHandler* mh = ServerInstance->Modes->FindMode(minmode, MODETYPE_CHANNEL);
464 if (mh && mypfx >= mh->GetPrefixRank())
465 return MOD_RES_ALLOW;
466 if (mh || minmode == '*')
468 return MOD_RES_PASSTHRU;