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"
47 extern ModuleList modules;
50 extern char lowermap[255];
51 extern std::vector<userrec*> all_opers;
53 char LOG_FILE[MAXBUF];
55 static char TIMESTR[26];
56 static time_t LAST = 0;
59 * Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level'
60 * is greater than the configured loglevel.
62 void InspIRCd::Log(int level, const char* text, ...)
65 char textbuffer[MAXBUF];
67 va_start(argsPtr, text);
68 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
71 InspIRCd::Log(level, std::string(textbuffer));
74 void InspIRCd::Log(int level, const std::string &text)
76 extern InspIRCd* ServerInstance;
78 if (!ServerInstance || !ServerInstance->Config)
81 /* If we were given -debug we output all messages, regardless of configured loglevel */
82 if ((level < ServerInstance->Config->LogLevel) && !ServerInstance->Config->forcedebug)
87 struct tm *timeinfo = localtime(&TIME);
89 strlcpy(TIMESTR,asctime(timeinfo),26);
94 if (ServerInstance->Config->log_file && ServerInstance->Config->writelog)
96 fprintf(ServerInstance->Config->log_file,"%s %s\n",TIMESTR,text.c_str());
97 fflush(ServerInstance->Config->log_file);
100 if (ServerInstance->Config->nofork)
102 printf("%s %s\n", TIMESTR, text.c_str());
106 std::string InspIRCd::GetServerDescription(const char* servername)
108 std::string description = "";
110 FOREACH_MOD_I(this,I_OnGetServerDescription,OnGetServerDescription(servername,description));
112 if (description != "")
118 // not a remote server that can be found, it must be me.
119 return Config->ServerDesc;
123 /* XXX - We don't use WriteMode for this because WriteMode is very slow and
124 * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
125 * the number of modes provided, e.g. if you send WriteMode 'og' to write to
126 * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
127 * uses the oper list, which means if you have 2000 users but only 5 opers,
128 * it iterates 5 times.
130 void InspIRCd::WriteOpers(const char* text, ...)
132 char textbuffer[MAXBUF];
135 va_start(argsPtr, text);
136 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
139 this->WriteOpers(std::string(textbuffer));
142 void InspIRCd::WriteOpers(const std::string &text)
144 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
147 if (IS_LOCAL(a) && a->modes[UM_SERVERNOTICE])
149 // send server notices to all with +s
150 a->WriteServ("NOTICE %s :%s",a->nick,text.c_str());
155 void InspIRCd::ServerNoticeAll(char* text, ...)
160 char textbuffer[MAXBUF];
161 char formatbuffer[MAXBUF];
163 va_start (argsPtr, text);
164 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
167 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s",Config->ServerName,textbuffer);
169 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
172 t->WriteServ(std::string(formatbuffer));
176 void InspIRCd::ServerPrivmsgAll(char* text, ...)
181 char textbuffer[MAXBUF];
182 char formatbuffer[MAXBUF];
184 va_start (argsPtr, text);
185 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
188 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
190 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
193 t->WriteServ(std::string(formatbuffer));
197 void InspIRCd::WriteMode(const char* modes, int flags, const char* text, ...)
199 char textbuffer[MAXBUF];
203 if ((!text) || (!modes) || (!flags))
205 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
209 va_start(argsPtr, text);
210 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
212 modelen = strlen(modes);
214 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
216 userrec* t = (userrec*)(*i);
217 bool send_to_user = false;
223 for (int n = 0; n < modelen; n++)
225 if (!t->modes[modes[n]-65])
227 send_to_user = false;
232 else if (flags == WM_OR)
234 send_to_user = false;
236 for (int n = 0; n < modelen; n++)
238 if (t->modes[modes[n]-65])
248 t->WriteServ("NOTICE %s :%s",t->nick,textbuffer);
253 /* Find a user record by nickname and return a pointer to it */
255 userrec* InspIRCd::FindNick(const std::string &nick)
257 user_hash::iterator iter = clientlist.find(nick);
259 if (iter == clientlist.end())
260 /* Couldn't find it */
266 userrec* InspIRCd::FindNick(const char* nick)
268 user_hash::iterator iter;
273 iter = clientlist.find(nick);
275 if (iter == clientlist.end())
281 /* find a channel record by channel name and return a pointer to it */
283 chanrec* InspIRCd::FindChan(const char* chan)
285 chan_hash::iterator iter;
290 iter = chanlist.find(chan);
292 if (iter == chanlist.end())
293 /* Couldn't find it */
299 chanrec* InspIRCd::FindChan(const std::string &chan)
301 chan_hash::iterator iter = chanlist.find(chan);
303 if (iter == chanlist.end())
304 /* Couldn't find it */
312 * sends out an error notice to all connected clients (not to be used
315 void InspIRCd::SendError(const char *s)
317 for (std::vector<userrec*>::const_iterator i = this->local_users.begin(); i != this->local_users.end(); i++)
319 userrec* t = (userrec*)(*i);
320 if (t->registered == REG_ALL)
322 t->WriteServ("NOTICE %s :%s",t->nick,s);
326 // fix - unregistered connections receive ERROR, not NOTICE
327 t->Write("ERROR :%s",s);
332 void InspIRCd::Error(int status)
338 signal(SIGALRM, SIG_IGN);
339 signal(SIGPIPE, SIG_IGN);
340 signal(SIGTERM, SIG_IGN);
341 signal(SIGABRT, SIG_IGN);
342 signal(SIGSEGV, SIG_IGN);
343 signal(SIGURG, SIG_IGN);
344 signal(SIGKILL, SIG_IGN);
345 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
347 log(DEFAULT,"Please report the backtrace lines shown below with any bugreport to the bugtracker at http://www.inspircd.org/bugtrack/");
348 size = backtrace(array, 30);
349 strings = backtrace_symbols(array, size);
350 for (size_t i = 0; i < size; i++) {
351 log(DEFAULT,"[%d] %s", i, strings[i]);
355 log(DEFAULT,"You do not have execinfo.h so i could not backtrace -- on FreeBSD, please install the libexecinfo port.");
357 signal(SIGSEGV, SIG_DFL);
358 if (raise(SIGSEGV) == -1)
360 log(DEFAULT,"What the hell, i couldnt re-raise SIGSEGV! Error: %s",strerror(errno));
365 // this function counts all users connected, wether they are registered or NOT.
366 int InspIRCd::usercnt()
368 return clientlist.size();
371 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
372 int InspIRCd::registered_usercount()
376 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
378 c += (i->second->registered == REG_ALL);
384 int InspIRCd::usercount_invisible()
388 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
390 c += ((i->second->registered == REG_ALL) && (i->second->modes[UM_INVISIBLE]));
396 int InspIRCd::usercount_opers()
400 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
402 if (*(i->second->oper))
408 int InspIRCd::usercount_unknown()
412 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
414 userrec* t = (userrec*)(*i);
415 if (t->registered != REG_ALL)
422 long InspIRCd::chancount()
424 return chanlist.size();
427 long InspIRCd::local_count()
431 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
433 userrec* t = (userrec*)(*i);
434 if (t->registered == REG_ALL)
441 bool InspIRCd::IsChannel(const char *chname)
445 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
446 if (!chname || *chname != '#')
451 c = (char *)chname + 1;
465 /* too long a name - note funky pointer arithmetic here. */
466 if ((c - chname) > CHANMAX)
474 void InspIRCd::OpenLog(char** argv, int argc)
478 if (Config->logpath == "")
480 Config->logpath = ServerConfig::GetFullProgDir(argv,argc) + "/ircd.log";
485 Config->log_file = fopen(LOG_FILE,"a+");
487 if (!Config->log_file)
489 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
495 Config->log_file = fopen(Config->logpath.c_str(),"a+");
497 if (!Config->log_file)
499 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
504 void InspIRCd::CheckRoot()
508 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
509 log(DEFAULT,"InspIRCd: startup: not starting with UID 0!");
514 void InspIRCd::CheckDie()
516 if (*Config->DieValue)
518 printf("WARNING: %s\n\n",Config->DieValue);
519 log(DEFAULT,"Uh-Oh, somebody didn't read their config file: '%s'",Config->DieValue);
524 /* We must load the modules AFTER initializing the socket engine, now */
525 void InspIRCd::LoadAllModules()
527 char configToken[MAXBUF];
528 Config->module_names.clear();
531 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "module"); count++)
533 Config->ConfValue(Config->config_data, "module","name",count,configToken,MAXBUF);
534 printf("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
536 if (!this->LoadModule(configToken))
538 log(DEFAULT,"Exiting due to a module loader error.");
539 printf("\nThere was an error loading a module: %s\n\n",this->ModuleError());
543 log(DEFAULT,"Total loaded modules: %lu",(unsigned long)MODCOUNT+1);