1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 /* $Core: libIRCDhelper */
20 #include "exitcodes.h"
22 static char TIMESTR[26];
23 static time_t LAST = 0;
26 * Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level'
27 * is greater than the configured loglevel.
29 void InspIRCd::Log(int level, const char* text, ...)
31 /* sanity check, just in case */
32 if (!this->Config || !this->Logger)
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)
51 /* sanity check, just in case */
52 if (!this->Config || !this->Logger)
55 /* If we were given -debug we output all messages, regardless of configured loglevel */
56 if ((level < Config->LogLevel) && !Config->forcedebug)
61 time_t local = Time();
62 struct tm *timeinfo = localtime(&local);
64 strlcpy(TIMESTR,asctime(timeinfo),26);
69 if (Config->log_file && Config->writelog)
71 std::string out = std::string(TIMESTR) + " " + text.c_str() + "\n";
72 this->Logger->WriteLogLine(out);
77 printf("%s %s\n", TIMESTR, text.c_str());
81 std::string InspIRCd::GetServerDescription(const char* servername)
83 std::string description;
85 FOREACH_MOD_I(this,I_OnGetServerDescription,OnGetServerDescription(servername,description));
87 if (!description.empty())
93 // not a remote server that can be found, it must be me.
94 return Config->ServerDesc;
98 void InspIRCd::ServerNoticeAll(char* text, ...)
103 char textbuffer[MAXBUF];
104 char formatbuffer[MAXBUF];
106 va_start (argsPtr, text);
107 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
110 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s",Config->ServerName,textbuffer);
112 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
115 t->WriteServ(std::string(formatbuffer));
119 void InspIRCd::ServerPrivmsgAll(char* text, ...)
124 char textbuffer[MAXBUF];
125 char formatbuffer[MAXBUF];
127 va_start (argsPtr, text);
128 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
131 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
133 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
136 t->WriteServ(std::string(formatbuffer));
140 void InspIRCd::WriteMode(const char* modes, int flags, const char* text, ...)
142 char textbuffer[MAXBUF];
146 if (!text || !modes || !flags)
148 this->Log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
152 va_start(argsPtr, text);
153 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
155 modelen = strlen(modes);
159 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
162 bool send_to_user = true;
164 for (int n = 0; n < modelen; n++)
166 if (!t->IsModeSet(modes[n]))
168 send_to_user = false;
174 t->WriteServ("NOTICE %s :%s", t->nick, textbuffer);
178 else if (flags == WM_OR)
180 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
183 bool send_to_user = false;
185 for (int n = 0; n < modelen; n++)
187 if (t->IsModeSet(modes[n]))
196 t->WriteServ("NOTICE %s :%s", t->nick, textbuffer);
202 /* Find a user record by nickname and return a pointer to it */
203 User* InspIRCd::FindNick(const std::string &nick)
205 if (!nick.empty() && isdigit(*nick.begin()))
206 return FindUUID(nick);
208 user_hash::iterator iter = clientlist->find(nick);
210 if (iter == clientlist->end())
211 /* Couldn't find it */
217 User* InspIRCd::FindNick(const char* nick)
220 return FindUUID(nick);
222 user_hash::iterator iter = clientlist->find(nick);
224 if (iter == clientlist->end())
230 User* InspIRCd::FindNickOnly(const std::string &nick)
232 user_hash::iterator iter = clientlist->find(nick);
234 if (iter == clientlist->end())
240 User* InspIRCd::FindNickOnly(const char* nick)
242 user_hash::iterator iter = clientlist->find(nick);
244 if (iter == clientlist->end())
250 User *InspIRCd::FindUUID(const std::string &uid)
252 return FindUUID(uid.c_str());
255 User *InspIRCd::FindUUID(const char *uid)
257 user_hash::iterator finduuid = uuidlist->find(uid);
259 if (finduuid == uuidlist->end())
262 return finduuid->second;
265 /* find a channel record by channel name and return a pointer to it */
266 Channel* InspIRCd::FindChan(const char* chan)
268 chan_hash::iterator iter = chanlist->find(chan);
270 if (iter == chanlist->end())
271 /* Couldn't find it */
277 Channel* InspIRCd::FindChan(const std::string &chan)
279 chan_hash::iterator iter = chanlist->find(chan);
281 if (iter == chanlist->end())
282 /* Couldn't find it */
288 /* Send an error notice to all users, registered or not */
289 void InspIRCd::SendError(const std::string &s)
291 for (std::vector<User*>::const_iterator i = this->local_users.begin(); i != this->local_users.end(); i++)
293 if ((*i)->registered == REG_ALL)
295 (*i)->WriteServ("NOTICE %s :%s",(*i)->nick,s.c_str());
299 /* Unregistered connections receive ERROR, not a NOTICE */
300 (*i)->Write("ERROR :" + s);
302 /* This might generate a whole load of EAGAIN, but we dont really
303 * care about this, as if we call SendError something catastrophic
304 * has occured anyway, and we wont receive the events for these.
306 (*i)->FlushWriteBuf();
310 /* return how many users have a given mode e.g. 'a' */
311 int InspIRCd::ModeCount(const char mode)
313 ModeHandler* mh = this->Modes->FindMode(mode, MODETYPE_USER);
316 return mh->GetCount();
321 /* return channel count */
322 long InspIRCd::ChannelCount()
324 return chanlist->size();
327 bool InspIRCd::IsValidMask(const std::string &mask)
329 char* dest = (char*)mask.c_str();
333 for (char* i = dest; *i; i++)
335 /* out of range character, bad mask */
336 if (*i < 32 || *i > 126)
352 /* valid masks only have 1 ! and @ */
353 if (exclamation != 1 || atsign != 1)
359 /* true for valid channel name, false else */
360 bool InspIRCd::IsChannel(const char *chname)
364 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
365 if (!chname || *chname != '#')
370 c = (char *)chname + 1;
384 /* too long a name - note funky pointer arithmetic here. */
385 if ((c - chname) > CHANMAX)
393 /* true for valid nickname, false else */
394 bool IsNickHandler::Call(const char* n)
400 for (char* i = (char*)n; *i; i++, p++)
402 if ((*i >= 'A') && (*i <= '}'))
404 /* "A"-"}" can occur anywhere in a nickname */
408 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
410 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
414 /* invalid character! abort */
418 /* too long? or not -- pointer arithmetic rocks */
419 return (p < NICKMAX - 1);
422 /* return true for good ident, false else */
423 bool IsIdentHandler::Call(const char* n)
428 for (char* i = (char*)n; *i; i++)
430 if ((*i >= 'A') && (*i <= '}'))
435 if (((*i >= '0') && (*i <= '9')) || (*i == '-') || (*i == '.'))
446 bool InspIRCd::IsSID(const std::string &str)
448 /* Returns true if the string given is exactly 3 characters long,
449 * starts with a digit, and the other two characters are A-Z or digits
451 return ((str.length() == 3) && isdigit(str[0]) &&
452 ((str[1] >= 'A' && str[1] <= 'Z') || isdigit(str[1])) &&
453 ((str[2] >= 'A' && str[2] <= 'Z') || isdigit(str[2])));
456 /* open the proper logfile */
457 bool InspIRCd::OpenLog(char**, int)
459 Config->MyDir = Config->GetFullProgDir();
461 if (!*this->LogFileName)
463 if (Config->logpath.empty())
465 Config->logpath = Config->MyDir + "/ircd.log";
468 Config->log_file = fopen(Config->logpath.c_str(),"a+");
472 Config->log_file = fopen(this->LogFileName,"a+");
475 if (!Config->log_file)
481 this->Logger = new FileLogger(this, Config->log_file);
485 void InspIRCd::CheckRoot()
489 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
490 this->Log(DEFAULT,"Cant start as root");
491 Exit(EXIT_STATUS_ROOT);
495 void InspIRCd::CheckDie()
497 if (*Config->DieValue)
499 printf("WARNING: %s\n\n",Config->DieValue);
500 this->Log(DEFAULT,"Died because of <die> tag: %s",Config->DieValue);
501 Exit(EXIT_STATUS_DIETAG);
505 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const std::string &text)
507 std::string copy_text = text;
510 FOREACH_RESULT_I(this, I_OnWhoisLine, OnWhoisLine(user, dest, numeric, copy_text));
513 user->WriteServ("%d %s", numeric, copy_text.c_str());
516 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const char* format, ...)
518 char textbuffer[MAXBUF];
520 va_start (argsPtr, format);
521 vsnprintf(textbuffer, MAXBUF, format, argsPtr);
524 this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));
527 /** Refactored by Brain, Jun 2008. Much faster with some clever O(1) array
528 * lookups and pointer maths.
530 long InspIRCd::Duration(const std::string &str)
532 unsigned char multiplier = 0;
537 /* Iterate each item in the string, looking for number or multiplier */
538 for (std::string::const_reverse_iterator i = str.rbegin(); i != str.rend(); ++i)
540 /* Found a number, queue it onto the current number */
541 if ((*i >= '0') && (*i <= '9'))
543 subtotal = subtotal + ((*i - '0') * times);
548 /* Found something thats not a number, find out how much
549 * it multiplies the built up number by, multiply the total
550 * and reset the built up number.
553 total += subtotal * duration_multi[multiplier];
555 /* Next subtotal please */
563 total += subtotal * duration_multi[multiplier];
566 /* Any trailing values built up are treated as raw seconds */
567 return total + subtotal;
570 bool InspIRCd::ULine(const char* server)
577 return (Config->ulines.find(server) != Config->ulines.end());
580 bool InspIRCd::SilentULine(const char* server)
582 std::map<irc::string,bool>::iterator n = Config->ulines.find(server);
583 if (n != Config->ulines.end())
588 std::string InspIRCd::TimeString(time_t curtime)
590 return std::string(ctime(&curtime),24);