1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
18 #include "exitcodes.h"
20 std::string InspIRCd::GetServerDescription(const char* servername)
22 std::string description;
24 FOREACH_MOD_I(this,I_OnGetServerDescription,OnGetServerDescription(servername,description));
26 if (!description.empty())
32 // not a remote server that can be found, it must be me.
33 return Config->ServerDesc;
37 /* Find a user record by nickname and return a pointer to it */
38 User* InspIRCd::FindNick(const std::string &nick)
40 if (!nick.empty() && isdigit(*nick.begin()))
41 return FindUUID(nick);
43 user_hash::iterator iter = this->Users->clientlist->find(nick);
45 if (iter == this->Users->clientlist->end())
46 /* Couldn't find it */
52 User* InspIRCd::FindNick(const char* nick)
55 return FindUUID(nick);
57 user_hash::iterator iter = this->Users->clientlist->find(nick);
59 if (iter == this->Users->clientlist->end())
65 User* InspIRCd::FindNickOnly(const std::string &nick)
67 user_hash::iterator iter = this->Users->clientlist->find(nick);
69 if (iter == this->Users->clientlist->end())
75 User* InspIRCd::FindNickOnly(const char* nick)
77 user_hash::iterator iter = this->Users->clientlist->find(nick);
79 if (iter == this->Users->clientlist->end())
85 User *InspIRCd::FindUUID(const std::string &uid)
87 return FindUUID(uid.c_str());
90 User *InspIRCd::FindUUID(const char *uid)
92 user_hash::iterator finduuid = this->Users->uuidlist->find(uid);
94 if (finduuid == this->Users->uuidlist->end())
97 return finduuid->second;
100 /* find a channel record by channel name and return a pointer to it */
101 Channel* InspIRCd::FindChan(const char* chan)
103 chan_hash::iterator iter = chanlist->find(chan);
105 if (iter == chanlist->end())
106 /* Couldn't find it */
112 Channel* InspIRCd::FindChan(const std::string &chan)
114 chan_hash::iterator iter = chanlist->find(chan);
116 if (iter == chanlist->end())
117 /* Couldn't find it */
123 /* Send an error notice to all users, registered or not */
124 void InspIRCd::SendError(const std::string &s)
126 for (std::vector<User*>::const_iterator i = this->Users->local_users.begin(); i != this->Users->local_users.end(); i++)
128 if ((*i)->registered == REG_ALL)
130 (*i)->WriteServ("NOTICE %s :%s",(*i)->nick.c_str(),s.c_str());
134 /* Unregistered connections receive ERROR, not a NOTICE */
135 (*i)->Write("ERROR :" + s);
137 /* This might generate a whole load of EAGAIN, but we dont really
138 * care about this, as if we call SendError something catastrophic
139 * has occured anyway, and we wont receive the events for these.
141 (*i)->FlushWriteBuf();
145 /* return channel count */
146 long InspIRCd::ChannelCount()
148 return chanlist->size();
151 bool InspIRCd::IsValidMask(const std::string &mask)
153 const char* dest = mask.c_str();
157 for (const char* i = dest; *i; i++)
159 /* out of range character, bad mask */
160 if (*i < 32 || *i > 126)
176 /* valid masks only have 1 ! and @ */
177 if (exclamation != 1 || atsign != 1)
183 /* true for valid channel name, false else */
184 bool IsChannelHandler::Call(const char *chname, size_t max)
186 const char *c = chname + 1;
188 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
189 if (!chname || *chname != '#')
207 size_t len = c - chname;
208 /* too long a name - note funky pointer arithmetic here. */
217 /* true for valid nickname, false else */
218 bool IsNickHandler::Call(const char* n, size_t max)
224 for (const char* i = n; *i; i++, p++)
226 if ((*i >= 'A') && (*i <= '}'))
228 /* "A"-"}" can occur anywhere in a nickname */
232 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
234 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
238 /* invalid character! abort */
242 /* too long? or not -- pointer arithmetic rocks */
246 /* return true for good ident, false else */
247 bool IsIdentHandler::Call(const char* n)
252 for (const char* i = n; *i; i++)
254 if ((*i >= 'A') && (*i <= '}'))
259 if (((*i >= '0') && (*i <= '9')) || (*i == '-') || (*i == '.'))
270 bool IsSIDHandler::Call(const std::string &str)
272 /* Returns true if the string given is exactly 3 characters long,
273 * starts with a digit, and the other two characters are A-Z or digits
275 return ((str.length() == 3) && isdigit(str[0]) &&
276 ((str[1] >= 'A' && str[1] <= 'Z') || isdigit(str[1])) &&
277 ((str[2] >= 'A' && str[2] <= 'Z') || isdigit(str[2])));
280 /* open the proper logfile */
281 bool InspIRCd::OpenLog(char**, int)
283 /* This function only happens at startup now */
286 this->Logs->SetupNoFork();
288 Config->MyDir = Config->GetFullProgDir();
290 /* Attempt to find home directory, portable to windows */
291 const char* home = getenv("HOME");
294 /* No $HOME, log to %USERPROFILE% */
295 home = getenv("USERPROFILE");
298 /* Nothing could be found at all, log to current dir */
299 Config->logpath = "./startup.log";
303 if (!Config->writelog) return true; // Skip opening default log if -nolog
305 if (!*this->LogFileName)
307 if (Config->logpath.empty())
309 Config->logpath = "./startup.log";
312 if (!Config->log_file)
313 Config->log_file = fopen(Config->logpath.c_str(),"a+");
317 Config->log_file = fopen(this->LogFileName,"a+");
320 if (!Config->log_file)
325 FileWriter* fw = new FileWriter(this, Config->log_file);
326 FileLogStream *f = new FileLogStream(this, (Config->forcedebug ? DEBUG : DEFAULT), fw);
328 this->Logs->AddLogType("*", f, true);
333 void InspIRCd::CheckRoot()
337 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
338 this->Logs->Log("STARTUP",DEFAULT,"Cant start as root");
339 Exit(EXIT_STATUS_ROOT);
343 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const std::string &text)
345 std::string copy_text = text;
347 ModResult MOD_RESULT;
348 FIRST_MOD_RESULT(this, OnWhoisLine, MOD_RESULT, (user, dest, numeric, copy_text));
351 user->WriteServ("%d %s", numeric, copy_text.c_str());
354 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const char* format, ...)
356 char textbuffer[MAXBUF];
358 va_start (argsPtr, format);
359 vsnprintf(textbuffer, MAXBUF, format, argsPtr);
362 this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));
365 /** Refactored by Brain, Jun 2009. Much faster with some clever O(1) array
366 * lookups and pointer maths.
368 long InspIRCd::Duration(const std::string &str)
370 unsigned char multiplier = 0;
375 /* Iterate each item in the string, looking for number or multiplier */
376 for (std::string::const_reverse_iterator i = str.rbegin(); i != str.rend(); ++i)
378 /* Found a number, queue it onto the current number */
379 if ((*i >= '0') && (*i <= '9'))
381 subtotal = subtotal + ((*i - '0') * times);
386 /* Found something thats not a number, find out how much
387 * it multiplies the built up number by, multiply the total
388 * and reset the built up number.
391 total += subtotal * duration_multi[multiplier];
393 /* Next subtotal please */
401 total += subtotal * duration_multi[multiplier];
404 /* Any trailing values built up are treated as raw seconds */
405 return total + subtotal;
408 bool InspIRCd::ULine(const char* sserver)
415 return (Config->ulines.find(sserver) != Config->ulines.end());
418 bool InspIRCd::SilentULine(const char* sserver)
420 std::map<irc::string,bool>::iterator n = Config->ulines.find(sserver);
421 if (n != Config->ulines.end())
426 std::string InspIRCd::TimeString(time_t curtime)
428 return std::string(ctime(&curtime),24);
431 // You should only pass a single character to this.
432 void InspIRCd::AddExtBanChar(char c)
434 std::string &tok = Config->data005;
435 std::string::size_type ebpos;
437 if ((ebpos = tok.find(" EXTBAN=,")) == std::string::npos)
439 tok.append(" EXTBAN=,");
443 tok.insert(ebpos + 9, 1, c);