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 * ---------------------------------------------------
15 #include "configreader.h"
23 static char TIMESTR[26];
24 static time_t LAST = 0;
27 * Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level'
28 * is greater than the configured loglevel.
30 void InspIRCd::Log(int level, const char* text, ...)
32 /* Do this check again here so that we save pointless vsnprintf calls */
33 if ((level < Config->LogLevel) && !Config->forcedebug)
37 char textbuffer[65536];
39 va_start(argsPtr, text);
40 vsnprintf(textbuffer, 65536, text, argsPtr);
43 this->Log(level, std::string(textbuffer));
46 void InspIRCd::Log(int level, const std::string &text)
51 /* If we were given -debug we output all messages, regardless of configured loglevel */
52 if ((level < Config->LogLevel) && !Config->forcedebug)
57 time_t local = Time();
58 struct tm *timeinfo = localtime(&local);
60 strlcpy(TIMESTR,asctime(timeinfo),26);
65 if (Config->log_file && Config->writelog)
67 std::string out = std::string(TIMESTR) + " " + text.c_str() + "\n";
68 this->Logger->WriteLogLine(out);
73 printf("%s %s\n", TIMESTR, text.c_str());
77 std::string InspIRCd::GetServerDescription(const char* servername)
79 std::string description = "";
81 FOREACH_MOD_I(this,I_OnGetServerDescription,OnGetServerDescription(servername,description));
83 if (description != "")
89 // not a remote server that can be found, it must be me.
90 return Config->ServerDesc;
94 /* XXX - We don't use WriteMode for this because WriteMode is very slow and
95 * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
96 * the number of modes provided, e.g. if you send WriteMode 'og' to write to
97 * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
98 * uses the oper list, which means if you have 2000 users but only 5 opers,
99 * it iterates 5 times.
101 void InspIRCd::WriteOpers(const char* text, ...)
103 char textbuffer[MAXBUF];
106 va_start(argsPtr, text);
107 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
110 this->WriteOpers(std::string(textbuffer));
113 void InspIRCd::WriteOpers(const std::string &text)
115 for (std::vector<userrec*>::iterator i = this->all_opers.begin(); i != this->all_opers.end(); i++)
118 if (IS_LOCAL(a) && a->modes[UM_SERVERNOTICE])
120 // send server notices to all with +s
121 a->WriteServ("NOTICE %s :%s",a->nick,text.c_str());
126 void InspIRCd::ServerNoticeAll(char* text, ...)
131 char textbuffer[MAXBUF];
132 char formatbuffer[MAXBUF];
134 va_start (argsPtr, text);
135 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
138 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s",Config->ServerName,textbuffer);
140 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
143 t->WriteServ(std::string(formatbuffer));
147 void InspIRCd::ServerPrivmsgAll(char* text, ...)
152 char textbuffer[MAXBUF];
153 char formatbuffer[MAXBUF];
155 va_start (argsPtr, text);
156 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
159 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
161 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
164 t->WriteServ(std::string(formatbuffer));
168 void InspIRCd::WriteMode(const char* modes, int flags, const char* text, ...)
170 char textbuffer[MAXBUF];
174 if (!text || !modes || !flags)
176 this->Log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
180 va_start(argsPtr, text);
181 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
183 modelen = strlen(modes);
187 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
190 bool send_to_user = true;
192 for (int n = 0; n < modelen; n++)
194 if (!t->modes[modes[n]-65])
196 send_to_user = false;
201 t->WriteServ("NOTICE %s :%s",t->nick,textbuffer);
207 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
210 bool send_to_user = false;
212 for (int n = 0; n < modelen; n++)
214 if (t->modes[modes[n]-65])
221 t->WriteServ("NOTICE %s :%s",t->nick,textbuffer);
226 /* Find a user record by nickname and return a pointer to it */
228 userrec* InspIRCd::FindNick(const std::string &nick)
230 user_hash::iterator iter = clientlist.find(nick);
232 if (iter == clientlist.end())
233 /* Couldn't find it */
239 userrec* InspIRCd::FindNick(const char* nick)
241 user_hash::iterator iter;
246 iter = clientlist.find(nick);
248 if (iter == clientlist.end())
254 /* find a channel record by channel name and return a pointer to it */
256 chanrec* InspIRCd::FindChan(const char* chan)
258 chan_hash::iterator iter;
263 iter = chanlist.find(chan);
265 if (iter == chanlist.end())
266 /* Couldn't find it */
272 chanrec* InspIRCd::FindChan(const std::string &chan)
274 chan_hash::iterator iter = chanlist.find(chan);
276 if (iter == chanlist.end())
277 /* Couldn't find it */
285 * sends out an error notice to all connected clients (not to be used
288 void InspIRCd::SendError(const char *s)
290 for (std::vector<userrec*>::const_iterator i = this->local_users.begin(); i != this->local_users.end(); i++)
292 userrec* t = (userrec*)(*i);
293 if (t->registered == REG_ALL)
295 t->WriteServ("NOTICE %s :%s",t->nick,s);
299 // fix - unregistered connections receive ERROR, not NOTICE
300 t->Write("ERROR :%s",s);
305 // this function counts all users connected, wether they are registered or NOT.
306 int InspIRCd::UserCount()
308 return clientlist.size();
311 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
312 int InspIRCd::RegisteredUserCount()
316 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
318 c += (i->second->registered == REG_ALL);
324 int InspIRCd::InvisibleUserCount()
328 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
330 c += ((i->second->registered == REG_ALL) && (i->second->modes[UM_INVISIBLE]));
336 int InspIRCd::OperCount()
340 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
342 if (*(i->second->oper))
348 int InspIRCd::UnregisteredUserCount()
352 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
354 userrec* t = (userrec*)(*i);
355 if (t->registered != REG_ALL)
362 long InspIRCd::ChannelCount()
364 return chanlist.size();
367 long InspIRCd::LocalUserCount()
371 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
373 userrec* t = (userrec*)(*i);
374 if (t->registered == REG_ALL)
381 bool InspIRCd::IsChannel(const char *chname)
385 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
386 if (!chname || *chname != '#')
391 c = (char *)chname + 1;
405 /* too long a name - note funky pointer arithmetic here. */
406 if ((c - chname) > CHANMAX)
414 bool InspIRCd::IsNick(const char* n)
420 for (char* i = (char*)n; *i; i++, p++)
422 if ((*i >= 'A') && (*i <= '}'))
424 /* "A"-"}" can occur anywhere in a nickname */
428 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
430 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
434 /* invalid character! abort */
438 /* too long? or not -- pointer arithmetic rocks */
439 return (p < NICKMAX - 1);
442 void InspIRCd::OpenLog(char** argv, int argc)
444 if (!*this->LogFileName)
446 if (Config->logpath == "")
448 Config->logpath = ServerConfig::GetFullProgDir(argv,argc) + "/ircd.log";
451 Config->log_file = fopen(Config->logpath.c_str(),"a+");
455 Config->log_file = fopen(this->LogFileName,"a+");
458 if (!Config->log_file)
460 printf("ERROR: Could not write to logfile %s: %s\n\n", Config->logpath.c_str(), strerror(errno));
464 this->Logger = new FileLogger(this, Config->log_file);
467 void InspIRCd::CheckRoot()
471 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
472 this->Log(DEFAULT,"Cant start as root");
477 void InspIRCd::CheckDie()
479 if (*Config->DieValue)
481 printf("WARNING: %s\n\n",Config->DieValue);
482 this->Log(DEFAULT,"Died because of <die> tag: %s",Config->DieValue);
487 /* We must load the modules AFTER initializing the socket engine, now */
488 void InspIRCd::LoadAllModules()
490 char configToken[MAXBUF];
491 Config->module_names.clear();
494 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "module"); count++)
496 Config->ConfValue(Config->config_data, "module","name",count,configToken,MAXBUF);
497 printf("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
499 if (!this->LoadModule(configToken))
501 this->Log(DEFAULT,"There was an error loading a module: %s", this->ModuleError());
502 printf("\nThere was an error loading a module: %s\n\n",this->ModuleError());
506 printf("\nA total of \033[1;32m%d\033[0m module%s been loaded.\n", this->ModCount+1, this->ModCount+1 == 1 ? " has" : "s have");
507 this->Log(DEFAULT,"Total loaded modules: %d", this->ModCount+1);
510 void InspIRCd::SendWhoisLine(userrec* user, userrec* dest, int numeric, const std::string &text)
512 std::string copy_text = text;
515 FOREACH_RESULT_I(this, I_OnWhoisLine, OnWhoisLine(user, dest, numeric, copy_text));
518 user->WriteServ("%d %s", numeric, copy_text.c_str());
521 void InspIRCd::SendWhoisLine(userrec* user, userrec* dest, int numeric, const char* format, ...)
523 char textbuffer[MAXBUF];
525 va_start (argsPtr, format);
526 vsnprintf(textbuffer, MAXBUF, format, argsPtr);
529 this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));