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 * ---------------------------------------------------
14 /* $Core: libIRCDhelper */
20 #include "exitcodes.h"
22 static char TIMESTR[26];
23 static time_t LAST = 0;
26 * Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level'
27 * is greater than the configured loglevel.
29 void InspIRCd::Log(int level, const char* text, ...)
31 /* sanity check, just in case */
32 if (!this->Config || !this->Logger)
35 /* Do this check again here so that we save pointless vsnprintf calls */
36 if ((level < Config->LogLevel) && !Config->forcedebug)
40 char textbuffer[65536];
42 va_start(argsPtr, text);
43 vsnprintf(textbuffer, 65536, text, argsPtr);
46 this->Log(level, std::string(textbuffer));
49 void InspIRCd::Log(int level, const std::string &text)
51 /* sanity check, just in case */
52 if (!this->Config || !this->Logger)
55 /* If we were given -debug we output all messages, regardless of configured loglevel */
56 if ((level < Config->LogLevel) && !Config->forcedebug)
61 time_t local = Time();
62 struct tm *timeinfo = localtime(&local);
64 strlcpy(TIMESTR,asctime(timeinfo),26);
69 if (Config->log_file && Config->writelog)
71 std::string out = std::string(TIMESTR) + " " + text.c_str() + "\n";
72 this->Logger->WriteLogLine(out);
77 printf("%s %s\n", TIMESTR, text.c_str());
81 std::string InspIRCd::GetServerDescription(const char* servername)
83 std::string description;
85 FOREACH_MOD_I(this,I_OnGetServerDescription,OnGetServerDescription(servername,description));
87 if (!description.empty())
93 // not a remote server that can be found, it must be me.
94 return Config->ServerDesc;
98 /* XXX - We don't use WriteMode for this because WriteMode is very slow and
99 * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
100 * the number of modes provided, e.g. if you send WriteMode 'og' to write to
101 * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
102 * uses the oper list, which means if you have 2000 users but only 5 opers,
103 * it iterates 5 times.
105 void InspIRCd::WriteOpers(const char* text, ...)
107 char textbuffer[MAXBUF];
110 va_start(argsPtr, text);
111 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
114 this->WriteOpers(std::string(textbuffer));
117 void InspIRCd::WriteOpers(const std::string &text)
119 for (std::list<User*>::iterator i = this->all_opers.begin(); i != this->all_opers.end(); i++)
122 if (IS_LOCAL(a) && a->IsModeSet('s'))
124 // send server notices to all with +s
125 a->WriteServ("NOTICE %s :%s",a->nick,text.c_str());
130 void InspIRCd::ServerNoticeAll(char* text, ...)
135 char textbuffer[MAXBUF];
136 char formatbuffer[MAXBUF];
138 va_start (argsPtr, text);
139 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
142 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s",Config->ServerName,textbuffer);
144 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
147 t->WriteServ(std::string(formatbuffer));
151 void InspIRCd::ServerPrivmsgAll(char* text, ...)
156 char textbuffer[MAXBUF];
157 char formatbuffer[MAXBUF];
159 va_start (argsPtr, text);
160 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
163 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
165 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
168 t->WriteServ(std::string(formatbuffer));
172 void InspIRCd::WriteMode(const char* modes, int flags, const char* text, ...)
174 char textbuffer[MAXBUF];
178 if (!text || !modes || !flags)
180 this->Log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
184 va_start(argsPtr, text);
185 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
187 modelen = strlen(modes);
191 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
194 bool send_to_user = true;
196 for (int n = 0; n < modelen; n++)
198 if (!t->IsModeSet(modes[n]))
200 send_to_user = false;
206 t->WriteServ("NOTICE %s :%s", t->nick, textbuffer);
210 else if (flags == WM_OR)
212 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
215 bool send_to_user = false;
217 for (int n = 0; n < modelen; n++)
219 if (t->IsModeSet(modes[n]))
228 t->WriteServ("NOTICE %s :%s", t->nick, textbuffer);
234 /* Find a user record by nickname and return a pointer to it */
235 User* InspIRCd::FindNick(const std::string &nick)
237 if (!nick.empty() && isdigit(*nick.begin()))
238 return FindUUID(nick);
240 user_hash::iterator iter = clientlist->find(nick);
242 if (iter == clientlist->end())
243 /* Couldn't find it */
249 User* InspIRCd::FindNick(const char* nick)
252 return FindUUID(nick);
254 user_hash::iterator iter = clientlist->find(nick);
256 if (iter == clientlist->end())
262 User* InspIRCd::FindNickOnly(const std::string &nick)
264 user_hash::iterator iter = clientlist->find(nick);
266 if (iter == clientlist->end())
272 User* InspIRCd::FindNickOnly(const char* nick)
274 user_hash::iterator iter = clientlist->find(nick);
276 if (iter == clientlist->end())
282 User *InspIRCd::FindUUID(const std::string &uid)
284 return FindUUID(uid.c_str());
287 User *InspIRCd::FindUUID(const char *uid)
289 user_hash::iterator finduuid = uuidlist->find(uid);
291 if (finduuid == uuidlist->end())
294 return finduuid->second;
297 /* find a channel record by channel name and return a pointer to it */
298 Channel* InspIRCd::FindChan(const char* chan)
300 chan_hash::iterator iter = chanlist->find(chan);
302 if (iter == chanlist->end())
303 /* Couldn't find it */
309 Channel* InspIRCd::FindChan(const std::string &chan)
311 chan_hash::iterator iter = chanlist->find(chan);
313 if (iter == chanlist->end())
314 /* Couldn't find it */
320 /* Send an error notice to all users, registered or not */
321 void InspIRCd::SendError(const std::string &s)
323 for (std::vector<User*>::const_iterator i = this->local_users.begin(); i != this->local_users.end(); i++)
325 if ((*i)->registered == REG_ALL)
327 (*i)->WriteServ("NOTICE %s :%s",(*i)->nick,s.c_str());
331 /* Unregistered connections receive ERROR, not a NOTICE */
332 (*i)->Write("ERROR :" + s);
334 /* This might generate a whole load of EAGAIN, but we dont really
335 * care about this, as if we call SendError something catastrophic
336 * has occured anyway, and we wont receive the events for these.
338 (*i)->FlushWriteBuf();
342 /* this function counts all users connected, wether they are registered or NOT. */
343 int InspIRCd::UserCount()
345 return clientlist->size();
348 /* this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state */
349 int InspIRCd::RegisteredUserCount()
351 return clientlist->size() - this->UnregisteredUserCount();
354 /* return how many users have a given mode e.g. 'a' */
355 int InspIRCd::ModeCount(const char mode)
357 ModeHandler* mh = this->Modes->FindMode(mode, MODETYPE_USER);
360 return mh->GetCount();
365 /* return how many users are opered */
366 int InspIRCd::OperCount()
368 return this->all_opers.size();
371 /* return how many users are unregistered */
372 int InspIRCd::UnregisteredUserCount()
374 return this->unregistered_count;
377 /* return channel count */
378 long InspIRCd::ChannelCount()
380 return chanlist->size();
383 /* return how many local registered users there are */
384 long InspIRCd::LocalUserCount()
386 /* Doesnt count unregistered clients */
387 return (local_users.size() - this->UnregisteredUserCount());
390 bool InspIRCd::IsValidMask(const std::string &mask)
392 char* dest = (char*)mask.c_str();
396 for (char* i = dest; *i; i++)
398 /* out of range character, bad mask */
399 if (*i < 32 || *i > 126)
415 /* valid masks only have 1 ! and @ */
416 if (exclamation != 1 || atsign != 1)
422 /* true for valid channel name, false else */
423 bool InspIRCd::IsChannel(const char *chname)
427 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
428 if (!chname || *chname != '#')
433 c = (char *)chname + 1;
447 /* too long a name - note funky pointer arithmetic here. */
448 if ((c - chname) > CHANMAX)
456 /* true for valid nickname, false else */
457 bool IsNickHandler::Call(const char* n)
463 for (char* i = (char*)n; *i; i++, p++)
465 if ((*i >= 'A') && (*i <= '}'))
467 /* "A"-"}" can occur anywhere in a nickname */
471 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
473 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
477 /* invalid character! abort */
481 /* too long? or not -- pointer arithmetic rocks */
482 return (p < NICKMAX - 1);
485 /* return true for good ident, false else */
486 bool IsIdentHandler::Call(const char* n)
491 for (char* i = (char*)n; *i; i++)
493 if ((*i >= 'A') && (*i <= '}'))
498 if (((*i >= '0') && (*i <= '9')) || (*i == '-') || (*i == '.'))
509 /* open the proper logfile */
510 bool InspIRCd::OpenLog(char**, int)
512 Config->MyDir = Config->GetFullProgDir();
514 if (!*this->LogFileName)
516 if (Config->logpath.empty())
518 Config->logpath = Config->MyDir + "/ircd.log";
521 Config->log_file = fopen(Config->logpath.c_str(),"a+");
525 Config->log_file = fopen(this->LogFileName,"a+");
528 if (!Config->log_file)
534 this->Logger = new FileLogger(this, Config->log_file);
538 void InspIRCd::CheckRoot()
542 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
543 this->Log(DEFAULT,"Cant start as root");
544 Exit(EXIT_STATUS_ROOT);
548 void InspIRCd::CheckDie()
550 if (*Config->DieValue)
552 printf("WARNING: %s\n\n",Config->DieValue);
553 this->Log(DEFAULT,"Died because of <die> tag: %s",Config->DieValue);
554 Exit(EXIT_STATUS_DIETAG);
558 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const std::string &text)
560 std::string copy_text = text;
563 FOREACH_RESULT_I(this, I_OnWhoisLine, OnWhoisLine(user, dest, numeric, copy_text));
566 user->WriteServ("%d %s", numeric, copy_text.c_str());
569 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const char* format, ...)
571 char textbuffer[MAXBUF];
573 va_start (argsPtr, format);
574 vsnprintf(textbuffer, MAXBUF, format, argsPtr);
577 this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));
580 /** Refactored by Brain, Jun 2007. Much faster with some clever O(1) array
581 * lookups and pointer maths.
583 long InspIRCd::Duration(const std::string &str)
585 unsigned char multiplier = 0;
590 /* Iterate each item in the string, looking for number or multiplier */
591 for (std::string::const_reverse_iterator i = str.rbegin(); i != str.rend(); ++i)
593 /* Found a number, queue it onto the current number */
594 if ((*i >= '0') && (*i <= '9'))
596 subtotal = subtotal + ((*i - '0') * times);
601 /* Found something thats not a number, find out how much
602 * it multiplies the built up number by, multiply the total
603 * and reset the built up number.
606 total += subtotal * duration_multi[multiplier];
608 /* Next subtotal please */
616 total += subtotal * duration_multi[multiplier];
619 /* Any trailing values built up are treated as raw seconds */
620 return total + subtotal;
623 bool InspIRCd::ULine(const char* server)
630 return (Config->ulines.find(server) != Config->ulines.end());
633 bool InspIRCd::SilentULine(const char* server)
635 std::map<irc::string,bool>::iterator n = Config->ulines.find(server);
636 if (n != Config->ulines.end())
641 std::string InspIRCd::TimeString(time_t curtime)
643 return std::string(ctime(&curtime),24);