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("WARNING", DEFAULT, "Deprecated call to InspIRCd::Log()! - log message follows");
40 this->Logs->Log("DEPRECATED", level, text);
43 std::string InspIRCd::GetServerDescription(const char* servername)
45 std::string description;
47 FOREACH_MOD_I(this,I_OnGetServerDescription,OnGetServerDescription(servername,description));
49 if (!description.empty())
55 // not a remote server that can be found, it must be me.
56 return Config->ServerDesc;
60 /* Find a user record by nickname and return a pointer to it */
61 User* InspIRCd::FindNick(const std::string &nick)
63 if (!nick.empty() && isdigit(*nick.begin()))
64 return FindUUID(nick);
66 user_hash::iterator iter = this->Users->clientlist->find(nick);
68 if (iter == this->Users->clientlist->end())
69 /* Couldn't find it */
75 User* InspIRCd::FindNick(const char* nick)
78 return FindUUID(nick);
80 user_hash::iterator iter = this->Users->clientlist->find(nick);
82 if (iter == this->Users->clientlist->end())
88 User* InspIRCd::FindNickOnly(const std::string &nick)
90 user_hash::iterator iter = this->Users->clientlist->find(nick);
92 if (iter == this->Users->clientlist->end())
98 User* InspIRCd::FindNickOnly(const char* nick)
100 user_hash::iterator iter = this->Users->clientlist->find(nick);
102 if (iter == this->Users->clientlist->end())
108 User *InspIRCd::FindUUID(const std::string &uid)
110 return FindUUID(uid.c_str());
113 User *InspIRCd::FindUUID(const char *uid)
115 user_hash::iterator finduuid = this->Users->uuidlist->find(uid);
117 if (finduuid == this->Users->uuidlist->end())
120 return finduuid->second;
123 /* find a channel record by channel name and return a pointer to it */
124 Channel* InspIRCd::FindChan(const char* chan)
126 chan_hash::iterator iter = chanlist->find(chan);
128 if (iter == chanlist->end())
129 /* Couldn't find it */
135 Channel* InspIRCd::FindChan(const std::string &chan)
137 chan_hash::iterator iter = chanlist->find(chan);
139 if (iter == chanlist->end())
140 /* Couldn't find it */
146 /* Send an error notice to all users, registered or not */
147 void InspIRCd::SendError(const std::string &s)
149 for (std::vector<User*>::const_iterator i = this->Users->local_users.begin(); i != this->Users->local_users.end(); i++)
151 if ((*i)->registered == REG_ALL)
153 (*i)->WriteServ("NOTICE %s :%s",(*i)->nick,s.c_str());
157 /* Unregistered connections receive ERROR, not a NOTICE */
158 (*i)->Write("ERROR :" + s);
160 /* This might generate a whole load of EAGAIN, but we dont really
161 * care about this, as if we call SendError something catastrophic
162 * has occured anyway, and we wont receive the events for these.
164 (*i)->FlushWriteBuf();
168 /* return channel count */
169 long InspIRCd::ChannelCount()
171 return chanlist->size();
174 bool InspIRCd::IsValidMask(const std::string &mask)
176 char* dest = (char*)mask.c_str();
180 for (char* i = dest; *i; i++)
182 /* out of range character, bad mask */
183 if (*i < 32 || *i > 126)
199 /* valid masks only have 1 ! and @ */
200 if (exclamation != 1 || atsign != 1)
206 /* true for valid channel name, false else */
207 bool InspIRCd::IsChannel(const char *chname)
211 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
212 if (!chname || *chname != '#')
217 c = (char *)chname + 1;
231 /* too long a name - note funky pointer arithmetic here. */
232 if ((c - chname) > CHANMAX)
240 /* true for valid nickname, false else */
241 bool IsNickHandler::Call(const char* n)
247 for (char* i = (char*)n; *i; i++, p++)
249 if ((*i >= 'A') && (*i <= '}'))
251 /* "A"-"}" can occur anywhere in a nickname */
255 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
257 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
261 /* invalid character! abort */
265 /* too long? or not -- pointer arithmetic rocks */
266 return (p < NICKMAX - 1);
269 /* return true for good ident, false else */
270 bool IsIdentHandler::Call(const char* n)
275 for (char* i = (char*)n; *i; i++)
277 if ((*i >= 'A') && (*i <= '}'))
282 if (((*i >= '0') && (*i <= '9')) || (*i == '-') || (*i == '.'))
293 bool InspIRCd::IsSID(const std::string &str)
295 /* Returns true if the string given is exactly 3 characters long,
296 * starts with a digit, and the other two characters are A-Z or digits
298 return ((str.length() == 3) && isdigit(str[0]) &&
299 ((str[1] >= 'A' && str[1] <= 'Z') || isdigit(str[1])) &&
300 ((str[2] >= 'A' && str[2] <= 'Z') || isdigit(str[2])));
303 /* open the proper logfile */
304 bool InspIRCd::OpenLog(char**, int)
306 Config->MyDir = Config->GetFullProgDir();
308 if (!*this->LogFileName)
310 if (Config->logpath.empty())
312 Config->logpath = Config->MyDir + "/ircd.log";
315 Config->log_file = fopen(Config->logpath.c_str(),"a+");
319 Config->log_file = fopen(this->LogFileName,"a+");
322 if (!Config->log_file)
327 FileLogStream *f = new FileLogStream(this, Config->LogLevel, Config->log_file);
328 this->Logs->AddLogType("*", f);
332 void InspIRCd::CheckRoot()
336 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
337 this->Log(DEFAULT,"Cant start as root");
338 Exit(EXIT_STATUS_ROOT);
342 void InspIRCd::CheckDie()
344 if (*Config->DieValue)
346 printf("WARNING: %s\n\n",Config->DieValue);
347 this->Log(DEFAULT,"Died because of <die> tag: %s",Config->DieValue);
348 Exit(EXIT_STATUS_DIETAG);
352 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const std::string &text)
354 std::string copy_text = text;
357 FOREACH_RESULT_I(this, I_OnWhoisLine, OnWhoisLine(user, dest, numeric, copy_text));
360 user->WriteServ("%d %s", numeric, copy_text.c_str());
363 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const char* format, ...)
365 char textbuffer[MAXBUF];
367 va_start (argsPtr, format);
368 vsnprintf(textbuffer, MAXBUF, format, argsPtr);
371 this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));
374 /** Refactored by Brain, Jun 2008. Much faster with some clever O(1) array
375 * lookups and pointer maths.
377 long InspIRCd::Duration(const std::string &str)
379 unsigned char multiplier = 0;
384 /* Iterate each item in the string, looking for number or multiplier */
385 for (std::string::const_reverse_iterator i = str.rbegin(); i != str.rend(); ++i)
387 /* Found a number, queue it onto the current number */
388 if ((*i >= '0') && (*i <= '9'))
390 subtotal = subtotal + ((*i - '0') * times);
395 /* Found something thats not a number, find out how much
396 * it multiplies the built up number by, multiply the total
397 * and reset the built up number.
400 total += subtotal * duration_multi[multiplier];
402 /* Next subtotal please */
410 total += subtotal * duration_multi[multiplier];
413 /* Any trailing values built up are treated as raw seconds */
414 return total + subtotal;
417 bool InspIRCd::ULine(const char* server)
424 return (Config->ulines.find(server) != Config->ulines.end());
427 bool InspIRCd::SilentULine(const char* server)
429 std::map<irc::string,bool>::iterator n = Config->ulines.find(server);
430 if (n != Config->ulines.end())
435 std::string InspIRCd::TimeString(time_t curtime)
437 return std::string(ctime(&curtime),24);