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 static char TIMESTR[26];
22 static time_t LAST = 0;
25 * Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level'
26 * is greater than the configured loglevel.
28 void InspIRCd::Log(int level, const char* text, ...)
30 /* sanity check, just in case */
31 if (!this->Config || !this->Logger)
34 /* Do this check again here so that we save pointless vsnprintf calls */
35 if ((level < Config->LogLevel) && !Config->forcedebug)
39 char textbuffer[65536];
41 va_start(argsPtr, text);
42 vsnprintf(textbuffer, 65536, text, argsPtr);
45 this->Log(level, std::string(textbuffer));
48 void InspIRCd::Log(int level, const std::string &text)
50 /* sanity check, just in case */
51 if (!this->Config || !this->Logger)
54 /* If we were given -debug we output all messages, regardless of configured loglevel */
55 if ((level < Config->LogLevel) && !Config->forcedebug)
60 time_t local = Time();
61 struct tm *timeinfo = localtime(&local);
63 strlcpy(TIMESTR,asctime(timeinfo),26);
68 if (Config->log_file && Config->writelog)
70 std::string out = std::string(TIMESTR) + " " + text.c_str() + "\n";
71 this->Logger->WriteLogLine(out);
76 printf("%s %s\n", TIMESTR, text.c_str());
80 std::string InspIRCd::GetServerDescription(const char* servername)
82 std::string description;
84 FOREACH_MOD_I(this,I_OnGetServerDescription,OnGetServerDescription(servername,description));
86 if (!description.empty())
92 // not a remote server that can be found, it must be me.
93 return Config->ServerDesc;
97 /* Find a user record by nickname and return a pointer to it */
98 User* InspIRCd::FindNick(const std::string &nick)
100 if (!nick.empty() && isdigit(*nick.begin()))
101 return FindUUID(nick);
103 user_hash::iterator iter = this->Users->clientlist->find(nick);
105 if (iter == this->Users->clientlist->end())
106 /* Couldn't find it */
112 User* InspIRCd::FindNick(const char* nick)
115 return FindUUID(nick);
117 user_hash::iterator iter = this->Users->clientlist->find(nick);
119 if (iter == this->Users->clientlist->end())
125 User* InspIRCd::FindNickOnly(const std::string &nick)
127 user_hash::iterator iter = this->Users->clientlist->find(nick);
129 if (iter == this->Users->clientlist->end())
135 User* InspIRCd::FindNickOnly(const char* nick)
137 user_hash::iterator iter = this->Users->clientlist->find(nick);
139 if (iter == this->Users->clientlist->end())
145 User *InspIRCd::FindUUID(const std::string &uid)
147 return FindUUID(uid.c_str());
150 User *InspIRCd::FindUUID(const char *uid)
152 user_hash::iterator finduuid = this->Users->uuidlist->find(uid);
154 if (finduuid == this->Users->uuidlist->end())
157 return finduuid->second;
160 /* find a channel record by channel name and return a pointer to it */
161 Channel* InspIRCd::FindChan(const char* chan)
163 chan_hash::iterator iter = chanlist->find(chan);
165 if (iter == chanlist->end())
166 /* Couldn't find it */
172 Channel* InspIRCd::FindChan(const std::string &chan)
174 chan_hash::iterator iter = chanlist->find(chan);
176 if (iter == chanlist->end())
177 /* Couldn't find it */
183 /* Send an error notice to all users, registered or not */
184 void InspIRCd::SendError(const std::string &s)
186 for (std::vector<User*>::const_iterator i = this->Users->local_users.begin(); i != this->Users->local_users.end(); i++)
188 if ((*i)->registered == REG_ALL)
190 (*i)->WriteServ("NOTICE %s :%s",(*i)->nick,s.c_str());
194 /* Unregistered connections receive ERROR, not a NOTICE */
195 (*i)->Write("ERROR :" + s);
197 /* This might generate a whole load of EAGAIN, but we dont really
198 * care about this, as if we call SendError something catastrophic
199 * has occured anyway, and we wont receive the events for these.
201 (*i)->FlushWriteBuf();
205 /* return channel count */
206 long InspIRCd::ChannelCount()
208 return chanlist->size();
211 bool InspIRCd::IsValidMask(const std::string &mask)
213 char* dest = (char*)mask.c_str();
217 for (char* i = dest; *i; i++)
219 /* out of range character, bad mask */
220 if (*i < 32 || *i > 126)
236 /* valid masks only have 1 ! and @ */
237 if (exclamation != 1 || atsign != 1)
243 /* true for valid channel name, false else */
244 bool InspIRCd::IsChannel(const char *chname)
248 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
249 if (!chname || *chname != '#')
254 c = (char *)chname + 1;
268 /* too long a name - note funky pointer arithmetic here. */
269 if ((c - chname) > CHANMAX)
277 /* true for valid nickname, false else */
278 bool IsNickHandler::Call(const char* n)
284 for (char* i = (char*)n; *i; i++, p++)
286 if ((*i >= 'A') && (*i <= '}'))
288 /* "A"-"}" can occur anywhere in a nickname */
292 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
294 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
298 /* invalid character! abort */
302 /* too long? or not -- pointer arithmetic rocks */
303 return (p < NICKMAX - 1);
306 /* return true for good ident, false else */
307 bool IsIdentHandler::Call(const char* n)
312 for (char* i = (char*)n; *i; i++)
314 if ((*i >= 'A') && (*i <= '}'))
319 if (((*i >= '0') && (*i <= '9')) || (*i == '-') || (*i == '.'))
330 bool InspIRCd::IsSID(const std::string &str)
332 /* Returns true if the string given is exactly 3 characters long,
333 * starts with a digit, and the other two characters are A-Z or digits
335 return ((str.length() == 3) && isdigit(str[0]) &&
336 ((str[1] >= 'A' && str[1] <= 'Z') || isdigit(str[1])) &&
337 ((str[2] >= 'A' && str[2] <= 'Z') || isdigit(str[2])));
340 /* open the proper logfile */
341 bool InspIRCd::OpenLog(char**, int)
343 Config->MyDir = Config->GetFullProgDir();
345 if (!*this->LogFileName)
347 if (Config->logpath.empty())
349 Config->logpath = Config->MyDir + "/ircd.log";
352 Config->log_file = fopen(Config->logpath.c_str(),"a+");
356 Config->log_file = fopen(this->LogFileName,"a+");
359 if (!Config->log_file)
365 this->Logger = new FileLogger(this, Config->log_file);
369 void InspIRCd::CheckRoot()
373 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
374 this->Log(DEFAULT,"Cant start as root");
375 Exit(EXIT_STATUS_ROOT);
379 void InspIRCd::CheckDie()
381 if (*Config->DieValue)
383 printf("WARNING: %s\n\n",Config->DieValue);
384 this->Log(DEFAULT,"Died because of <die> tag: %s",Config->DieValue);
385 Exit(EXIT_STATUS_DIETAG);
389 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const std::string &text)
391 std::string copy_text = text;
394 FOREACH_RESULT_I(this, I_OnWhoisLine, OnWhoisLine(user, dest, numeric, copy_text));
397 user->WriteServ("%d %s", numeric, copy_text.c_str());
400 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const char* format, ...)
402 char textbuffer[MAXBUF];
404 va_start (argsPtr, format);
405 vsnprintf(textbuffer, MAXBUF, format, argsPtr);
408 this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));
411 /** Refactored by Brain, Jun 2008. Much faster with some clever O(1) array
412 * lookups and pointer maths.
414 long InspIRCd::Duration(const std::string &str)
416 unsigned char multiplier = 0;
421 /* Iterate each item in the string, looking for number or multiplier */
422 for (std::string::const_reverse_iterator i = str.rbegin(); i != str.rend(); ++i)
424 /* Found a number, queue it onto the current number */
425 if ((*i >= '0') && (*i <= '9'))
427 subtotal = subtotal + ((*i - '0') * times);
432 /* Found something thats not a number, find out how much
433 * it multiplies the built up number by, multiply the total
434 * and reset the built up number.
437 total += subtotal * duration_multi[multiplier];
439 /* Next subtotal please */
447 total += subtotal * duration_multi[multiplier];
450 /* Any trailing values built up are treated as raw seconds */
451 return total + subtotal;
454 bool InspIRCd::ULine(const char* server)
461 return (Config->ulines.find(server) != Config->ulines.end());
464 bool InspIRCd::SilentULine(const char* server)
466 std::map<irc::string,bool>::iterator n = Config->ulines.find(server);
467 if (n != Config->ulines.end())
472 std::string InspIRCd::TimeString(time_t curtime)
474 return std::string(ctime(&curtime),24);