1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
18 #include "inspircd_config.h"
19 #include "configreader.h"
22 #include <sys/errno.h>
30 #include "connection.h"
40 #include "inspstring.h"
41 #include "helperfuncs.h"
48 static char TIMESTR[26];
49 static time_t LAST = 0;
52 * Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level'
53 * is greater than the configured loglevel.
55 void InspIRCd::Log(int level, const char* text, ...)
58 char textbuffer[MAXBUF];
60 va_start(argsPtr, text);
61 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
64 InspIRCd::Log(level, std::string(textbuffer));
67 void InspIRCd::Log(int level, const std::string &text)
69 extern InspIRCd* ServerInstance;
71 if (!ServerInstance || !ServerInstance->Config)
74 /* If we were given -debug we output all messages, regardless of configured loglevel */
75 if ((level < ServerInstance->Config->LogLevel) && !ServerInstance->Config->forcedebug)
80 struct tm *timeinfo = localtime(&TIME);
82 strlcpy(TIMESTR,asctime(timeinfo),26);
87 if (ServerInstance->Config->log_file && ServerInstance->Config->writelog)
89 fprintf(ServerInstance->Config->log_file,"%s %s\n",TIMESTR,text.c_str());
90 fflush(ServerInstance->Config->log_file);
93 if (ServerInstance->Config->nofork)
95 printf("%s %s\n", TIMESTR, text.c_str());
99 std::string InspIRCd::GetServerDescription(const char* servername)
101 std::string description = "";
103 FOREACH_MOD_I(this,I_OnGetServerDescription,OnGetServerDescription(servername,description));
105 if (description != "")
111 // not a remote server that can be found, it must be me.
112 return Config->ServerDesc;
116 /* XXX - We don't use WriteMode for this because WriteMode is very slow and
117 * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
118 * the number of modes provided, e.g. if you send WriteMode 'og' to write to
119 * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
120 * uses the oper list, which means if you have 2000 users but only 5 opers,
121 * it iterates 5 times.
123 void InspIRCd::WriteOpers(const char* text, ...)
125 char textbuffer[MAXBUF];
128 va_start(argsPtr, text);
129 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
132 this->WriteOpers(std::string(textbuffer));
135 void InspIRCd::WriteOpers(const std::string &text)
137 for (std::vector<userrec*>::iterator i = this->all_opers.begin(); i != this->all_opers.end(); i++)
140 if (IS_LOCAL(a) && a->modes[UM_SERVERNOTICE])
142 // send server notices to all with +s
143 a->WriteServ("NOTICE %s :%s",a->nick,text.c_str());
148 void InspIRCd::ServerNoticeAll(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,"NOTICE $%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::ServerPrivmsgAll(char* text, ...)
174 char textbuffer[MAXBUF];
175 char formatbuffer[MAXBUF];
177 va_start (argsPtr, text);
178 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
181 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
183 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
186 t->WriteServ(std::string(formatbuffer));
190 void InspIRCd::WriteMode(const char* modes, int flags, const char* text, ...)
192 char textbuffer[MAXBUF];
196 if ((!text) || (!modes) || (!flags))
198 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
202 va_start(argsPtr, text);
203 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
205 modelen = strlen(modes);
207 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
209 userrec* t = (userrec*)(*i);
210 bool send_to_user = false;
216 for (int n = 0; n < modelen; n++)
218 if (!t->modes[modes[n]-65])
220 send_to_user = false;
225 else if (flags == WM_OR)
227 send_to_user = false;
229 for (int n = 0; n < modelen; n++)
231 if (t->modes[modes[n]-65])
241 t->WriteServ("NOTICE %s :%s",t->nick,textbuffer);
246 /* Find a user record by nickname and return a pointer to it */
248 userrec* InspIRCd::FindNick(const std::string &nick)
250 user_hash::iterator iter = clientlist.find(nick);
252 if (iter == clientlist.end())
253 /* Couldn't find it */
259 userrec* InspIRCd::FindNick(const char* nick)
261 user_hash::iterator iter;
266 iter = clientlist.find(nick);
268 if (iter == clientlist.end())
274 /* find a channel record by channel name and return a pointer to it */
276 chanrec* InspIRCd::FindChan(const char* chan)
278 chan_hash::iterator iter;
283 iter = chanlist.find(chan);
285 if (iter == chanlist.end())
286 /* Couldn't find it */
292 chanrec* InspIRCd::FindChan(const std::string &chan)
294 chan_hash::iterator iter = chanlist.find(chan);
296 if (iter == chanlist.end())
297 /* Couldn't find it */
305 * sends out an error notice to all connected clients (not to be used
308 void InspIRCd::SendError(const char *s)
310 for (std::vector<userrec*>::const_iterator i = this->local_users.begin(); i != this->local_users.end(); i++)
312 userrec* t = (userrec*)(*i);
313 if (t->registered == REG_ALL)
315 t->WriteServ("NOTICE %s :%s",t->nick,s);
319 // fix - unregistered connections receive ERROR, not NOTICE
320 t->Write("ERROR :%s",s);
325 void InspIRCd::Error(int status)
331 signal(SIGALRM, SIG_IGN);
332 signal(SIGPIPE, SIG_IGN);
333 signal(SIGTERM, SIG_IGN);
334 signal(SIGABRT, SIG_IGN);
335 signal(SIGSEGV, SIG_IGN);
336 signal(SIGURG, SIG_IGN);
337 signal(SIGKILL, SIG_IGN);
338 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
340 log(DEFAULT,"Please report the backtrace lines shown below with any bugreport to the bugtracker at http://www.inspircd.org/bugtrack/");
341 size = backtrace(array, 30);
342 strings = backtrace_symbols(array, size);
343 for (size_t i = 0; i < size; i++) {
344 log(DEFAULT,"[%d] %s", i, strings[i]);
348 log(DEFAULT,"You do not have execinfo.h so i could not backtrace -- on FreeBSD, please install the libexecinfo port.");
350 signal(SIGSEGV, SIG_DFL);
351 if (raise(SIGSEGV) == -1)
353 log(DEFAULT,"What the hell, i couldnt re-raise SIGSEGV! Error: %s",strerror(errno));
358 // this function counts all users connected, wether they are registered or NOT.
359 int InspIRCd::usercnt()
361 return clientlist.size();
364 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
365 int InspIRCd::registered_usercount()
369 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
371 c += (i->second->registered == REG_ALL);
377 int InspIRCd::usercount_invisible()
381 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
383 c += ((i->second->registered == REG_ALL) && (i->second->modes[UM_INVISIBLE]));
389 int InspIRCd::usercount_opers()
393 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
395 if (*(i->second->oper))
401 int InspIRCd::usercount_unknown()
405 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
407 userrec* t = (userrec*)(*i);
408 if (t->registered != REG_ALL)
415 long InspIRCd::chancount()
417 return chanlist.size();
420 long InspIRCd::local_count()
424 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
426 userrec* t = (userrec*)(*i);
427 if (t->registered == REG_ALL)
434 bool InspIRCd::IsChannel(const char *chname)
438 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
439 if (!chname || *chname != '#')
444 c = (char *)chname + 1;
458 /* too long a name - note funky pointer arithmetic here. */
459 if ((c - chname) > CHANMAX)
467 void InspIRCd::OpenLog(char** argv, int argc)
469 if (!*this->LogFileName)
471 if (Config->logpath == "")
473 Config->logpath = ServerConfig::GetFullProgDir(argv,argc) + "/ircd.log";
478 Config->log_file = fopen(this->LogFileName,"a+");
480 if (!Config->log_file)
482 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
488 Config->log_file = fopen(Config->logpath.c_str(),"a+");
490 if (!Config->log_file)
492 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
497 void InspIRCd::CheckRoot()
501 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
502 log(DEFAULT,"InspIRCd: startup: not starting with UID 0!");
507 void InspIRCd::CheckDie()
509 if (*Config->DieValue)
511 printf("WARNING: %s\n\n",Config->DieValue);
512 log(DEFAULT,"Uh-Oh, somebody didn't read their config file: '%s'",Config->DieValue);
517 /* We must load the modules AFTER initializing the socket engine, now */
518 void InspIRCd::LoadAllModules()
520 char configToken[MAXBUF];
521 Config->module_names.clear();
524 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "module"); count++)
526 Config->ConfValue(Config->config_data, "module","name",count,configToken,MAXBUF);
527 printf("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
529 if (!this->LoadModule(configToken))
531 log(DEFAULT,"Exiting due to a module loader error.");
532 printf("\nThere was an error loading a module: %s\n\n",this->ModuleError());
536 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");
537 log(DEFAULT,"Total loaded modules: %d", this->ModCount+1);