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 "configreader.h"
26 static char TIMESTR[26];
27 static time_t LAST = 0;
30 * Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level'
31 * is greater than the configured loglevel.
33 void InspIRCd::Log(int level, const char* text, ...)
36 char textbuffer[MAXBUF];
38 va_start(argsPtr, text);
39 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
42 this->Log(level, std::string(textbuffer));
45 void InspIRCd::Log(int level, const std::string &text)
50 /* If we were given -debug we output all messages, regardless of configured loglevel */
51 if ((level < Config->LogLevel) && !Config->forcedebug)
56 time_t local = Time();
57 struct tm *timeinfo = localtime(&local);
59 strlcpy(TIMESTR,asctime(timeinfo),26);
64 if (Config->log_file && Config->writelog)
66 std::string out = std::string(TIMESTR) + " " + text.c_str() + "\n";
67 this->Logger->WriteLogLine(out);
72 printf("%s %s\n", TIMESTR, text.c_str());
76 std::string InspIRCd::GetServerDescription(const char* servername)
78 std::string description = "";
80 FOREACH_MOD_I(this,I_OnGetServerDescription,OnGetServerDescription(servername,description));
82 if (description != "")
88 // not a remote server that can be found, it must be me.
89 return Config->ServerDesc;
93 /* XXX - We don't use WriteMode for this because WriteMode is very slow and
94 * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
95 * the number of modes provided, e.g. if you send WriteMode 'og' to write to
96 * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
97 * uses the oper list, which means if you have 2000 users but only 5 opers,
98 * it iterates 5 times.
100 void InspIRCd::WriteOpers(const char* text, ...)
102 char textbuffer[MAXBUF];
105 va_start(argsPtr, text);
106 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
109 this->WriteOpers(std::string(textbuffer));
112 void InspIRCd::WriteOpers(const std::string &text)
114 for (std::vector<userrec*>::iterator i = this->all_opers.begin(); i != this->all_opers.end(); i++)
117 if (IS_LOCAL(a) && a->modes[UM_SERVERNOTICE])
119 // send server notices to all with +s
120 a->WriteServ("NOTICE %s :%s",a->nick,text.c_str());
125 void InspIRCd::ServerNoticeAll(char* text, ...)
130 char textbuffer[MAXBUF];
131 char formatbuffer[MAXBUF];
133 va_start (argsPtr, text);
134 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
137 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s",Config->ServerName,textbuffer);
139 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
142 t->WriteServ(std::string(formatbuffer));
146 void InspIRCd::ServerPrivmsgAll(char* text, ...)
151 char textbuffer[MAXBUF];
152 char formatbuffer[MAXBUF];
154 va_start (argsPtr, text);
155 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
158 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
160 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
163 t->WriteServ(std::string(formatbuffer));
167 void InspIRCd::WriteMode(const char* modes, int flags, const char* text, ...)
169 char textbuffer[MAXBUF];
173 if (!text || !modes || !flags)
175 this->Log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
179 va_start(argsPtr, text);
180 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
182 modelen = strlen(modes);
186 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
189 bool send_to_user = true;
191 for (int n = 0; n < modelen; n++)
193 if (!t->modes[modes[n]-65])
195 send_to_user = false;
200 t->WriteServ("NOTICE %s :%s",t->nick,textbuffer);
206 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
209 bool send_to_user = false;
211 for (int n = 0; n < modelen; n++)
213 if (t->modes[modes[n]-65])
220 t->WriteServ("NOTICE %s :%s",t->nick,textbuffer);
225 /* Find a user record by nickname and return a pointer to it */
227 userrec* InspIRCd::FindNick(const std::string &nick)
229 user_hash::iterator iter = clientlist.find(nick);
231 if (iter == clientlist.end())
232 /* Couldn't find it */
238 userrec* InspIRCd::FindNick(const char* nick)
240 user_hash::iterator iter;
245 iter = clientlist.find(nick);
247 if (iter == clientlist.end())
253 /* find a channel record by channel name and return a pointer to it */
255 chanrec* InspIRCd::FindChan(const char* chan)
257 chan_hash::iterator iter;
262 iter = chanlist.find(chan);
264 if (iter == chanlist.end())
265 /* Couldn't find it */
271 chanrec* InspIRCd::FindChan(const std::string &chan)
273 chan_hash::iterator iter = chanlist.find(chan);
275 if (iter == chanlist.end())
276 /* Couldn't find it */
284 * sends out an error notice to all connected clients (not to be used
287 void InspIRCd::SendError(const char *s)
289 for (std::vector<userrec*>::const_iterator i = this->local_users.begin(); i != this->local_users.end(); i++)
291 userrec* t = (userrec*)(*i);
292 if (t->registered == REG_ALL)
294 t->WriteServ("NOTICE %s :%s",t->nick,s);
298 // fix - unregistered connections receive ERROR, not NOTICE
299 t->Write("ERROR :%s",s);
304 // this function counts all users connected, wether they are registered or NOT.
305 int InspIRCd::UserCount()
307 return clientlist.size();
310 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
311 int InspIRCd::RegisteredUserCount()
315 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
317 c += (i->second->registered == REG_ALL);
323 int InspIRCd::InvisibleUserCount()
327 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
329 c += ((i->second->registered == REG_ALL) && (i->second->modes[UM_INVISIBLE]));
335 int InspIRCd::OperCount()
339 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
341 if (*(i->second->oper))
347 int InspIRCd::UnregisteredUserCount()
351 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
353 userrec* t = (userrec*)(*i);
354 if (t->registered != REG_ALL)
361 long InspIRCd::ChannelCount()
363 return chanlist.size();
366 long InspIRCd::LocalUserCount()
370 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
372 userrec* t = (userrec*)(*i);
373 if (t->registered == REG_ALL)
380 bool InspIRCd::IsChannel(const char *chname)
384 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
385 if (!chname || *chname != '#')
390 c = (char *)chname + 1;
404 /* too long a name - note funky pointer arithmetic here. */
405 if ((c - chname) > CHANMAX)
413 bool InspIRCd::IsNick(const char* n)
419 for (char* i = (char*)n; *i; i++, p++)
421 if ((*i >= 'A') && (*i <= '}'))
423 /* "A"-"}" can occur anywhere in a nickname */
427 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
429 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
433 /* invalid character! abort */
437 /* too long? or not -- pointer arithmetic rocks */
438 return (p < NICKMAX - 1);
441 void InspIRCd::OpenLog(char** argv, int argc)
443 if (!*this->LogFileName)
445 if (Config->logpath == "")
447 Config->logpath = ServerConfig::GetFullProgDir(argv,argc) + "/ircd.log";
450 Config->log_file = fopen(Config->logpath.c_str(),"a+");
454 Config->log_file = fopen(this->LogFileName,"a+");
457 if (!Config->log_file)
459 printf("ERROR: Could not write to logfile %s: %s\n\n", Config->logpath.c_str(), strerror(errno));
463 this->Logger = new FileLogger(this, Config->log_file);
466 void InspIRCd::CheckRoot()
470 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
471 this->Log(DEFAULT,"Cant start as root");
476 void InspIRCd::CheckDie()
478 if (*Config->DieValue)
480 printf("WARNING: %s\n\n",Config->DieValue);
481 this->Log(DEFAULT,"Died because of <die> tag: %s",Config->DieValue);
486 /* We must load the modules AFTER initializing the socket engine, now */
487 void InspIRCd::LoadAllModules()
489 char configToken[MAXBUF];
490 Config->module_names.clear();
493 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "module"); count++)
495 Config->ConfValue(Config->config_data, "module","name",count,configToken,MAXBUF);
496 printf("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
498 if (!this->LoadModule(configToken))
500 this->Log(DEFAULT,"There was an error loading a module: %s", this->ModuleError());
501 printf("\nThere was an error loading a module: %s\n\n",this->ModuleError());
505 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");
506 this->Log(DEFAULT,"Total loaded modules: %d", this->ModCount+1);
509 void InspIRCd::SendWhoisLine(userrec* user, userrec* dest, int numeric, const std::string &text)
511 std::string copy_text = text;
514 FOREACH_RESULT_I(this, I_OnWhoisLine, OnWhoisLine(user, dest, numeric, copy_text));
517 user->WriteServ("%d %s", numeric, copy_text.c_str());
520 void InspIRCd::SendWhoisLine(userrec* user, userrec* dest, int numeric, const char* format, ...)
522 char textbuffer[MAXBUF];
524 va_start (argsPtr, format);
525 vsnprintf(textbuffer, MAXBUF, format, argsPtr);
528 this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));