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"
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 /* 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)
52 /* If we were given -debug we output all messages, regardless of configured loglevel */
53 if ((level < Config->LogLevel) && !Config->forcedebug)
58 time_t local = Time();
59 struct tm *timeinfo = localtime(&local);
61 strlcpy(TIMESTR,asctime(timeinfo),26);
66 if (Config->log_file && Config->writelog)
68 std::string out = std::string(TIMESTR) + " " + text.c_str() + "\n";
69 this->Logger->WriteLogLine(out);
74 printf("%s %s\n", TIMESTR, text.c_str());
78 std::string InspIRCd::GetServerDescription(const char* servername)
80 std::string description;
82 FOREACH_MOD_I(this,I_OnGetServerDescription,OnGetServerDescription(servername,description));
84 if (description != "")
90 // not a remote server that can be found, it must be me.
91 return Config->ServerDesc;
95 /* XXX - We don't use WriteMode for this because WriteMode is very slow and
96 * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
97 * the number of modes provided, e.g. if you send WriteMode 'og' to write to
98 * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
99 * uses the oper list, which means if you have 2000 users but only 5 opers,
100 * it iterates 5 times.
102 void InspIRCd::WriteOpers(const char* text, ...)
104 char textbuffer[MAXBUF];
107 va_start(argsPtr, text);
108 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
111 this->WriteOpers(std::string(textbuffer));
114 void InspIRCd::WriteOpers(const std::string &text)
116 for (std::vector<userrec*>::iterator i = this->all_opers.begin(); i != this->all_opers.end(); i++)
119 if (IS_LOCAL(a) && a->modes[UM_SERVERNOTICE])
121 // send server notices to all with +s
122 a->WriteServ("NOTICE %s :%s",a->nick,text.c_str());
127 void InspIRCd::ServerNoticeAll(char* text, ...)
132 char textbuffer[MAXBUF];
133 char formatbuffer[MAXBUF];
135 va_start (argsPtr, text);
136 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
139 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s",Config->ServerName,textbuffer);
141 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
144 t->WriteServ(std::string(formatbuffer));
148 void InspIRCd::ServerPrivmsgAll(char* text, ...)
153 char textbuffer[MAXBUF];
154 char formatbuffer[MAXBUF];
156 va_start (argsPtr, text);
157 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
160 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
162 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
165 t->WriteServ(std::string(formatbuffer));
169 void InspIRCd::WriteMode(const char* modes, int flags, const char* text, ...)
171 char textbuffer[MAXBUF];
175 if (!text || !modes || !flags)
177 this->Log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
181 va_start(argsPtr, text);
182 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
184 modelen = strlen(modes);
188 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
191 bool send_to_user = true;
193 for (int n = 0; n < modelen; n++)
195 if (!t->modes[modes[n]-65])
197 send_to_user = false;
202 t->WriteServ("NOTICE %s :%s",t->nick,textbuffer);
208 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
211 bool send_to_user = false;
213 for (int n = 0; n < modelen; n++)
215 if (t->modes[modes[n]-65])
222 t->WriteServ("NOTICE %s :%s",t->nick,textbuffer);
227 /* Find a user record by nickname and return a pointer to it */
229 userrec* InspIRCd::FindNick(const std::string &nick)
231 user_hash::iterator iter = clientlist->find(nick);
233 if (iter == clientlist->end())
234 /* Couldn't find it */
240 userrec* InspIRCd::FindNick(const char* nick)
242 user_hash::iterator iter = clientlist->find(nick);
244 if (iter == clientlist->end())
250 /* find a channel record by channel name and return a pointer to it */
252 chanrec* InspIRCd::FindChan(const char* chan)
254 chan_hash::iterator iter = chanlist->find(chan);
256 if (iter == chanlist->end())
257 /* Couldn't find it */
263 chanrec* InspIRCd::FindChan(const std::string &chan)
265 chan_hash::iterator iter = chanlist->find(chan);
267 if (iter == chanlist->end())
268 /* Couldn't find it */
275 * sends out an error notice to all connected clients (not to be used
278 void InspIRCd::SendError(const std::string &s)
280 for (std::vector<userrec*>::const_iterator i = this->local_users.begin(); i != this->local_users.end(); i++)
282 if ((*i)->registered == REG_ALL)
284 (*i)->WriteServ("NOTICE %s :%s",(*i)->nick,s.c_str());
288 /* Unregistered connections receive ERROR, not a NOTICE */
289 (*i)->Write("ERROR :" + s);
291 /* This might generate a whole load of EAGAIN, but we dont really
292 * care about this, as if we call SendError something catastrophic
293 * has occured anyway, and we wont receive the events for these.
295 (*i)->FlushWriteBuf();
299 // this function counts all users connected, wether they are registered or NOT.
300 int InspIRCd::UserCount()
302 return clientlist->size();
305 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
306 int InspIRCd::RegisteredUserCount()
308 return clientlist->size() - this->UnregisteredUserCount();
311 int InspIRCd::ModeCount(const char mode)
313 ModeHandler* mh = this->Modes->FindMode(mode, MODETYPE_USER);
316 return mh->GetCount();
321 int InspIRCd::InvisibleUserCount()
323 return ModeCount('i');
326 int InspIRCd::OperCount()
328 return this->all_opers.size();
331 int InspIRCd::UnregisteredUserCount()
333 return this->unregistered_count;
336 long InspIRCd::ChannelCount()
338 return chanlist->size();
341 long InspIRCd::LocalUserCount()
343 /* Doesnt count unregistered clients */
344 return (local_users.size() - this->UnregisteredUserCount());
347 bool InspIRCd::IsChannel(const char *chname)
351 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
352 if (!chname || *chname != '#')
357 c = (char *)chname + 1;
371 /* too long a name - note funky pointer arithmetic here. */
372 if ((c - chname) > CHANMAX)
380 bool InspIRCd::IsNick(const char* n)
386 for (char* i = (char*)n; *i; i++, p++)
388 if ((*i >= 'A') && (*i <= '}'))
390 /* "A"-"}" can occur anywhere in a nickname */
394 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
396 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
400 /* invalid character! abort */
404 /* too long? or not -- pointer arithmetic rocks */
405 return (p < NICKMAX - 1);
408 void InspIRCd::OpenLog(char** argv, int argc)
410 Config->MyDir = ServerConfig::GetFullProgDir(argv,argc);
412 if (!*this->LogFileName)
414 if (Config->logpath == "")
416 Config->logpath = Config->MyDir + "/ircd.log";
419 Config->log_file = fopen(Config->logpath.c_str(),"a+");
423 Config->log_file = fopen(this->LogFileName,"a+");
426 if (!Config->log_file)
428 printf("ERROR: Could not write to logfile %s: %s\n\n", Config->logpath.c_str(), strerror(errno));
429 Exit(EXIT_STATUS_LOG);
432 this->Logger = new FileLogger(this, Config->log_file);
435 void InspIRCd::CheckRoot()
439 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
440 this->Log(DEFAULT,"Cant start as root");
441 Exit(EXIT_STATUS_ROOT);
445 void InspIRCd::CheckDie()
447 if (*Config->DieValue)
449 printf("WARNING: %s\n\n",Config->DieValue);
450 this->Log(DEFAULT,"Died because of <die> tag: %s",Config->DieValue);
451 Exit(EXIT_STATUS_DIETAG);
455 /* We must load the modules AFTER initializing the socket engine, now */
456 void InspIRCd::LoadAllModules()
458 char configToken[MAXBUF];
459 Config->module_names.clear();
462 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "module"); count++)
464 Config->ConfValue(Config->config_data, "module","name",count,configToken,MAXBUF);
465 printf("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
467 if (!this->LoadModule(configToken))
469 this->Log(DEFAULT,"There was an error loading a module: %s", this->ModuleError());
470 printf("\nThere was an error loading a module: %s\n\n",this->ModuleError());
471 Exit(EXIT_STATUS_MODULE);
474 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");
475 this->Log(DEFAULT,"Total loaded modules: %d", this->ModCount+1);
478 void InspIRCd::SendWhoisLine(userrec* user, userrec* dest, int numeric, const std::string &text)
480 std::string copy_text = text;
483 FOREACH_RESULT_I(this, I_OnWhoisLine, OnWhoisLine(user, dest, numeric, copy_text));
486 user->WriteServ("%d %s", numeric, copy_text.c_str());
489 void InspIRCd::SendWhoisLine(userrec* user, userrec* dest, int numeric, const char* format, ...)
491 char textbuffer[MAXBUF];
493 va_start (argsPtr, format);
494 vsnprintf(textbuffer, MAXBUF, format, argsPtr);
497 this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));