]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/helperfuncs.cpp
80feae464c6e52468463de830163772c3d8a58be
[user/henk/code/inspircd.git] / src / helperfuncs.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 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 char* servername)
21 {
22         std::string description;
23
24         FOREACH_MOD_I(this,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<User*>::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         return true;
177 }
178
179 /* true for valid channel name, false else */
180 bool IsChannelHandler::Call(const char *chname, size_t max)
181 {
182         const char *c = chname + 1;
183
184         /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
185         if (!chname || *chname != '#')
186         {
187                 return false;
188         }
189
190         while (*c)
191         {
192                 switch (*c)
193                 {
194                         case ' ':
195                         case ',':
196                         case 7:
197                                 return false;
198                 }
199
200                 c++;
201         }
202
203         size_t len = c - chname;
204         /* too long a name - note funky pointer arithmetic here. */
205         if (len > max)
206         {
207                         return false;
208         }
209
210         return true;
211 }
212
213 /* true for valid nickname, false else */
214 bool IsNickHandler::Call(const char* n, size_t max)
215 {
216         if (!n || !*n)
217                 return false;
218
219         unsigned int p = 0;
220         for (const char* i = n; *i; i++, p++)
221         {
222                 if ((*i >= 'A') && (*i <= '}'))
223                 {
224                         /* "A"-"}" can occur anywhere in a nickname */
225                         continue;
226                 }
227
228                 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
229                 {
230                         /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
231                         continue;
232                 }
233
234                 /* invalid character! abort */
235                 return false;
236         }
237
238         /* too long? or not -- pointer arithmetic rocks */
239         return (p < max);
240 }
241
242 /* return true for good ident, false else */
243 bool IsIdentHandler::Call(const char* n)
244 {
245         if (!n || !*n)
246                 return false;
247
248         for (const char* i = n; *i; i++)
249         {
250                 if ((*i >= 'A') && (*i <= '}'))
251                 {
252                         continue;
253                 }
254
255                 if (((*i >= '0') && (*i <= '9')) || (*i == '-') || (*i == '.'))
256                 {
257                         continue;
258                 }
259
260                 return false;
261         }
262
263         return true;
264 }
265
266 bool IsSIDHandler::Call(const std::string &str)
267 {
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
270          */
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])));
274 }
275
276 /* open the proper logfile */
277 bool InspIRCd::OpenLog(char**, int)
278 {
279         /* This function only happens at startup now */
280         if (Config->nofork)
281         {
282                 this->Logs->SetupNoFork();
283         }
284         Config->MyDir = Config->GetFullProgDir();
285
286         /* Attempt to find home directory, portable to windows */
287         const char* home = getenv("HOME");
288         if (!home)
289         {
290                 /* No $HOME, log to %USERPROFILE% */
291                 home = getenv("USERPROFILE");
292                 if (!home)
293                 {
294                         /* Nothing could be found at all, log to current dir */
295                         Config->logpath = "./startup.log";
296                 }
297         }
298
299         if (!Config->writelog) return true; // Skip opening default log if -nolog
300
301         if (!*this->LogFileName)
302         {
303                 if (Config->logpath.empty())
304                 {
305                         Config->logpath = "./startup.log";
306                 }
307
308                 if (!Config->log_file)
309                         Config->log_file = fopen(Config->logpath.c_str(),"a+");
310         }
311         else
312         {
313                 Config->log_file = fopen(this->LogFileName,"a+");
314         }
315
316         if (!Config->log_file)
317         {
318                 return false;
319         }
320
321         FileWriter* fw = new FileWriter(this, Config->log_file);
322         FileLogStream *f = new FileLogStream(this, (Config->forcedebug ? DEBUG : DEFAULT), fw);
323
324         this->Logs->AddLogType("*", f, true);
325
326         return true;
327 }
328
329 void InspIRCd::CheckRoot()
330 {
331         if (geteuid() == 0)
332         {
333                 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
334                 this->Logs->Log("STARTUP",DEFAULT,"Cant start as root");
335                 Exit(EXIT_STATUS_ROOT);
336         }
337 }
338
339 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const std::string &text)
340 {
341         std::string copy_text = text;
342
343         ModResult MOD_RESULT;
344         FIRST_MOD_RESULT(this, OnWhoisLine, MOD_RESULT, (user, dest, numeric, copy_text));
345
346         if (!MOD_RESULT)
347                 user->WriteServ("%d %s", numeric, copy_text.c_str());
348 }
349
350 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const char* format, ...)
351 {
352         char textbuffer[MAXBUF];
353         va_list argsPtr;
354         va_start (argsPtr, format);
355         vsnprintf(textbuffer, MAXBUF, format, argsPtr);
356         va_end(argsPtr);
357
358         this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));
359 }
360
361 /** Refactored by Brain, Jun 2009. Much faster with some clever O(1) array
362  * lookups and pointer maths.
363  */
364 long InspIRCd::Duration(const std::string &str)
365 {
366         unsigned char multiplier = 0;
367         long total = 0;
368         long times = 1;
369         long subtotal = 0;
370
371         /* Iterate each item in the string, looking for number or multiplier */
372         for (std::string::const_reverse_iterator i = str.rbegin(); i != str.rend(); ++i)
373         {
374                 /* Found a number, queue it onto the current number */
375                 if ((*i >= '0') && (*i <= '9'))
376                 {
377                         subtotal = subtotal + ((*i - '0') * times);
378                         times = times * 10;
379                 }
380                 else
381                 {
382                         /* Found something thats not a number, find out how much
383                          * it multiplies the built up number by, multiply the total
384                          * and reset the built up number.
385                          */
386                         if (subtotal)
387                                 total += subtotal * duration_multi[multiplier];
388
389                         /* Next subtotal please */
390                         subtotal = 0;
391                         multiplier = *i;
392                         times = 1;
393                 }
394         }
395         if (multiplier)
396         {
397                 total += subtotal * duration_multi[multiplier];
398                 subtotal = 0;
399         }
400         /* Any trailing values built up are treated as raw seconds */
401         return total + subtotal;
402 }
403
404 bool InspIRCd::ULine(const char* sserver)
405 {
406         if (!sserver)
407                 return false;
408         if (!*sserver)
409                 return true;
410
411         return (Config->ulines.find(sserver) != Config->ulines.end());
412 }
413
414 bool InspIRCd::SilentULine(const char* sserver)
415 {
416         std::map<irc::string,bool>::iterator n = Config->ulines.find(sserver);
417         if (n != Config->ulines.end())
418                 return n->second;
419         else return false;
420 }
421
422 std::string InspIRCd::TimeString(time_t curtime)
423 {
424         return std::string(ctime(&curtime),24);
425 }
426
427 // You should only pass a single character to this.
428 void InspIRCd::AddExtBanChar(char c)
429 {
430         std::string &tok = Config->data005;
431         std::string::size_type ebpos;
432
433         if ((ebpos = tok.find(" EXTBAN=,")) == std::string::npos)
434         {
435                 tok.append(" EXTBAN=,");
436                 tok.push_back(c);
437         }
438         else
439                 tok.insert(ebpos + 9, 1, c);
440 }