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 * ---------------------------------------------------
16 #include "configreader.h"
22 #include "exitcodes.h"
24 static char TIMESTR[26];
25 static time_t LAST = 0;
28 * Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level'
29 * is greater than the configured loglevel.
31 void InspIRCd::Log(int level, const char* text, ...)
33 /* sanity check, just in case */
34 if (!this->Config || !this->Logger)
37 /* Do this check again here so that we save pointless vsnprintf calls */
38 if ((level < Config->LogLevel) && !Config->forcedebug)
42 char textbuffer[65536];
44 va_start(argsPtr, text);
45 vsnprintf(textbuffer, 65536, text, argsPtr);
48 this->Log(level, std::string(textbuffer));
51 void InspIRCd::Log(int level, const std::string &text)
53 /* sanity check, just in case */
54 if (!this->Config || !this->Logger)
57 /* If we were given -debug we output all messages, regardless of configured loglevel */
58 if ((level < Config->LogLevel) && !Config->forcedebug)
63 time_t local = Time();
64 struct tm *timeinfo = localtime(&local);
66 strlcpy(TIMESTR,asctime(timeinfo),26);
71 if (Config->log_file && Config->writelog)
73 std::string out = std::string(TIMESTR) + " " + text.c_str() + "\n";
74 this->Logger->WriteLogLine(out);
79 printf("%s %s\n", TIMESTR, text.c_str());
83 std::string InspIRCd::GetServerDescription(const char* servername)
85 std::string description;
87 FOREACH_MOD_I(this,I_OnGetServerDescription,OnGetServerDescription(servername,description));
89 if (!description.empty())
95 // not a remote server that can be found, it must be me.
96 return Config->ServerDesc;
100 /* XXX - We don't use WriteMode for this because WriteMode is very slow and
101 * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
102 * the number of modes provided, e.g. if you send WriteMode 'og' to write to
103 * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
104 * uses the oper list, which means if you have 2000 users but only 5 opers,
105 * it iterates 5 times.
107 void InspIRCd::WriteOpers(const char* text, ...)
109 char textbuffer[MAXBUF];
112 va_start(argsPtr, text);
113 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
116 this->WriteOpers(std::string(textbuffer));
119 void InspIRCd::WriteOpers(const std::string &text)
121 for (std::vector<userrec*>::iterator i = this->all_opers.begin(); i != this->all_opers.end(); i++)
124 if (IS_LOCAL(a) && a->IsModeSet('s'))
126 // send server notices to all with +s
127 a->WriteServ("NOTICE %s :%s",a->nick,text.c_str());
132 void InspIRCd::ServerNoticeAll(char* text, ...)
137 char textbuffer[MAXBUF];
138 char formatbuffer[MAXBUF];
140 va_start (argsPtr, text);
141 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
144 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s",Config->ServerName,textbuffer);
146 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
149 t->WriteServ(std::string(formatbuffer));
153 void InspIRCd::ServerPrivmsgAll(char* text, ...)
158 char textbuffer[MAXBUF];
159 char formatbuffer[MAXBUF];
161 va_start (argsPtr, text);
162 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
165 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
167 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
170 t->WriteServ(std::string(formatbuffer));
174 void InspIRCd::WriteMode(const char* modes, int flags, const char* text, ...)
176 char textbuffer[MAXBUF];
180 if (!text || !modes || !flags)
182 this->Log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
186 va_start(argsPtr, text);
187 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
189 modelen = strlen(modes);
193 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
196 bool send_to_user = true;
198 for (int n = 0; n < modelen; n++)
200 if (!t->IsModeSet(modes[n]))
202 send_to_user = false;
208 t->WriteServ("NOTICE %s :%s", t->nick, textbuffer);
212 else if (flags == WM_OR)
214 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
217 bool send_to_user = false;
219 for (int n = 0; n < modelen; n++)
221 if (t->IsModeSet(modes[n]))
230 t->WriteServ("NOTICE %s :%s", t->nick, textbuffer);
236 /* Find a user record by nickname and return a pointer to it */
237 userrec* InspIRCd::FindNick(const std::string &nick)
239 if (!nick.empty() && isdigit(*nick.begin()))
240 return FindUUID(nick);
242 user_hash::iterator iter = clientlist->find(nick);
244 if (iter == clientlist->end())
245 /* Couldn't find it */
251 userrec* InspIRCd::FindNick(const char* nick)
254 return FindUUID(nick);
256 user_hash::iterator iter = clientlist->find(nick);
258 if (iter == clientlist->end())
264 userrec* InspIRCd::FindNickOnly(const std::string &nick)
266 user_hash::iterator iter = clientlist->find(nick);
268 if (iter == clientlist->end())
274 userrec* InspIRCd::FindNickOnly(const char* nick)
276 user_hash::iterator iter = clientlist->find(nick);
278 if (iter == clientlist->end())
284 userrec *InspIRCd::FindUUID(const std::string &uid)
286 return FindUUID(uid.c_str());
289 userrec *InspIRCd::FindUUID(const char *uid)
291 user_hash::iterator finduuid = uuidlist->find(uid);
293 if (finduuid == uuidlist->end())
296 return finduuid->second;
299 /* find a channel record by channel name and return a pointer to it */
300 chanrec* InspIRCd::FindChan(const char* chan)
302 chan_hash::iterator iter = chanlist->find(chan);
304 if (iter == chanlist->end())
305 /* Couldn't find it */
311 chanrec* InspIRCd::FindChan(const std::string &chan)
313 chan_hash::iterator iter = chanlist->find(chan);
315 if (iter == chanlist->end())
316 /* Couldn't find it */
322 /* Send an error notice to all users, registered or not */
323 void InspIRCd::SendError(const std::string &s)
325 for (std::vector<userrec*>::const_iterator i = this->local_users.begin(); i != this->local_users.end(); i++)
327 if ((*i)->registered == REG_ALL)
329 (*i)->WriteServ("NOTICE %s :%s",(*i)->nick,s.c_str());
333 /* Unregistered connections receive ERROR, not a NOTICE */
334 (*i)->Write("ERROR :" + s);
336 /* This might generate a whole load of EAGAIN, but we dont really
337 * care about this, as if we call SendError something catastrophic
338 * has occured anyway, and we wont receive the events for these.
340 (*i)->FlushWriteBuf();
344 /* this function counts all users connected, wether they are registered or NOT. */
345 int InspIRCd::UserCount()
347 return clientlist->size();
350 /* this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state */
351 int InspIRCd::RegisteredUserCount()
353 return clientlist->size() - this->UnregisteredUserCount();
356 /* return how many users have a given mode e.g. 'a' */
357 int InspIRCd::ModeCount(const char mode)
359 ModeHandler* mh = this->Modes->FindMode(mode, MODETYPE_USER);
362 return mh->GetCount();
367 /* wrapper for readability */
368 int InspIRCd::InvisibleUserCount()
370 return ModeCount('i');
373 /* return how many users are opered */
374 int InspIRCd::OperCount()
376 return this->all_opers.size();
379 /* return how many users are unregistered */
380 int InspIRCd::UnregisteredUserCount()
382 return this->unregistered_count;
385 /* return channel count */
386 long InspIRCd::ChannelCount()
388 return chanlist->size();
391 /* return how many local registered users there are */
392 long InspIRCd::LocalUserCount()
394 /* Doesnt count unregistered clients */
395 return (local_users.size() - this->UnregisteredUserCount());
398 /* true for valid channel name, false else */
399 bool InspIRCd::IsChannel(const char *chname)
403 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
404 if (!chname || *chname != '#')
409 c = (char *)chname + 1;
423 /* too long a name - note funky pointer arithmetic here. */
424 if ((c - chname) > CHANMAX)
432 /* true for valid nickname, false else */
433 bool IsNickHandler::Call(const char* n)
439 for (char* i = (char*)n; *i; i++, p++)
441 if ((*i >= 'A') && (*i <= '}'))
443 /* "A"-"}" can occur anywhere in a nickname */
447 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
449 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
453 /* invalid character! abort */
457 /* too long? or not -- pointer arithmetic rocks */
458 return (p < NICKMAX - 1);
461 /* return true for good ident, false else */
462 bool IsIdentHandler::Call(const char* n)
467 for (char* i = (char*)n; *i; i++)
469 if ((*i >= 'A') && (*i <= '}'))
474 if (((*i >= '0') && (*i <= '9')) || (*i == '-') || (*i == '.'))
485 /* open the proper logfile */
486 bool InspIRCd::OpenLog(char** argv, int argc)
488 Config->MyDir = Config->GetFullProgDir();
490 if (!*this->LogFileName)
492 if (Config->logpath.empty())
494 Config->logpath = Config->MyDir + "/ircd.log";
497 Config->log_file = fopen(Config->logpath.c_str(),"a+");
501 Config->log_file = fopen(this->LogFileName,"a+");
504 if (!Config->log_file)
510 this->Logger = new FileLogger(this, Config->log_file);
514 void InspIRCd::CheckRoot()
518 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
519 this->Log(DEFAULT,"Cant start as root");
520 Exit(EXIT_STATUS_ROOT);
524 void InspIRCd::CheckDie()
526 if (*Config->DieValue)
528 printf("WARNING: %s\n\n",Config->DieValue);
529 this->Log(DEFAULT,"Died because of <die> tag: %s",Config->DieValue);
530 Exit(EXIT_STATUS_DIETAG);
534 /* We must load the modules AFTER initializing the socket engine, now */
535 void InspIRCd::LoadAllModules()
537 char configToken[MAXBUF];
538 Config->module_names.clear();
541 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "module"); count++)
543 Config->ConfValue(Config->config_data, "module", "name", count, configToken, MAXBUF);
544 printf_c("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
546 if (!this->LoadModule(configToken))
548 this->Log(DEFAULT,"There was an error loading the module '%s': %s", configToken, this->ModuleError());
549 printf_c("\n[\033[1;31m*\033[0m] There was an error loading the module '%s': %s\n\n", configToken, this->ModuleError());
550 Exit(EXIT_STATUS_MODULE);
553 printf_c("\nA total of \033[1;32m%d\033[0m module%s been loaded.\n", this->ModCount+1, this->ModCount+1 == 1 ? " has" : "s have");
554 this->Log(DEFAULT,"Total loaded modules: %d", this->ModCount+1);
557 void InspIRCd::SendWhoisLine(userrec* user, userrec* dest, int numeric, const std::string &text)
559 std::string copy_text = text;
562 FOREACH_RESULT_I(this, I_OnWhoisLine, OnWhoisLine(user, dest, numeric, copy_text));
565 user->WriteServ("%d %s", numeric, copy_text.c_str());
568 void InspIRCd::SendWhoisLine(userrec* user, userrec* dest, int numeric, const char* format, ...)
570 char textbuffer[MAXBUF];
572 va_start (argsPtr, format);
573 vsnprintf(textbuffer, MAXBUF, format, argsPtr);
576 this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));