]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/helperfuncs.cpp
Resolve /STATS S conflict between SVSHOLD and SHUN
[user/henk/code/inspircd.git] / src / helperfuncs.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core */
15
16 #include "inspircd.h"
17 #include "xline.h"
18 #include "exitcodes.h"
19
20 std::string InspIRCd::GetServerDescription(const std::string& servername)
21 {
22         std::string description;
23
24         FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
25
26         if (!description.empty())
27         {
28                 return description;
29         }
30         else
31         {
32                 // not a remote server that can be found, it must be me.
33                 return Config->ServerDesc;
34         }
35 }
36
37 /* Find a user record by nickname and return a pointer to it */
38 User* InspIRCd::FindNick(const std::string &nick)
39 {
40         if (!nick.empty() && isdigit(*nick.begin()))
41                 return FindUUID(nick);
42
43         user_hash::iterator iter = this->Users->clientlist->find(nick);
44
45         if (iter == this->Users->clientlist->end())
46                 /* Couldn't find it */
47                 return NULL;
48
49         return iter->second;
50 }
51
52 User* InspIRCd::FindNick(const char* nick)
53 {
54         if (isdigit(*nick))
55                 return FindUUID(nick);
56
57         user_hash::iterator iter = this->Users->clientlist->find(nick);
58
59         if (iter == this->Users->clientlist->end())
60                 return NULL;
61
62         return iter->second;
63 }
64
65 User* InspIRCd::FindNickOnly(const std::string &nick)
66 {
67         user_hash::iterator iter = this->Users->clientlist->find(nick);
68
69         if (iter == this->Users->clientlist->end())
70                 return NULL;
71
72         return iter->second;
73 }
74
75 User* InspIRCd::FindNickOnly(const char* nick)
76 {
77         user_hash::iterator iter = this->Users->clientlist->find(nick);
78
79         if (iter == this->Users->clientlist->end())
80                 return NULL;
81
82         return iter->second;
83 }
84
85 User *InspIRCd::FindUUID(const std::string &uid)
86 {
87         return FindUUID(uid.c_str());
88 }
89
90 User *InspIRCd::FindUUID(const char *uid)
91 {
92         user_hash::iterator finduuid = this->Users->uuidlist->find(uid);
93
94         if (finduuid == this->Users->uuidlist->end())
95                 return NULL;
96
97         return finduuid->second;
98 }
99
100 /* find a channel record by channel name and return a pointer to it */
101 Channel* InspIRCd::FindChan(const char* chan)
102 {
103         chan_hash::iterator iter = chanlist->find(chan);
104
105         if (iter == chanlist->end())
106                 /* Couldn't find it */
107                 return NULL;
108
109         return iter->second;
110 }
111
112 Channel* InspIRCd::FindChan(const std::string &chan)
113 {
114         chan_hash::iterator iter = chanlist->find(chan);
115
116         if (iter == chanlist->end())
117                 /* Couldn't find it */
118                 return NULL;
119
120         return iter->second;
121 }
122
123 /* Send an error notice to all users, registered or not */
124 void InspIRCd::SendError(const std::string &s)
125 {
126         for (std::vector<LocalUser*>::const_iterator i = this->Users->local_users.begin(); i != this->Users->local_users.end(); i++)
127         {
128                 User* u = *i;
129                 if (u->registered == REG_ALL)
130                 {
131                         u->WriteServ("NOTICE %s :%s",u->nick.c_str(),s.c_str());
132                 }
133                 else
134                 {
135                         /* Unregistered connections receive ERROR, not a NOTICE */
136                         u->Write("ERROR :" + s);
137                 }
138         }
139 }
140
141 /* return channel count */
142 long InspIRCd::ChannelCount()
143 {
144         return chanlist->size();
145 }
146
147 bool InspIRCd::IsValidMask(const std::string &mask)
148 {
149         const char* dest = mask.c_str();
150         int exclamation = 0;
151         int atsign = 0;
152
153         for (const char* i = dest; *i; i++)
154         {
155                 /* out of range character, bad mask */
156                 if (*i < 32 || *i > 126)
157                 {
158                         return false;
159                 }
160
161                 switch (*i)
162                 {
163                         case '!':
164                                 exclamation++;
165                                 break;
166                         case '@':
167                                 atsign++;
168                                 break;
169                 }
170         }
171
172         /* valid masks only have 1 ! and @ */
173         if (exclamation != 1 || atsign != 1)
174                 return false;
175
176         if (mask.length() > 250)
177                 return false;
178
179         return true;
180 }
181
182 /* true for valid channel name, false else */
183 bool IsChannelHandler::Call(const char *chname, size_t max)
184 {
185         const char *c = chname + 1;
186
187         /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
188         if (!chname || *chname != '#')
189         {
190                 return false;
191         }
192
193         while (*c)
194         {
195                 switch (*c)
196                 {
197                         case ' ':
198                         case ',':
199                         case 7:
200                                 return false;
201                 }
202
203                 c++;
204         }
205
206         size_t len = c - chname;
207         /* too long a name - note funky pointer arithmetic here. */
208         if (len > max)
209         {
210                         return false;
211         }
212
213         return true;
214 }
215
216 /* true for valid nickname, false else */
217 bool IsNickHandler::Call(const char* n, size_t max)
218 {
219         if (!n || !*n)
220                 return false;
221
222         unsigned int p = 0;
223         for (const char* i = n; *i; i++, p++)
224         {
225                 if ((*i >= 'A') && (*i <= '}'))
226                 {
227                         /* "A"-"}" can occur anywhere in a nickname */
228                         continue;
229                 }
230
231                 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
232                 {
233                         /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
234                         continue;
235                 }
236
237                 /* invalid character! abort */
238                 return false;
239         }
240
241         /* too long? or not -- pointer arithmetic rocks */
242         return (p < max);
243 }
244
245 /* return true for good ident, false else */
246 bool IsIdentHandler::Call(const char* n)
247 {
248         if (!n || !*n)
249                 return false;
250
251         for (const char* i = n; *i; i++)
252         {
253                 if ((*i >= 'A') && (*i <= '}'))
254                 {
255                         continue;
256                 }
257
258                 if (((*i >= '0') && (*i <= '9')) || (*i == '-') || (*i == '.'))
259                 {
260                         continue;
261                 }
262
263                 return false;
264         }
265
266         return true;
267 }
268
269 bool IsSIDHandler::Call(const std::string &str)
270 {
271         /* Returns true if the string given is exactly 3 characters long,
272          * starts with a digit, and the other two characters are A-Z or digits
273          */
274         return ((str.length() == 3) && isdigit(str[0]) &&
275                         ((str[1] >= 'A' && str[1] <= 'Z') || isdigit(str[1])) &&
276                          ((str[2] >= 'A' && str[2] <= 'Z') || isdigit(str[2])));
277 }
278
279 /* open the proper logfile */
280 bool InspIRCd::OpenLog(char**, int)
281 {
282         if (!Config->cmdline.writelog) return true; // Skip opening default log if -nolog
283
284         if (Config->cmdline.startup_log.empty())
285                 Config->cmdline.startup_log = "logs/startup.log";
286         FILE* startup = fopen(Config->cmdline.startup_log.c_str(), "a+");
287
288         if (!startup)
289         {
290                 return false;
291         }
292
293         FileWriter* fw = new FileWriter(startup);
294         FileLogStream *f = new FileLogStream((Config->cmdline.forcedebug ? DEBUG : DEFAULT), fw);
295
296         this->Logs->AddLogType("*", f, true);
297
298         return true;
299 }
300
301 void InspIRCd::CheckRoot()
302 {
303         if (geteuid() == 0)
304         {
305                 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
306                 this->Logs->Log("STARTUP",DEFAULT,"Cant start as root");
307                 Exit(EXIT_STATUS_ROOT);
308         }
309 }
310
311 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const std::string &text)
312 {
313         std::string copy_text = text;
314
315         ModResult MOD_RESULT;
316         FIRST_MOD_RESULT(OnWhoisLine, MOD_RESULT, (user, dest, numeric, copy_text));
317
318         if (MOD_RESULT != MOD_RES_DENY)
319                 user->WriteServ("%d %s", numeric, copy_text.c_str());
320 }
321
322 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const char* format, ...)
323 {
324         char textbuffer[MAXBUF];
325         va_list argsPtr;
326         va_start (argsPtr, format);
327         vsnprintf(textbuffer, MAXBUF, format, argsPtr);
328         va_end(argsPtr);
329
330         this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));
331 }
332
333 /** Refactored by Brain, Jun 2009. Much faster with some clever O(1) array
334  * lookups and pointer maths.
335  */
336 long InspIRCd::Duration(const std::string &str)
337 {
338         unsigned char multiplier = 0;
339         long total = 0;
340         long times = 1;
341         long subtotal = 0;
342
343         /* Iterate each item in the string, looking for number or multiplier */
344         for (std::string::const_reverse_iterator i = str.rbegin(); i != str.rend(); ++i)
345         {
346                 /* Found a number, queue it onto the current number */
347                 if ((*i >= '0') && (*i <= '9'))
348                 {
349                         subtotal = subtotal + ((*i - '0') * times);
350                         times = times * 10;
351                 }
352                 else
353                 {
354                         /* Found something thats not a number, find out how much
355                          * it multiplies the built up number by, multiply the total
356                          * and reset the built up number.
357                          */
358                         if (subtotal)
359                                 total += subtotal * duration_multi[multiplier];
360
361                         /* Next subtotal please */
362                         subtotal = 0;
363                         multiplier = *i;
364                         times = 1;
365                 }
366         }
367         if (multiplier)
368         {
369                 total += subtotal * duration_multi[multiplier];
370                 subtotal = 0;
371         }
372         /* Any trailing values built up are treated as raw seconds */
373         return total + subtotal;
374 }
375
376 bool InspIRCd::ULine(const std::string& sserver)
377 {
378         if (sserver.empty())
379                 return true;
380
381         return (Config->ulines.find(sserver.c_str()) != Config->ulines.end());
382 }
383
384 bool InspIRCd::SilentULine(const std::string& sserver)
385 {
386         std::map<irc::string,bool>::iterator n = Config->ulines.find(sserver.c_str());
387         if (n != Config->ulines.end())
388                 return n->second;
389         else
390                 return false;
391 }
392
393 std::string InspIRCd::TimeString(time_t curtime)
394 {
395         return std::string(ctime(&curtime),24);
396 }
397
398 // You should only pass a single character to this.
399 void InspIRCd::AddExtBanChar(char c)
400 {
401         std::string &tok = Config->data005;
402         std::string::size_type ebpos = tok.find(" EXTBAN=,");
403
404         if (ebpos == std::string::npos)
405         {
406                 tok.append(" EXTBAN=,");
407                 tok.push_back(c);
408         }
409         else
410         {
411                 ebpos += 9;
412                 while (isalpha(tok[ebpos]) && tok[ebpos] < c)
413                         ebpos++;
414                 tok.insert(ebpos, 1, c);
415         }
416 }
417
418 std::string InspIRCd::GenRandomStr(int length, bool printable)
419 {
420         char* buf = new char[length];
421         GenRandom(buf, length);
422         std::string rv;
423         rv.resize(length);
424         for(int i=0; i < length; i++)
425                 rv[i] = printable ? 0x3F + (buf[i] & 0x3F) : buf[i];
426         delete[] buf;
427         return rv;
428 }
429
430 // NOTE: this has a slight bias for lower values if max is not a power of 2.
431 // Don't use it if that matters.
432 unsigned long InspIRCd::GenRandomInt(unsigned long max)
433 {
434         unsigned long rv;
435         GenRandom((char*)&rv, sizeof(rv));
436         return rv % max;
437 }
438
439 // This is overridden by a higher-quality algorithm when SSL support is loaded
440 void GenRandomHandler::Call(char *output, size_t max)
441 {
442         for(unsigned int i=0; i < max; i++)
443                 output[i] = random();
444 }
445
446 ModResult OnCheckExemptionHandler::Call(User* user, Channel* chan, const std::string& restriction)
447 {
448         unsigned int mypfx = chan->GetPrefixValue(user);
449         char minmode = 0;
450         std::string current;
451
452         irc::spacesepstream defaultstream(ServerInstance->Config->ConfValue("options")->getString("exemptchanops"));
453
454         while (defaultstream.GetToken(current))
455         {
456                 std::string::size_type pos = current.find(':');
457                 if (pos == std::string::npos)
458                         continue;
459                 if (current.substr(0,pos) == restriction)
460                         minmode = current[pos+1];
461         }
462
463         ModeHandler* mh = ServerInstance->Modes->FindMode(minmode, MODETYPE_CHANNEL);
464         if (mh && mypfx >= mh->GetPrefixRank())
465                 return MOD_RES_ALLOW;
466         if (mh || minmode == '*')
467                 return MOD_RES_DENY;
468         return MOD_RES_PASSTHRU;
469 }