1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 /* $Core: libIRCDhelper */
19 #include "exitcodes.h"
22 * Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level'
23 * is greater than the configured loglevel.
25 void InspIRCd::Log(int level, const char* text, ...)
28 char textbuffer[65536];
30 va_start(argsPtr, text);
31 vsnprintf(textbuffer, 65536, text, argsPtr);
34 this->Log(level, std::string(textbuffer));
37 void InspIRCd::Log(int level, const std::string &text)
39 this->Logs->Log("DEPRECATED", level, "Deprecated use of InspIRCd::Log(), message = %s", text.c_str());
42 std::string InspIRCd::GetServerDescription(const char* servername)
44 std::string description;
46 FOREACH_MOD_I(this,I_OnGetServerDescription,OnGetServerDescription(servername,description));
48 if (!description.empty())
54 // not a remote server that can be found, it must be me.
55 return Config->ServerDesc;
59 /* Find a user record by nickname and return a pointer to it */
60 User* InspIRCd::FindNick(const std::string &nick)
62 if (!nick.empty() && isdigit(*nick.begin()))
63 return FindUUID(nick);
65 user_hash::iterator iter = this->Users->clientlist->find(nick);
67 if (iter == this->Users->clientlist->end())
68 /* Couldn't find it */
74 User* InspIRCd::FindNick(const char* nick)
77 return FindUUID(nick);
79 user_hash::iterator iter = this->Users->clientlist->find(nick);
81 if (iter == this->Users->clientlist->end())
87 User* InspIRCd::FindNickOnly(const std::string &nick)
89 user_hash::iterator iter = this->Users->clientlist->find(nick);
91 if (iter == this->Users->clientlist->end())
97 User* InspIRCd::FindNickOnly(const char* nick)
99 user_hash::iterator iter = this->Users->clientlist->find(nick);
101 if (iter == this->Users->clientlist->end())
107 User *InspIRCd::FindUUID(const std::string &uid)
109 return FindUUID(uid.c_str());
112 User *InspIRCd::FindUUID(const char *uid)
114 user_hash::iterator finduuid = this->Users->uuidlist->find(uid);
116 if (finduuid == this->Users->uuidlist->end())
119 return finduuid->second;
122 /* find a channel record by channel name and return a pointer to it */
123 Channel* InspIRCd::FindChan(const char* chan)
125 chan_hash::iterator iter = chanlist->find(chan);
127 if (iter == chanlist->end())
128 /* Couldn't find it */
134 Channel* InspIRCd::FindChan(const std::string &chan)
136 chan_hash::iterator iter = chanlist->find(chan);
138 if (iter == chanlist->end())
139 /* Couldn't find it */
145 /* Send an error notice to all users, registered or not */
146 void InspIRCd::SendError(const std::string &s)
148 for (std::vector<User*>::const_iterator i = this->Users->local_users.begin(); i != this->Users->local_users.end(); i++)
150 if ((*i)->registered == REG_ALL)
152 (*i)->WriteServ("NOTICE %s :%s",(*i)->nick,s.c_str());
156 /* Unregistered connections receive ERROR, not a NOTICE */
157 (*i)->Write("ERROR :" + s);
159 /* This might generate a whole load of EAGAIN, but we dont really
160 * care about this, as if we call SendError something catastrophic
161 * has occured anyway, and we wont receive the events for these.
163 (*i)->FlushWriteBuf();
167 /* return channel count */
168 long InspIRCd::ChannelCount()
170 return chanlist->size();
173 bool InspIRCd::IsValidMask(const std::string &mask)
175 char* dest = (char*)mask.c_str();
179 for (char* i = dest; *i; i++)
181 /* out of range character, bad mask */
182 if (*i < 32 || *i > 126)
198 /* valid masks only have 1 ! and @ */
199 if (exclamation != 1 || atsign != 1)
205 /* true for valid channel name, false else */
206 bool InspIRCd::IsChannel(const char *chname)
210 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
211 if (!chname || *chname != '#')
216 c = (char *)chname + 1;
230 /* too long a name - note funky pointer arithmetic here. */
231 if ((c - chname) > CHANMAX)
239 /* true for valid nickname, false else */
240 bool IsNickHandler::Call(const char* n)
246 for (char* i = (char*)n; *i; i++, p++)
248 if ((*i >= 'A') && (*i <= '}'))
250 /* "A"-"}" can occur anywhere in a nickname */
254 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
256 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
260 /* invalid character! abort */
264 /* too long? or not -- pointer arithmetic rocks */
265 return (p < NICKMAX - 1);
268 /* return true for good ident, false else */
269 bool IsIdentHandler::Call(const char* n)
274 for (char* i = (char*)n; *i; i++)
276 if ((*i >= 'A') && (*i <= '}'))
281 if (((*i >= '0') && (*i <= '9')) || (*i == '-') || (*i == '.'))
292 bool InspIRCd::IsSID(const std::string &str)
294 /* Returns true if the string given is exactly 3 characters long,
295 * starts with a digit, and the other two characters are A-Z or digits
297 return ((str.length() == 3) && isdigit(str[0]) &&
298 ((str[1] >= 'A' && str[1] <= 'Z') || isdigit(str[1])) &&
299 ((str[2] >= 'A' && str[2] <= 'Z') || isdigit(str[2])));
302 /* open the proper logfile */
303 bool InspIRCd::OpenLog(char**, int)
305 /* This function only happens at startup now (log reopening is done at OnReadConfig stage now instead of rehash) */
308 this->Logs->SetupNoFork();
310 if (!Config->writelog) return true; // Skip opening default log if -nolog
311 Config->MyDir = Config->GetFullProgDir();
313 if (!*this->LogFileName)
315 if (Config->logpath.empty())
317 Config->logpath = Config->MyDir + "/ircd.log";
320 Config->log_file = fopen(Config->logpath.c_str(),"a+");
324 Config->log_file = fopen(this->LogFileName,"a+");
327 if (!Config->log_file)
332 FileWriter* fw = new FileWriter(this, Config->log_file);
333 FileLogStream *f = new FileLogStream(this, (Config->forcedebug ? DEBUG : Config->LogLevel), fw);
335 this->Logs->AddLogType("*", f, true);
340 void InspIRCd::CheckRoot()
344 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
345 this->Logs->Log("STARTUP",DEFAULT,"Cant start as root");
346 Exit(EXIT_STATUS_ROOT);
350 void InspIRCd::CheckDie()
352 if (*Config->DieValue)
354 printf("WARNING: %s\n\n",Config->DieValue);
355 this->Logs->Log("CONFIG",DEFAULT,"Died because of <die> tag: %s",Config->DieValue);
356 Exit(EXIT_STATUS_DIETAG);
360 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const std::string &text)
362 std::string copy_text = text;
365 FOREACH_RESULT_I(this, I_OnWhoisLine, OnWhoisLine(user, dest, numeric, copy_text));
368 user->WriteServ("%d %s", numeric, copy_text.c_str());
371 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const char* format, ...)
373 char textbuffer[MAXBUF];
375 va_start (argsPtr, format);
376 vsnprintf(textbuffer, MAXBUF, format, argsPtr);
379 this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));
382 /** Refactored by Brain, Jun 2008. Much faster with some clever O(1) array
383 * lookups and pointer maths.
385 long InspIRCd::Duration(const std::string &str)
387 unsigned char multiplier = 0;
392 /* Iterate each item in the string, looking for number or multiplier */
393 for (std::string::const_reverse_iterator i = str.rbegin(); i != str.rend(); ++i)
395 /* Found a number, queue it onto the current number */
396 if ((*i >= '0') && (*i <= '9'))
398 subtotal = subtotal + ((*i - '0') * times);
403 /* Found something thats not a number, find out how much
404 * it multiplies the built up number by, multiply the total
405 * and reset the built up number.
408 total += subtotal * duration_multi[multiplier];
410 /* Next subtotal please */
418 total += subtotal * duration_multi[multiplier];
421 /* Any trailing values built up are treated as raw seconds */
422 return total + subtotal;
425 bool InspIRCd::ULine(const char* sserver)
432 return (Config->ulines.find(sserver) != Config->ulines.end());
435 bool InspIRCd::SilentULine(const char* sserver)
437 std::map<irc::string,bool>::iterator n = Config->ulines.find(sserver);
438 if (n != Config->ulines.end())
443 std::string InspIRCd::TimeString(time_t curtime)
445 return std::string(ctime(&curtime),24);