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 */
19 #include "exitcodes.h"
21 std::string InspIRCd::GetServerDescription(const char* servername)
23 std::string description;
25 FOREACH_MOD_I(this,I_OnGetServerDescription,OnGetServerDescription(servername,description));
27 if (!description.empty())
33 // not a remote server that can be found, it must be me.
34 return Config->ServerDesc;
38 /* Find a user record by nickname and return a pointer to it */
39 User* InspIRCd::FindNick(const std::string &nick)
41 if (!nick.empty() && isdigit(*nick.begin()))
42 return FindUUID(nick);
44 user_hash::iterator iter = this->Users->clientlist->find(nick);
46 if (iter == this->Users->clientlist->end())
47 /* Couldn't find it */
53 User* InspIRCd::FindNick(const char* nick)
56 return FindUUID(nick);
58 user_hash::iterator iter = this->Users->clientlist->find(nick);
60 if (iter == this->Users->clientlist->end())
66 User* InspIRCd::FindNickOnly(const std::string &nick)
68 user_hash::iterator iter = this->Users->clientlist->find(nick);
70 if (iter == this->Users->clientlist->end())
76 User* InspIRCd::FindNickOnly(const char* nick)
78 user_hash::iterator iter = this->Users->clientlist->find(nick);
80 if (iter == this->Users->clientlist->end())
86 User *InspIRCd::FindUUID(const std::string &uid)
88 return FindUUID(uid.c_str());
91 User *InspIRCd::FindUUID(const char *uid)
93 user_hash::iterator finduuid = this->Users->uuidlist->find(uid);
95 if (finduuid == this->Users->uuidlist->end())
98 return finduuid->second;
101 /* find a channel record by channel name and return a pointer to it */
102 Channel* InspIRCd::FindChan(const char* chan)
104 chan_hash::iterator iter = chanlist->find(chan);
106 if (iter == chanlist->end())
107 /* Couldn't find it */
113 Channel* InspIRCd::FindChan(const std::string &chan)
115 chan_hash::iterator iter = chanlist->find(chan);
117 if (iter == chanlist->end())
118 /* Couldn't find it */
124 /* Send an error notice to all users, registered or not */
125 void InspIRCd::SendError(const std::string &s)
127 for (std::vector<User*>::const_iterator i = this->Users->local_users.begin(); i != this->Users->local_users.end(); i++)
129 if ((*i)->registered == REG_ALL)
131 (*i)->WriteServ("NOTICE %s :%s",(*i)->nick,s.c_str());
135 /* Unregistered connections receive ERROR, not a NOTICE */
136 (*i)->Write("ERROR :" + s);
138 /* This might generate a whole load of EAGAIN, but we dont really
139 * care about this, as if we call SendError something catastrophic
140 * has occured anyway, and we wont receive the events for these.
142 (*i)->FlushWriteBuf();
146 /* return channel count */
147 long InspIRCd::ChannelCount()
149 return chanlist->size();
152 bool InspIRCd::IsValidMask(const std::string &mask)
154 char* dest = (char*)mask.c_str();
158 for (char* i = dest; *i; i++)
160 /* out of range character, bad mask */
161 if (*i < 32 || *i > 126)
177 /* valid masks only have 1 ! and @ */
178 if (exclamation != 1 || atsign != 1)
184 /* true for valid channel name, false else */
185 bool IsChannelHandler::Call(const char *chname)
189 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
190 if (!chname || *chname != '#')
195 c = (char *)chname + 1;
209 /* too long a name - note funky pointer arithmetic here. */
210 if ((c - chname) > CHANMAX)
218 /* true for valid nickname, false else */
219 bool IsNickHandler::Call(const char* n)
225 for (char* i = (char*)n; *i; i++, p++)
227 if ((*i >= 'A') && (*i <= '}'))
229 /* "A"-"}" can occur anywhere in a nickname */
233 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
235 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
239 /* invalid character! abort */
243 /* too long? or not -- pointer arithmetic rocks */
244 return (p < NICKMAX - 1);
247 /* return true for good ident, false else */
248 bool IsIdentHandler::Call(const char* n)
253 for (char* i = (char*)n; *i; i++)
255 if ((*i >= 'A') && (*i <= '}'))
260 if (((*i >= '0') && (*i <= '9')) || (*i == '-') || (*i == '.'))
271 bool IsSIDHandler::Call(const std::string &str)
273 /* Returns true if the string given is exactly 3 characters long,
274 * starts with a digit, and the other two characters are A-Z or digits
276 return ((str.length() == 3) && isdigit(str[0]) &&
277 ((str[1] >= 'A' && str[1] <= 'Z') || isdigit(str[1])) &&
278 ((str[2] >= 'A' && str[2] <= 'Z') || isdigit(str[2])));
281 /* open the proper logfile */
282 bool InspIRCd::OpenLog(char**, int)
284 /* This function only happens at startup now */
287 this->Logs->SetupNoFork();
289 Config->MyDir = Config->GetFullProgDir();
291 /* Attempt to find home directory, portable to windows */
292 const char* home = getenv("HOME");
295 /* No $HOME, log to %USERPROFILE% */
296 home = getenv("USERPROFILE");
299 /* Nothing could be found at all, log to current dir */
300 Config->logpath = "./startup.log";
304 if (!Config->writelog) return true; // Skip opening default log if -nolog
306 if (!*this->LogFileName)
308 if (Config->logpath.empty())
310 std::string path = std::string(home) + "/.inspircd";
311 // This tries to create the ~/.inspircd. If it succeeds, then we go ahead and use it.
312 // If it fails due to an existing target, then we use it anyway.
313 // Either way, we make sure we can get write access to the log at this point.
314 if (!mkdir(path.c_str(), 0700) || errno == EEXIST)
316 /* Log to ~/.inspircd/ircd.log */
317 Config->logpath = path + "/startup.log";
318 FILE* fd = fopen(Config->logpath.c_str(), "a+");
321 // Could not get write access... Why?
322 if (errno == ENOTDIR)
323 // ~/.inspircd is not actually a directory!
324 printf("\nWARNING: Unable to create directory: %s (Exists and is not a directory)\n", path.c_str());
326 // Not writable for some other reason (no +w access, readonly fs, file too big, whatever).
327 printf("\nWARNING: No write access to %s (%s)\n", Config->logpath.c_str(), strerror(errno));
328 Config->logpath = "./startup.log";
332 Config->log_file = fd;
337 /* Couldn't make ~/.inspircd directory, log to current dir */
338 Config->logpath = "./startup.log";
339 printf("\nWARNING: Unable to create directory: %s (%s)\n", path.c_str(), strerror(errno));
343 if (!Config->log_file)
344 Config->log_file = fopen(Config->logpath.c_str(),"a+");
348 Config->log_file = fopen(this->LogFileName,"a+");
351 if (!Config->log_file)
356 FileWriter* fw = new FileWriter(this, Config->log_file);
357 FileLogStream *f = new FileLogStream(this, (Config->forcedebug ? DEBUG : DEFAULT), fw);
359 this->Logs->AddLogType("*", f, true);
364 void InspIRCd::CheckRoot()
368 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
369 this->Logs->Log("STARTUP",DEFAULT,"Cant start as root");
370 Exit(EXIT_STATUS_ROOT);
374 void InspIRCd::CheckDie()
376 if (*Config->DieValue)
378 printf("WARNING: %s\n\n",Config->DieValue);
379 this->Logs->Log("CONFIG",DEFAULT,"Died because of <die> tag: %s",Config->DieValue);
380 Exit(EXIT_STATUS_DIETAG);
384 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const std::string &text)
386 std::string copy_text = text;
389 FOREACH_RESULT_I(this, I_OnWhoisLine, OnWhoisLine(user, dest, numeric, copy_text));
392 user->WriteServ("%d %s", numeric, copy_text.c_str());
395 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const char* format, ...)
397 char textbuffer[MAXBUF];
399 va_start (argsPtr, format);
400 vsnprintf(textbuffer, MAXBUF, format, argsPtr);
403 this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));
406 /** Refactored by Brain, Jun 2008. Much faster with some clever O(1) array
407 * lookups and pointer maths.
409 long InspIRCd::Duration(const std::string &str)
411 unsigned char multiplier = 0;
416 /* Iterate each item in the string, looking for number or multiplier */
417 for (std::string::const_reverse_iterator i = str.rbegin(); i != str.rend(); ++i)
419 /* Found a number, queue it onto the current number */
420 if ((*i >= '0') && (*i <= '9'))
422 subtotal = subtotal + ((*i - '0') * times);
427 /* Found something thats not a number, find out how much
428 * it multiplies the built up number by, multiply the total
429 * and reset the built up number.
432 total += subtotal * duration_multi[multiplier];
434 /* Next subtotal please */
442 total += subtotal * duration_multi[multiplier];
445 /* Any trailing values built up are treated as raw seconds */
446 return total + subtotal;
449 bool InspIRCd::ULine(const char* sserver)
456 return (Config->ulines.find(sserver) != Config->ulines.end());
459 bool InspIRCd::SilentULine(const char* sserver)
461 std::map<irc::string,bool>::iterator n = Config->ulines.find(sserver);
462 if (n != Config->ulines.end())
467 std::string InspIRCd::TimeString(time_t curtime)
469 return std::string(ctime(&curtime),24);