1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 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 * ---------------------------------------------------
18 #include "exitcodes.h"
20 static char TIMESTR[26];
21 static time_t LAST = 0;
24 * Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level'
25 * is greater than the configured loglevel.
27 void InspIRCd::Log(int level, const char* text, ...)
29 /* sanity check, just in case */
30 if (!this->Config || !this->Logger)
33 /* Do this check again here so that we save pointless vsnprintf calls */
34 if ((level < Config->LogLevel) && !Config->forcedebug)
38 char textbuffer[65536];
40 va_start(argsPtr, text);
41 vsnprintf(textbuffer, 65536, text, argsPtr);
44 this->Log(level, std::string(textbuffer));
47 void InspIRCd::Log(int level, const std::string &text)
49 /* sanity check, just in case */
50 if (!this->Config || !this->Logger)
53 /* If we were given -debug we output all messages, regardless of configured loglevel */
54 if ((level < Config->LogLevel) && !Config->forcedebug)
59 time_t local = Time();
60 struct tm *timeinfo = localtime(&local);
62 strlcpy(TIMESTR,asctime(timeinfo),26);
67 if (Config->log_file && Config->writelog)
69 std::string out = std::string(TIMESTR) + " " + text.c_str() + "\n";
70 this->Logger->WriteLogLine(out);
75 printf("%s %s\n", TIMESTR, text.c_str());
79 std::string InspIRCd::GetServerDescription(const char* servername)
81 std::string description;
83 FOREACH_MOD_I(this,I_OnGetServerDescription,OnGetServerDescription(servername,description));
85 if (!description.empty())
91 // not a remote server that can be found, it must be me.
92 return Config->ServerDesc;
96 /* XXX - We don't use WriteMode for this because WriteMode is very slow and
97 * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
98 * the number of modes provided, e.g. if you send WriteMode 'og' to write to
99 * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
100 * uses the oper list, which means if you have 2000 users but only 5 opers,
101 * it iterates 5 times.
103 void InspIRCd::WriteOpers(const char* text, ...)
105 char textbuffer[MAXBUF];
108 va_start(argsPtr, text);
109 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
112 this->WriteOpers(std::string(textbuffer));
115 void InspIRCd::WriteOpers(const std::string &text)
117 for (std::list<User*>::iterator i = this->all_opers.begin(); i != this->all_opers.end(); i++)
120 if (IS_LOCAL(a) && a->IsModeSet('s'))
122 // send server notices to all with +s
123 a->WriteServ("NOTICE %s :%s",a->nick,text.c_str());
128 void InspIRCd::ServerNoticeAll(char* text, ...)
133 char textbuffer[MAXBUF];
134 char formatbuffer[MAXBUF];
136 va_start (argsPtr, text);
137 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
140 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s",Config->ServerName,textbuffer);
142 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
145 t->WriteServ(std::string(formatbuffer));
149 void InspIRCd::ServerPrivmsgAll(char* text, ...)
154 char textbuffer[MAXBUF];
155 char formatbuffer[MAXBUF];
157 va_start (argsPtr, text);
158 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
161 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
163 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
166 t->WriteServ(std::string(formatbuffer));
170 void InspIRCd::WriteMode(const char* modes, int flags, const char* text, ...)
172 char textbuffer[MAXBUF];
176 if (!text || !modes || !flags)
178 this->Log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
182 va_start(argsPtr, text);
183 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
185 modelen = strlen(modes);
189 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
192 bool send_to_user = true;
194 for (int n = 0; n < modelen; n++)
196 if (!t->IsModeSet(modes[n]))
198 send_to_user = false;
204 t->WriteServ("NOTICE %s :%s", t->nick, textbuffer);
208 else if (flags == WM_OR)
210 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
213 bool send_to_user = false;
215 for (int n = 0; n < modelen; n++)
217 if (t->IsModeSet(modes[n]))
226 t->WriteServ("NOTICE %s :%s", t->nick, textbuffer);
232 /* Find a user record by nickname and return a pointer to it */
233 User* InspIRCd::FindNick(const std::string &nick)
235 if (!nick.empty() && isdigit(*nick.begin()))
236 return FindUUID(nick);
238 user_hash::iterator iter = clientlist->find(nick);
240 if (iter == clientlist->end())
241 /* Couldn't find it */
247 User* InspIRCd::FindNick(const char* nick)
250 return FindUUID(nick);
252 user_hash::iterator iter = clientlist->find(nick);
254 if (iter == clientlist->end())
260 User* InspIRCd::FindNickOnly(const std::string &nick)
262 user_hash::iterator iter = clientlist->find(nick);
264 if (iter == clientlist->end())
270 User* InspIRCd::FindNickOnly(const char* nick)
272 user_hash::iterator iter = clientlist->find(nick);
274 if (iter == clientlist->end())
280 User *InspIRCd::FindUUID(const std::string &uid)
282 return FindUUID(uid.c_str());
285 User *InspIRCd::FindUUID(const char *uid)
287 user_hash::iterator finduuid = uuidlist->find(uid);
289 if (finduuid == uuidlist->end())
292 return finduuid->second;
295 /* find a channel record by channel name and return a pointer to it */
296 Channel* InspIRCd::FindChan(const char* chan)
298 chan_hash::iterator iter = chanlist->find(chan);
300 if (iter == chanlist->end())
301 /* Couldn't find it */
307 Channel* InspIRCd::FindChan(const std::string &chan)
309 chan_hash::iterator iter = chanlist->find(chan);
311 if (iter == chanlist->end())
312 /* Couldn't find it */
318 /* Send an error notice to all users, registered or not */
319 void InspIRCd::SendError(const std::string &s)
321 for (std::vector<User*>::const_iterator i = this->local_users.begin(); i != this->local_users.end(); i++)
323 if ((*i)->registered == REG_ALL)
325 (*i)->WriteServ("NOTICE %s :%s",(*i)->nick,s.c_str());
329 /* Unregistered connections receive ERROR, not a NOTICE */
330 (*i)->Write("ERROR :" + s);
332 /* This might generate a whole load of EAGAIN, but we dont really
333 * care about this, as if we call SendError something catastrophic
334 * has occured anyway, and we wont receive the events for these.
336 (*i)->FlushWriteBuf();
340 /* this function counts all users connected, wether they are registered or NOT. */
341 int InspIRCd::UserCount()
343 return clientlist->size();
346 /* this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state */
347 int InspIRCd::RegisteredUserCount()
349 return clientlist->size() - this->UnregisteredUserCount();
352 /* return how many users have a given mode e.g. 'a' */
353 int InspIRCd::ModeCount(const char mode)
355 ModeHandler* mh = this->Modes->FindMode(mode, MODETYPE_USER);
358 return mh->GetCount();
363 /* wrapper for readability */
364 int InspIRCd::InvisibleUserCount()
366 return ModeCount('i');
369 /* return how many users are opered */
370 int InspIRCd::OperCount()
372 return this->all_opers.size();
375 /* return how many users are unregistered */
376 int InspIRCd::UnregisteredUserCount()
378 return this->unregistered_count;
381 /* return channel count */
382 long InspIRCd::ChannelCount()
384 return chanlist->size();
387 /* return how many local registered users there are */
388 long InspIRCd::LocalUserCount()
390 /* Doesnt count unregistered clients */
391 return (local_users.size() - this->UnregisteredUserCount());
394 /* true for valid channel name, false else */
395 bool InspIRCd::IsChannel(const char *chname)
399 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
400 if (!chname || *chname != '#')
405 c = (char *)chname + 1;
419 /* too long a name - note funky pointer arithmetic here. */
420 if ((c - chname) > CHANMAX)
428 /* true for valid nickname, false else */
429 bool IsNickHandler::Call(const char* n)
435 for (char* i = (char*)n; *i; i++, p++)
437 if ((*i >= 'A') && (*i <= '}'))
439 /* "A"-"}" can occur anywhere in a nickname */
443 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
445 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
449 /* invalid character! abort */
453 /* too long? or not -- pointer arithmetic rocks */
454 return (p < NICKMAX - 1);
457 /* return true for good ident, false else */
458 bool IsIdentHandler::Call(const char* n)
463 for (char* i = (char*)n; *i; i++)
465 if ((*i >= 'A') && (*i <= '}'))
470 if (((*i >= '0') && (*i <= '9')) || (*i == '-') || (*i == '.'))
481 /* open the proper logfile */
482 bool InspIRCd::OpenLog(char** argv, int argc)
484 Config->MyDir = Config->GetFullProgDir();
486 if (!*this->LogFileName)
488 if (Config->logpath.empty())
490 Config->logpath = Config->MyDir + "/ircd.log";
493 Config->log_file = fopen(Config->logpath.c_str(),"a+");
497 Config->log_file = fopen(this->LogFileName,"a+");
500 if (!Config->log_file)
506 this->Logger = new FileLogger(this, Config->log_file);
510 void InspIRCd::CheckRoot()
514 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
515 this->Log(DEFAULT,"Cant start as root");
516 Exit(EXIT_STATUS_ROOT);
520 void InspIRCd::CheckDie()
522 if (*Config->DieValue)
524 printf("WARNING: %s\n\n",Config->DieValue);
525 this->Log(DEFAULT,"Died because of <die> tag: %s",Config->DieValue);
526 Exit(EXIT_STATUS_DIETAG);
530 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const std::string &text)
532 std::string copy_text = text;
535 FOREACH_RESULT_I(this, I_OnWhoisLine, OnWhoisLine(user, dest, numeric, copy_text));
538 user->WriteServ("%d %s", numeric, copy_text.c_str());
541 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const char* format, ...)
543 char textbuffer[MAXBUF];
545 va_start (argsPtr, format);
546 vsnprintf(textbuffer, MAXBUF, format, argsPtr);
549 this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));
552 /** Refactored by Brain, Jun 2007. Much faster with some clever O(1) array
553 * lookups and pointer maths.
555 long InspIRCd::Duration(const std::string &str)
557 unsigned char multiplier = 0;
562 /* Iterate each item in the string, looking for number or multiplier */
563 for (std::string::const_reverse_iterator i = str.rbegin(); i != str.rend(); ++i)
565 /* Found a number, queue it onto the current number */
566 if ((*i >= '0') && (*i <= '9'))
568 subtotal = subtotal + ((*i - '0') * times);
573 /* Found something thats not a number, find out how much
574 * it multiplies the built up number by, multiply the total
575 * and reset the built up number.
578 total += subtotal * duration_multi[multiplier];
580 /* Next subtotal please */
588 total += subtotal * duration_multi[multiplier];
591 /* Any trailing values built up are treated as raw seconds */
592 return total + subtotal;
595 bool InspIRCd::ULine(const char* server)
602 return (Config->ulines.find(server) != Config->ulines.end());
605 bool InspIRCd::SilentULine(const char* server)
607 std::map<irc::string,bool>::iterator n = Config->ulines.find(server);
608 if (n != Config->ulines.end())
613 std::string InspIRCd::TimeString(time_t curtime)
615 return std::string(ctime(&curtime),24);