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"
46 static char TIMESTR[26];
47 static time_t LAST = 0;
50 * Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level'
51 * is greater than the configured loglevel.
53 void InspIRCd::Log(int level, const char* text, ...)
56 char textbuffer[MAXBUF];
58 va_start(argsPtr, text);
59 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
62 this->Log(level, std::string(textbuffer));
65 void InspIRCd::Log(int level, const std::string &text)
70 /* If we were given -debug we output all messages, regardless of configured loglevel */
71 if ((level < Config->LogLevel) && !Config->forcedebug)
76 time_t local = Time();
77 struct tm *timeinfo = localtime(&local);
79 strlcpy(TIMESTR,asctime(timeinfo),26);
84 if (Config->log_file && Config->writelog)
86 std::string out = std::string(TIMESTR) + " " + text.c_str() + "\n";
87 this->Logger->WriteLogLine(out);
92 printf("%s %s\n", TIMESTR, text.c_str());
96 std::string InspIRCd::GetServerDescription(const char* servername)
98 std::string description = "";
100 FOREACH_MOD_I(this,I_OnGetServerDescription,OnGetServerDescription(servername,description));
102 if (description != "")
108 // not a remote server that can be found, it must be me.
109 return Config->ServerDesc;
113 /* XXX - We don't use WriteMode for this because WriteMode is very slow and
114 * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
115 * the number of modes provided, e.g. if you send WriteMode 'og' to write to
116 * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
117 * uses the oper list, which means if you have 2000 users but only 5 opers,
118 * it iterates 5 times.
120 void InspIRCd::WriteOpers(const char* text, ...)
122 char textbuffer[MAXBUF];
125 va_start(argsPtr, text);
126 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
129 this->WriteOpers(std::string(textbuffer));
132 void InspIRCd::WriteOpers(const std::string &text)
134 for (std::vector<userrec*>::iterator i = this->all_opers.begin(); i != this->all_opers.end(); i++)
137 if (IS_LOCAL(a) && a->modes[UM_SERVERNOTICE])
139 // send server notices to all with +s
140 a->WriteServ("NOTICE %s :%s",a->nick,text.c_str());
145 void InspIRCd::ServerNoticeAll(char* text, ...)
150 char textbuffer[MAXBUF];
151 char formatbuffer[MAXBUF];
153 va_start (argsPtr, text);
154 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
157 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s",Config->ServerName,textbuffer);
159 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
162 t->WriteServ(std::string(formatbuffer));
166 void InspIRCd::ServerPrivmsgAll(char* text, ...)
171 char textbuffer[MAXBUF];
172 char formatbuffer[MAXBUF];
174 va_start (argsPtr, text);
175 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
178 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
180 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
183 t->WriteServ(std::string(formatbuffer));
187 void InspIRCd::WriteMode(const char* modes, int flags, const char* text, ...)
189 char textbuffer[MAXBUF];
193 if ((!text) || (!modes) || (!flags))
195 this->Log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
199 va_start(argsPtr, text);
200 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
202 modelen = strlen(modes);
204 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
206 userrec* t = (userrec*)(*i);
207 bool send_to_user = false;
213 for (int n = 0; n < modelen; n++)
215 if (!t->modes[modes[n]-65])
217 send_to_user = false;
222 else if (flags == WM_OR)
224 send_to_user = false;
226 for (int n = 0; n < modelen; n++)
228 if (t->modes[modes[n]-65])
238 t->WriteServ("NOTICE %s :%s",t->nick,textbuffer);
243 /* Find a user record by nickname and return a pointer to it */
245 userrec* InspIRCd::FindNick(const std::string &nick)
247 user_hash::iterator iter = clientlist.find(nick);
249 if (iter == clientlist.end())
250 /* Couldn't find it */
256 userrec* InspIRCd::FindNick(const char* nick)
258 user_hash::iterator iter;
263 iter = clientlist.find(nick);
265 if (iter == clientlist.end())
271 /* find a channel record by channel name and return a pointer to it */
273 chanrec* InspIRCd::FindChan(const char* chan)
275 chan_hash::iterator iter;
280 iter = chanlist.find(chan);
282 if (iter == chanlist.end())
283 /* Couldn't find it */
289 chanrec* InspIRCd::FindChan(const std::string &chan)
291 chan_hash::iterator iter = chanlist.find(chan);
293 if (iter == chanlist.end())
294 /* Couldn't find it */
302 * sends out an error notice to all connected clients (not to be used
305 void InspIRCd::SendError(const char *s)
307 for (std::vector<userrec*>::const_iterator i = this->local_users.begin(); i != this->local_users.end(); i++)
309 userrec* t = (userrec*)(*i);
310 if (t->registered == REG_ALL)
312 t->WriteServ("NOTICE %s :%s",t->nick,s);
316 // fix - unregistered connections receive ERROR, not NOTICE
317 t->Write("ERROR :%s",s);
322 // this function counts all users connected, wether they are registered or NOT.
323 int InspIRCd::UserCount()
325 return clientlist.size();
328 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
329 int InspIRCd::RegisteredUserCount()
333 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
335 c += (i->second->registered == REG_ALL);
341 int InspIRCd::InvisibleUserCount()
345 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
347 c += ((i->second->registered == REG_ALL) && (i->second->modes[UM_INVISIBLE]));
353 int InspIRCd::OperCount()
357 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
359 if (*(i->second->oper))
365 int InspIRCd::UnregisteredUserCount()
369 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
371 userrec* t = (userrec*)(*i);
372 if (t->registered != REG_ALL)
379 long InspIRCd::ChannelCount()
381 return chanlist.size();
384 long InspIRCd::LocalUserCount()
388 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
390 userrec* t = (userrec*)(*i);
391 if (t->registered == REG_ALL)
398 bool InspIRCd::IsChannel(const char *chname)
402 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
403 if (!chname || *chname != '#')
408 c = (char *)chname + 1;
422 /* too long a name - note funky pointer arithmetic here. */
423 if ((c - chname) > CHANMAX)
431 void InspIRCd::OpenLog(char** argv, int argc)
433 if (!*this->LogFileName)
435 if (Config->logpath == "")
437 Config->logpath = ServerConfig::GetFullProgDir(argv,argc) + "/ircd.log";
442 Config->log_file = fopen(this->LogFileName,"a+");
444 if (!Config->log_file)
446 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
450 this->Logger = new FileLogger(this, Config->log_file);
454 Config->log_file = fopen(Config->logpath.c_str(),"a+");
456 if (!Config->log_file)
458 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
462 this->Logger = new FileLogger(this, Config->log_file);
465 void InspIRCd::CheckRoot()
469 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
470 this->Log(DEFAULT,"Cant start as root");
475 void InspIRCd::CheckDie()
477 if (*Config->DieValue)
479 printf("WARNING: %s\n\n",Config->DieValue);
480 this->Log(DEFAULT,"Died because of <die> tag: %s",Config->DieValue);
485 /* We must load the modules AFTER initializing the socket engine, now */
486 void InspIRCd::LoadAllModules()
488 char configToken[MAXBUF];
489 Config->module_names.clear();
492 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "module"); count++)
494 Config->ConfValue(Config->config_data, "module","name",count,configToken,MAXBUF);
495 printf("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
497 if (!this->LoadModule(configToken))
499 this->Log(DEFAULT,"There was an error loading a module: %s", this->ModuleError());
500 printf("\nThere was an error loading a module: %s\n\n",this->ModuleError());
504 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");
505 this->Log(DEFAULT,"Total loaded modules: %d", this->ModCount+1);