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 std::string& servername)
22 std::string description;
24 FOREACH_MOD(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<LocalUser*>::const_iterator i = this->Users->local_users.begin(); i != this->Users->local_users.end(); i++)
129 if (u->registered == REG_ALL)
131 u->WriteServ("NOTICE %s :%s",u->nick.c_str(),s.c_str());
135 /* Unregistered connections receive ERROR, not a NOTICE */
136 u->Write("ERROR :" + s);
141 /* return channel count */
142 long InspIRCd::ChannelCount()
144 return chanlist->size();
147 bool InspIRCd::IsValidMask(const std::string &mask)
149 const char* dest = mask.c_str();
153 for (const char* i = dest; *i; i++)
155 /* out of range character, bad mask */
156 if (*i < 32 || *i > 126)
172 /* valid masks only have 1 ! and @ */
173 if (exclamation != 1 || atsign != 1)
179 /* true for valid channel name, false else */
180 bool IsChannelHandler::Call(const char *chname, size_t max)
182 const char *c = chname + 1;
184 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
185 if (!chname || *chname != '#')
203 size_t len = c - chname;
204 /* too long a name - note funky pointer arithmetic here. */
213 /* true for valid nickname, false else */
214 bool IsNickHandler::Call(const char* n, size_t max)
220 for (const char* i = n; *i; i++, p++)
222 if ((*i >= 'A') && (*i <= '}'))
224 /* "A"-"}" can occur anywhere in a nickname */
228 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
230 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
234 /* invalid character! abort */
238 /* too long? or not -- pointer arithmetic rocks */
242 /* return true for good ident, false else */
243 bool IsIdentHandler::Call(const char* n)
248 for (const char* i = n; *i; i++)
250 if ((*i >= 'A') && (*i <= '}'))
255 if (((*i >= '0') && (*i <= '9')) || (*i == '-') || (*i == '.'))
266 bool IsSIDHandler::Call(const std::string &str)
268 /* Returns true if the string given is exactly 3 characters long,
269 * starts with a digit, and the other two characters are A-Z or digits
271 return ((str.length() == 3) && isdigit(str[0]) &&
272 ((str[1] >= 'A' && str[1] <= 'Z') || isdigit(str[1])) &&
273 ((str[2] >= 'A' && str[2] <= 'Z') || isdigit(str[2])));
276 /* open the proper logfile */
277 bool InspIRCd::OpenLog(char**, int)
279 if (!Config->cmdline.writelog) return true; // Skip opening default log if -nolog
281 if (Config->cmdline.startup_log.empty())
282 Config->cmdline.startup_log = "logs/startup.log";
283 FILE* startup = fopen(Config->cmdline.startup_log.c_str(), "a+");
290 FileWriter* fw = new FileWriter(startup);
291 FileLogStream *f = new FileLogStream((Config->cmdline.forcedebug ? DEBUG : DEFAULT), fw);
293 this->Logs->AddLogType("*", f, true);
298 void InspIRCd::CheckRoot()
302 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
303 this->Logs->Log("STARTUP",DEFAULT,"Cant start as root");
304 Exit(EXIT_STATUS_ROOT);
308 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const std::string &text)
310 std::string copy_text = text;
312 ModResult MOD_RESULT;
313 FIRST_MOD_RESULT(OnWhoisLine, MOD_RESULT, (user, dest, numeric, copy_text));
315 if (MOD_RESULT != MOD_RES_DENY)
316 user->WriteServ("%d %s", numeric, copy_text.c_str());
319 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const char* format, ...)
321 char textbuffer[MAXBUF];
323 va_start (argsPtr, format);
324 vsnprintf(textbuffer, MAXBUF, format, argsPtr);
327 this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));
330 /** Refactored by Brain, Jun 2009. Much faster with some clever O(1) array
331 * lookups and pointer maths.
333 long InspIRCd::Duration(const std::string &str)
335 unsigned char multiplier = 0;
340 /* Iterate each item in the string, looking for number or multiplier */
341 for (std::string::const_reverse_iterator i = str.rbegin(); i != str.rend(); ++i)
343 /* Found a number, queue it onto the current number */
344 if ((*i >= '0') && (*i <= '9'))
346 subtotal = subtotal + ((*i - '0') * times);
351 /* Found something thats not a number, find out how much
352 * it multiplies the built up number by, multiply the total
353 * and reset the built up number.
356 total += subtotal * duration_multi[multiplier];
358 /* Next subtotal please */
366 total += subtotal * duration_multi[multiplier];
369 /* Any trailing values built up are treated as raw seconds */
370 return total + subtotal;
373 bool InspIRCd::ULine(const std::string& sserver)
378 return (Config->ulines.find(sserver.c_str()) != Config->ulines.end());
381 bool InspIRCd::SilentULine(const std::string& sserver)
383 std::map<irc::string,bool>::iterator n = Config->ulines.find(sserver.c_str());
384 if (n != Config->ulines.end())
390 std::string InspIRCd::TimeString(time_t curtime)
392 return std::string(ctime(&curtime),24);
395 // You should only pass a single character to this.
396 void InspIRCd::AddExtBanChar(char c)
398 std::string &tok = Config->data005;
399 std::string::size_type ebpos;
401 if ((ebpos = tok.find(" EXTBAN=,")) == std::string::npos)
403 tok.append(" EXTBAN=,");
407 tok.insert(ebpos + 9, 1, c);