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;
48 extern InspIRCd* ServerInstance;
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 if (!ServerInstance || !ServerInstance->Config)
79 /* If we were given -debug we output all messages, regardless of configured loglevel */
80 if ((level < ServerInstance->Config->LogLevel) && !ServerInstance->Config->forcedebug)
85 struct tm *timeinfo = localtime(&TIME);
87 strlcpy(TIMESTR,asctime(timeinfo),26);
92 if (ServerInstance->Config->log_file && ServerInstance->Config->writelog)
94 fprintf(ServerInstance->Config->log_file,"%s %s\n",TIMESTR,text.c_str());
95 fflush(ServerInstance->Config->log_file);
98 if (ServerInstance->Config->nofork)
100 printf("%s %s\n", TIMESTR, text.c_str());
104 std::string InspIRCd::GetServerDescription(const char* servername)
106 std::string description = "";
108 FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
110 if (description != "")
116 // not a remote server that can be found, it must be me.
117 return Config->ServerDesc;
121 /* XXX - We don't use WriteMode for this because WriteMode is very slow and
122 * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
123 * the number of modes provided, e.g. if you send WriteMode 'og' to write to
124 * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
125 * uses the oper list, which means if you have 2000 users but only 5 opers,
126 * it iterates 5 times.
128 void InspIRCd::WriteOpers(const char* text, ...)
130 char textbuffer[MAXBUF];
133 va_start(argsPtr, text);
134 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
137 this->WriteOpers(std::string(textbuffer));
140 void InspIRCd::WriteOpers(const std::string &text)
142 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
145 if (IS_LOCAL(a) && a->modes[UM_SERVERNOTICE])
147 // send server notices to all with +s
148 a->WriteServ("NOTICE %s :%s",a->nick,text.c_str());
153 void InspIRCd::ServerNoticeAll(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,"NOTICE $%s :%s",ServerInstance->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::ServerPrivmsgAll(char* text, ...)
179 char textbuffer[MAXBUF];
180 char formatbuffer[MAXBUF];
182 va_start (argsPtr, text);
183 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
186 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s",ServerInstance->Config->ServerName,textbuffer);
188 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
191 t->WriteServ(std::string(formatbuffer));
195 void InspIRCd::WriteMode(const char* modes, int flags, const char* text, ...)
197 char textbuffer[MAXBUF];
201 if ((!text) || (!modes) || (!flags))
203 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
207 va_start(argsPtr, text);
208 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
210 modelen = strlen(modes);
212 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
214 userrec* t = (userrec*)(*i);
215 bool send_to_user = false;
221 for (int n = 0; n < modelen; n++)
223 if (!t->modes[modes[n]-65])
225 send_to_user = false;
230 else if (flags == WM_OR)
232 send_to_user = false;
234 for (int n = 0; n < modelen; n++)
236 if (t->modes[modes[n]-65])
246 t->WriteServ("NOTICE %s :%s",t->nick,textbuffer);
251 /* Find a user record by nickname and return a pointer to it */
253 userrec* InspIRCd::FindNick(const std::string &nick)
255 user_hash::iterator iter = clientlist.find(nick);
257 if (iter == clientlist.end())
258 /* Couldn't find it */
264 userrec* InspIRCd::FindNick(const char* nick)
266 user_hash::iterator iter;
271 iter = clientlist.find(nick);
273 if (iter == clientlist.end())
279 /* find a channel record by channel name and return a pointer to it */
281 chanrec* InspIRCd::FindChan(const char* chan)
283 chan_hash::iterator iter;
288 iter = chanlist.find(chan);
290 if (iter == chanlist.end())
291 /* Couldn't find it */
297 chanrec* InspIRCd::FindChan(const std::string &chan)
299 chan_hash::iterator iter = chanlist.find(chan);
301 if (iter == chanlist.end())
302 /* Couldn't find it */
310 * sends out an error notice to all connected clients (not to be used
313 void InspIRCd::SendError(const char *s)
315 for (std::vector<userrec*>::const_iterator i = this->local_users.begin(); i != this->local_users.end(); i++)
317 userrec* t = (userrec*)(*i);
318 if (t->registered == REG_ALL)
320 t->WriteServ("NOTICE %s :%s",t->nick,s);
324 // fix - unregistered connections receive ERROR, not NOTICE
325 t->Write("ERROR :%s",s);
330 void Error(int status)
336 signal(SIGALRM, SIG_IGN);
337 signal(SIGPIPE, SIG_IGN);
338 signal(SIGTERM, SIG_IGN);
339 signal(SIGABRT, SIG_IGN);
340 signal(SIGSEGV, SIG_IGN);
341 signal(SIGURG, SIG_IGN);
342 signal(SIGKILL, SIG_IGN);
343 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
345 log(DEFAULT,"Please report the backtrace lines shown below with any bugreport to the bugtracker at http://www.inspircd.org/bugtrack/");
346 size = backtrace(array, 30);
347 strings = backtrace_symbols(array, size);
348 for (size_t i = 0; i < size; i++) {
349 log(DEFAULT,"[%d] %s", i, strings[i]);
352 ServerInstance->WriteOpers("*** SIGSEGV: Please see the ircd.log for backtrace and report the error to http://www.inspircd.org/bugtrack/");
354 log(DEFAULT,"You do not have execinfo.h so i could not backtrace -- on FreeBSD, please install the libexecinfo port.");
356 ServerInstance->SendError("Somebody screwed up... Whoops. IRC Server terminating.");
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 void ShowMOTD(userrec *user)
443 if (!ServerInstance->Config->MOTD.size())
445 user->WriteServ("422 %s :Message of the day file is missing.",user->nick);
448 user->WriteServ("375 %s :%s message of the day", user->nick, ServerInstance->Config->ServerName);
450 for (unsigned int i = 0; i < ServerInstance->Config->MOTD.size(); i++)
451 user->WriteServ("372 %s :- %s",user->nick,ServerInstance->Config->MOTD[i].c_str());
453 user->WriteServ("376 %s :End of message of the day.", user->nick);
456 void ShowRULES(userrec *user)
458 if (!ServerInstance->Config->RULES.size())
460 user->WriteServ("NOTICE %s :Rules file is missing.",user->nick);
463 user->WriteServ("NOTICE %s :%s rules",user->nick,ServerInstance->Config->ServerName);
465 for (unsigned int i = 0; i < ServerInstance->Config->RULES.size(); i++)
466 user->WriteServ("NOTICE %s :%s",user->nick,ServerInstance->Config->RULES[i].c_str());
468 user->WriteServ("NOTICE %s :End of %s rules.",user->nick,ServerInstance->Config->ServerName);
471 bool IsValidChannelName(const char *chname)
475 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
476 if (!chname || *chname != '#')
481 c = (char *)chname + 1;
495 /* too long a name - note funky pointer arithmetic here. */
496 if ((c - chname) > CHANMAX)
504 void OpenLog(char** argv, int argc)
508 if (ServerInstance->Config->logpath == "")
510 ServerInstance->Config->logpath = ServerConfig::GetFullProgDir(argv,argc) + "/ircd.log";
515 ServerInstance->Config->log_file = fopen(LOG_FILE,"a+");
517 if (!ServerInstance->Config->log_file)
519 printf("ERROR: Could not write to logfile %s, bailing!\n\n",ServerInstance->Config->logpath.c_str());
526 ServerInstance->Config->log_file = fopen(ServerInstance->Config->logpath.c_str(),"a+");
528 if (!ServerInstance->Config->log_file)
530 printf("ERROR: Could not write to logfile %s, bailing!\n\n",ServerInstance->Config->logpath.c_str());
539 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
540 log(DEFAULT,"InspIRCd: startup: not starting with UID 0!");
547 if (*ServerInstance->Config->DieValue)
549 printf("WARNING: %s\n\n",ServerInstance->Config->DieValue);
550 log(DEFAULT,"Uh-Oh, somebody didn't read their config file: '%s'",ServerInstance->Config->DieValue);
555 /* We must load the modules AFTER initializing the socket engine, now */
556 void LoadAllModules(InspIRCd* ServerInstance)
558 char configToken[MAXBUF];
559 ServerInstance->Config->module_names.clear();
562 for (int count = 0; count < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "module"); count++)
564 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "module","name",count,configToken,MAXBUF);
565 printf("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
567 if (!ServerInstance->LoadModule(configToken))
569 log(DEFAULT,"Exiting due to a module loader error.");
570 printf("\nThere was an error loading a module: %s\n\n",ServerInstance->ModuleError());
575 log(DEFAULT,"Total loaded modules: %lu",(unsigned long)MODCOUNT+1);