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"
21 std::string InspIRCd::GetServerDescription(const char* servername)
23 std::string description;
25 FOREACH_MOD_I(this,I_OnGetServerDescription,OnGetServerDescription(servername,description));
27 if (!description.empty())
33 // not a remote server that can be found, it must be me.
34 return Config->ServerDesc;
38 /* Find a user record by nickname and return a pointer to it */
39 User* InspIRCd::FindNick(const std::string &nick)
41 if (!nick.empty() && isdigit(*nick.begin()))
42 return FindUUID(nick);
44 user_hash::iterator iter = this->Users->clientlist->find(nick);
46 if (iter == this->Users->clientlist->end())
47 /* Couldn't find it */
53 User* InspIRCd::FindNick(const char* nick)
56 return FindUUID(nick);
58 user_hash::iterator iter = this->Users->clientlist->find(nick);
60 if (iter == this->Users->clientlist->end())
66 User* InspIRCd::FindNickOnly(const std::string &nick)
68 user_hash::iterator iter = this->Users->clientlist->find(nick);
70 if (iter == this->Users->clientlist->end())
76 User* InspIRCd::FindNickOnly(const char* nick)
78 user_hash::iterator iter = this->Users->clientlist->find(nick);
80 if (iter == this->Users->clientlist->end())
86 User *InspIRCd::FindUUID(const std::string &uid)
88 return FindUUID(uid.c_str());
91 User *InspIRCd::FindUUID(const char *uid)
93 user_hash::iterator finduuid = this->Users->uuidlist->find(uid);
95 if (finduuid == this->Users->uuidlist->end())
98 return finduuid->second;
101 /* find a channel record by channel name and return a pointer to it */
102 Channel* InspIRCd::FindChan(const char* chan)
104 chan_hash::iterator iter = chanlist->find(chan);
106 if (iter == chanlist->end())
107 /* Couldn't find it */
113 Channel* InspIRCd::FindChan(const std::string &chan)
115 chan_hash::iterator iter = chanlist->find(chan);
117 if (iter == chanlist->end())
118 /* Couldn't find it */
124 /* Send an error notice to all users, registered or not */
125 void InspIRCd::SendError(const std::string &s)
127 for (std::vector<User*>::const_iterator i = this->Users->local_users.begin(); i != this->Users->local_users.end(); i++)
129 if ((*i)->registered == REG_ALL)
131 (*i)->WriteServ("NOTICE %s :%s",(*i)->nick,s.c_str());
135 /* Unregistered connections receive ERROR, not a NOTICE */
136 (*i)->Write("ERROR :" + s);
138 /* This might generate a whole load of EAGAIN, but we dont really
139 * care about this, as if we call SendError something catastrophic
140 * has occured anyway, and we wont receive the events for these.
142 (*i)->FlushWriteBuf();
146 /* return channel count */
147 long InspIRCd::ChannelCount()
149 return chanlist->size();
152 bool InspIRCd::IsValidMask(const std::string &mask)
154 char* dest = (char*)mask.c_str();
158 for (char* i = dest; *i; i++)
160 /* out of range character, bad mask */
161 if (*i < 32 || *i > 126)
177 /* valid masks only have 1 ! and @ */
178 if (exclamation != 1 || atsign != 1)
184 /* true for valid channel name, false else */
185 bool InspIRCd::IsChannel(const char *chname)
189 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
190 if (!chname || *chname != '#')
195 c = (char *)chname + 1;
209 /* too long a name - note funky pointer arithmetic here. */
210 if ((c - chname) > CHANMAX)
218 /* true for valid nickname, false else */
219 bool IsNickHandler::Call(const char* n)
225 for (char* i = (char*)n; *i; i++, p++)
227 if ((*i >= 'A') && (*i <= '}'))
229 /* "A"-"}" can occur anywhere in a nickname */
233 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
235 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
239 /* invalid character! abort */
243 /* too long? or not -- pointer arithmetic rocks */
244 return (p < NICKMAX - 1);
247 /* return true for good ident, false else */
248 bool IsIdentHandler::Call(const char* n)
253 for (char* i = (char*)n; *i; i++)
255 if ((*i >= 'A') && (*i <= '}'))
260 if (((*i >= '0') && (*i <= '9')) || (*i == '-') || (*i == '.'))
271 bool InspIRCd::IsSID(const std::string &str)
273 /* Returns true if the string given is exactly 3 characters long,
274 * starts with a digit, and the other two characters are A-Z or digits
276 return ((str.length() == 3) && isdigit(str[0]) &&
277 ((str[1] >= 'A' && str[1] <= 'Z') || isdigit(str[1])) &&
278 ((str[2] >= 'A' && str[2] <= 'Z') || isdigit(str[2])));
281 /* open the proper logfile */
282 bool InspIRCd::OpenLog(char**, int)
284 /* This function only happens at startup now (log reopening is done at OnReadConfig stage now instead of rehash) */
287 this->Logs->SetupNoFork();
289 if (!Config->writelog) return true; // Skip opening default log if -nolog
290 Config->MyDir = Config->GetFullProgDir();
292 if (!*this->LogFileName)
294 if (Config->logpath.empty())
296 Config->logpath = Config->MyDir + "/ircd.log";
299 Config->log_file = fopen(Config->logpath.c_str(),"a+");
303 Config->log_file = fopen(this->LogFileName,"a+");
306 if (!Config->log_file)
311 FileWriter* fw = new FileWriter(this, Config->log_file);
312 FileLogStream *f = new FileLogStream(this, (Config->forcedebug ? DEBUG : Config->LogLevel), fw);
314 this->Logs->AddLogType("*", f, true);
319 void InspIRCd::CheckRoot()
323 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
324 this->Logs->Log("STARTUP",DEFAULT,"Cant start as root");
325 Exit(EXIT_STATUS_ROOT);
329 void InspIRCd::CheckDie()
331 if (*Config->DieValue)
333 printf("WARNING: %s\n\n",Config->DieValue);
334 this->Logs->Log("CONFIG",DEFAULT,"Died because of <die> tag: %s",Config->DieValue);
335 Exit(EXIT_STATUS_DIETAG);
339 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const std::string &text)
341 std::string copy_text = text;
344 FOREACH_RESULT_I(this, I_OnWhoisLine, OnWhoisLine(user, dest, numeric, copy_text));
347 user->WriteServ("%d %s", numeric, copy_text.c_str());
350 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const char* format, ...)
352 char textbuffer[MAXBUF];
354 va_start (argsPtr, format);
355 vsnprintf(textbuffer, MAXBUF, format, argsPtr);
358 this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));
361 /** Refactored by Brain, Jun 2008. Much faster with some clever O(1) array
362 * lookups and pointer maths.
364 long InspIRCd::Duration(const std::string &str)
366 unsigned char multiplier = 0;
371 /* Iterate each item in the string, looking for number or multiplier */
372 for (std::string::const_reverse_iterator i = str.rbegin(); i != str.rend(); ++i)
374 /* Found a number, queue it onto the current number */
375 if ((*i >= '0') && (*i <= '9'))
377 subtotal = subtotal + ((*i - '0') * times);
382 /* Found something thats not a number, find out how much
383 * it multiplies the built up number by, multiply the total
384 * and reset the built up number.
387 total += subtotal * duration_multi[multiplier];
389 /* Next subtotal please */
397 total += subtotal * duration_multi[multiplier];
400 /* Any trailing values built up are treated as raw seconds */
401 return total + subtotal;
404 bool InspIRCd::ULine(const char* sserver)
411 return (Config->ulines.find(sserver) != Config->ulines.end());
414 bool InspIRCd::SilentULine(const char* sserver)
416 std::map<irc::string,bool>::iterator n = Config->ulines.find(sserver);
417 if (n != Config->ulines.end())
422 std::string InspIRCd::TimeString(time_t curtime)
424 return std::string(ctime(&curtime),24);