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, ...)
35 /* Do this check again here so that we save pointless vsnprintf calls */
36 if ((level < Config->LogLevel) && !Config->forcedebug)
40 char textbuffer[65536];
42 va_start(argsPtr, text);
43 vsnprintf(textbuffer, 65536, text, argsPtr);
46 this->Log(level, std::string(textbuffer));
49 void InspIRCd::Log(int level, const std::string &text)
54 /* If we were given -debug we output all messages, regardless of configured loglevel */
55 if ((level < Config->LogLevel) && !Config->forcedebug)
60 time_t local = Time();
61 struct tm *timeinfo = localtime(&local);
63 strlcpy(TIMESTR,asctime(timeinfo),26);
68 if (Config->log_file && Config->writelog)
70 std::string out = std::string(TIMESTR) + " " + text.c_str() + "\n";
71 this->Logger->WriteLogLine(out);
76 printf("%s %s\n", TIMESTR, text.c_str());
80 std::string InspIRCd::GetServerDescription(const char* servername)
82 std::string description = "";
84 FOREACH_MOD_I(this,I_OnGetServerDescription,OnGetServerDescription(servername,description));
86 if (description != "")
92 // not a remote server that can be found, it must be me.
93 return Config->ServerDesc;
97 /* XXX - We don't use WriteMode for this because WriteMode is very slow and
98 * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
99 * the number of modes provided, e.g. if you send WriteMode 'og' to write to
100 * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
101 * uses the oper list, which means if you have 2000 users but only 5 opers,
102 * it iterates 5 times.
104 void InspIRCd::WriteOpers(const char* text, ...)
106 char textbuffer[MAXBUF];
109 va_start(argsPtr, text);
110 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
113 this->WriteOpers(std::string(textbuffer));
116 void InspIRCd::WriteOpers(const std::string &text)
118 for (std::vector<userrec*>::iterator i = this->all_opers.begin(); i != this->all_opers.end(); i++)
121 if (IS_LOCAL(a) && a->modes[UM_SERVERNOTICE])
123 // send server notices to all with +s
124 a->WriteServ("NOTICE %s :%s",a->nick,text.c_str());
129 void InspIRCd::ServerNoticeAll(char* text, ...)
134 char textbuffer[MAXBUF];
135 char formatbuffer[MAXBUF];
137 va_start (argsPtr, text);
138 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
141 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s",Config->ServerName,textbuffer);
143 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
146 t->WriteServ(std::string(formatbuffer));
150 void InspIRCd::ServerPrivmsgAll(char* text, ...)
155 char textbuffer[MAXBUF];
156 char formatbuffer[MAXBUF];
158 va_start (argsPtr, text);
159 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
162 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
164 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
167 t->WriteServ(std::string(formatbuffer));
171 void InspIRCd::WriteMode(const char* modes, int flags, const char* text, ...)
173 char textbuffer[MAXBUF];
177 if (!text || !modes || !flags)
179 this->Log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
183 va_start(argsPtr, text);
184 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
186 modelen = strlen(modes);
190 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
193 bool send_to_user = true;
195 for (int n = 0; n < modelen; n++)
197 if (!t->modes[modes[n]-65])
199 send_to_user = false;
204 t->WriteServ("NOTICE %s :%s",t->nick,textbuffer);
210 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
213 bool send_to_user = false;
215 for (int n = 0; n < modelen; n++)
217 if (t->modes[modes[n]-65])
224 t->WriteServ("NOTICE %s :%s",t->nick,textbuffer);
229 /* Find a user record by nickname and return a pointer to it */
231 userrec* InspIRCd::FindNick(const std::string &nick)
233 user_hash::iterator iter = clientlist.find(nick);
235 if (iter == clientlist.end())
236 /* Couldn't find it */
242 userrec* InspIRCd::FindNick(const char* nick)
244 user_hash::iterator iter;
249 iter = clientlist.find(nick);
251 if (iter == clientlist.end())
257 /* find a channel record by channel name and return a pointer to it */
259 chanrec* InspIRCd::FindChan(const char* chan)
261 chan_hash::iterator iter;
266 iter = chanlist.find(chan);
268 if (iter == chanlist.end())
269 /* Couldn't find it */
275 chanrec* InspIRCd::FindChan(const std::string &chan)
277 chan_hash::iterator iter = chanlist.find(chan);
279 if (iter == chanlist.end())
280 /* Couldn't find it */
288 * sends out an error notice to all connected clients (not to be used
291 void InspIRCd::SendError(const char *s)
293 for (std::vector<userrec*>::const_iterator i = this->local_users.begin(); i != this->local_users.end(); i++)
295 userrec* t = (userrec*)(*i);
296 if (t->registered == REG_ALL)
298 t->WriteServ("NOTICE %s :%s",t->nick,s);
302 // fix - unregistered connections receive ERROR, not NOTICE
303 t->Write("ERROR :%s",s);
308 // this function counts all users connected, wether they are registered or NOT.
309 int InspIRCd::UserCount()
311 return clientlist.size();
314 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
315 int InspIRCd::RegisteredUserCount()
319 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
321 c += (i->second->registered == REG_ALL);
327 int InspIRCd::InvisibleUserCount()
331 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
333 c += ((i->second->registered == REG_ALL) && (i->second->modes[UM_INVISIBLE]));
339 int InspIRCd::OperCount()
343 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
345 if (*(i->second->oper))
351 int InspIRCd::UnregisteredUserCount()
355 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
357 userrec* t = (userrec*)(*i);
358 if (t->registered != REG_ALL)
365 long InspIRCd::ChannelCount()
367 return chanlist.size();
370 long InspIRCd::LocalUserCount()
374 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
376 userrec* t = (userrec*)(*i);
377 if (t->registered == REG_ALL)
384 bool InspIRCd::IsChannel(const char *chname)
388 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
389 if (!chname || *chname != '#')
394 c = (char *)chname + 1;
408 /* too long a name - note funky pointer arithmetic here. */
409 if ((c - chname) > CHANMAX)
417 bool InspIRCd::IsNick(const char* n)
423 for (char* i = (char*)n; *i; i++, p++)
425 if ((*i >= 'A') && (*i <= '}'))
427 /* "A"-"}" can occur anywhere in a nickname */
431 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
433 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
437 /* invalid character! abort */
441 /* too long? or not -- pointer arithmetic rocks */
442 return (p < NICKMAX - 1);
445 void InspIRCd::OpenLog(char** argv, int argc)
447 if (!*this->LogFileName)
449 if (Config->logpath == "")
451 Config->logpath = ServerConfig::GetFullProgDir(argv,argc) + "/ircd.log";
454 Config->log_file = fopen(Config->logpath.c_str(),"a+");
458 Config->log_file = fopen(this->LogFileName,"a+");
461 if (!Config->log_file)
463 printf("ERROR: Could not write to logfile %s: %s\n\n", Config->logpath.c_str(), strerror(errno));
467 this->Logger = new FileLogger(this, Config->log_file);
470 void InspIRCd::CheckRoot()
474 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
475 this->Log(DEFAULT,"Cant start as root");
480 void InspIRCd::CheckDie()
482 if (*Config->DieValue)
484 printf("WARNING: %s\n\n",Config->DieValue);
485 this->Log(DEFAULT,"Died because of <die> tag: %s",Config->DieValue);
490 /* We must load the modules AFTER initializing the socket engine, now */
491 void InspIRCd::LoadAllModules()
493 char configToken[MAXBUF];
494 Config->module_names.clear();
497 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "module"); count++)
499 Config->ConfValue(Config->config_data, "module","name",count,configToken,MAXBUF);
500 printf("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
502 if (!this->LoadModule(configToken))
504 this->Log(DEFAULT,"There was an error loading a module: %s", this->ModuleError());
505 printf("\nThere was an error loading a module: %s\n\n",this->ModuleError());
509 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");
510 this->Log(DEFAULT,"Total loaded modules: %d", this->ModCount+1);
513 void InspIRCd::SendWhoisLine(userrec* user, userrec* dest, int numeric, const std::string &text)
515 std::string copy_text = text;
518 FOREACH_RESULT_I(this, I_OnWhoisLine, OnWhoisLine(user, dest, numeric, copy_text));
521 user->WriteServ("%d %s", numeric, copy_text.c_str());
524 void InspIRCd::SendWhoisLine(userrec* user, userrec* dest, int numeric, const char* format, ...)
526 char textbuffer[MAXBUF];
528 va_start (argsPtr, format);
529 vsnprintf(textbuffer, MAXBUF, format, argsPtr);
532 this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));