]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/helperfuncs.cpp
Nuke trailing spaces
[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://www.inspircd.org/wiki/index.php/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                 if ((*i)->registered == REG_ALL)
129                 {
130                         (*i)->WriteServ("NOTICE %s :%s",(*i)->nick.c_str(),s.c_str());
131                 }
132                 else
133                 {
134                         /* Unregistered connections receive ERROR, not a NOTICE */
135                         (*i)->Write("ERROR :" + s);
136                 }
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.
140                  */
141                 (*i)->FlushWriteBuf();
142         }
143 }
144
145 /* return channel count */
146 long InspIRCd::ChannelCount()
147 {
148         return chanlist->size();
149 }
150
151 bool InspIRCd::IsValidMask(const std::string &mask)
152 {
153         char* dest = (char*)mask.c_str();
154         int exclamation = 0;
155         int atsign = 0;
156
157         for (char* i = dest; *i; i++)
158         {
159                 /* out of range character, bad mask */
160                 if (*i < 32 || *i > 126)
161                 {
162                         return false;
163                 }
164
165                 switch (*i)
166                 {
167                         case '!':
168                                 exclamation++;
169                                 break;
170                         case '@':
171                                 atsign++;
172                                 break;
173                 }
174         }
175
176         /* valid masks only have 1 ! and @ */
177         if (exclamation != 1 || atsign != 1)
178                 return false;
179
180         return true;
181 }
182
183 /* true for valid channel name, false else */
184 bool IsChannelHandler::Call(const char *chname, size_t max)
185 {
186         const char *c = chname + 1;
187
188         /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
189         if (!chname || *chname != '#')
190         {
191                 return false;
192         }
193
194         while (*c)
195         {
196                 switch (*c)
197                 {
198                         case ' ':
199                         case ',':
200                         case 7:
201                                 return false;
202                 }
203
204                 c++;
205         }
206
207         size_t len = c - chname;
208         /* too long a name - note funky pointer arithmetic here. */
209         if (len > max)
210         {
211                         return false;
212         }
213
214         return true;
215 }
216
217 /* true for valid nickname, false else */
218 bool IsNickHandler::Call(const char* n, size_t max)
219 {
220         if (!n || !*n)
221                 return false;
222
223         unsigned int p = 0;
224         for (const char* i = n; *i; i++, p++)
225         {
226                 if ((*i >= 'A') && (*i <= '}'))
227                 {
228                         /* "A"-"}" can occur anywhere in a nickname */
229                         continue;
230                 }
231
232                 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
233                 {
234                         /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
235                         continue;
236                 }
237
238                 /* invalid character! abort */
239                 return false;
240         }
241
242         /* too long? or not -- pointer arithmetic rocks */
243         return (p < max);
244 }
245
246 /* return true for good ident, false else */
247 bool IsIdentHandler::Call(const char* n)
248 {
249         if (!n || !*n)
250                 return false;
251
252         for (const char* i = n; *i; i++)
253         {
254                 if ((*i >= 'A') && (*i <= '}'))
255                 {
256                         continue;
257                 }
258
259                 if (((*i >= '0') && (*i <= '9')) || (*i == '-') || (*i == '.'))
260                 {
261                         continue;
262                 }
263
264                 return false;
265         }
266
267         return true;
268 }
269
270 bool IsSIDHandler::Call(const std::string &str)
271 {
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
274          */
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])));
278 }
279
280 /* open the proper logfile */
281 bool InspIRCd::OpenLog(char**, int)
282 {
283         /* This function only happens at startup now */
284         if (Config->nofork)
285         {
286                 this->Logs->SetupNoFork();
287         }
288         Config->MyDir = Config->GetFullProgDir();
289
290         /* Attempt to find home directory, portable to windows */
291         const char* home = getenv("HOME");
292         if (!home)
293         {
294                 /* No $HOME, log to %USERPROFILE% */
295                 home = getenv("USERPROFILE");
296                 if (!home)
297                 {
298                         /* Nothing could be found at all, log to current dir */
299                         Config->logpath = "./startup.log";
300                 }
301         }
302
303         if (!Config->writelog) return true; // Skip opening default log if -nolog
304
305         if (!*this->LogFileName)
306         {
307                 if (Config->logpath.empty())
308                 {
309                         Config->logpath = "./startup.log";
310                 }
311
312                 if (!Config->log_file)
313                         Config->log_file = fopen(Config->logpath.c_str(),"a+");
314         }
315         else
316         {
317                 Config->log_file = fopen(this->LogFileName,"a+");
318         }
319
320         if (!Config->log_file)
321         {
322                 return false;
323         }
324
325         FileWriter* fw = new FileWriter(this, Config->log_file);
326         FileLogStream *f = new FileLogStream(this, (Config->forcedebug ? DEBUG : DEFAULT), fw);
327
328         this->Logs->AddLogType("*", f, true);
329
330         return true;
331 }
332
333 void InspIRCd::CheckRoot()
334 {
335         if (geteuid() == 0)
336         {
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);
340         }
341 }
342
343 void InspIRCd::CheckDie()
344 {
345         if (*Config->DieValue)
346         {
347                 printf("WARNING: %s\n\n",Config->DieValue);
348                 this->Logs->Log("CONFIG",DEFAULT,"Died because of <die> tag: %s",Config->DieValue);
349                 Exit(EXIT_STATUS_DIETAG);
350         }
351 }
352
353 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const std::string &text)
354 {
355         std::string copy_text = text;
356
357         int MOD_RESULT = 0;
358         FOREACH_RESULT_I(this, I_OnWhoisLine, OnWhoisLine(user, dest, numeric, copy_text));
359
360         if (!MOD_RESULT)
361                 user->WriteServ("%d %s", numeric, copy_text.c_str());
362 }
363
364 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const char* format, ...)
365 {
366         char textbuffer[MAXBUF];
367         va_list argsPtr;
368         va_start (argsPtr, format);
369         vsnprintf(textbuffer, MAXBUF, format, argsPtr);
370         va_end(argsPtr);
371
372         this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));
373 }
374
375 /** Refactored by Brain, Jun 2009. Much faster with some clever O(1) array
376  * lookups and pointer maths.
377  */
378 long InspIRCd::Duration(const std::string &str)
379 {
380         unsigned char multiplier = 0;
381         long total = 0;
382         long times = 1;
383         long subtotal = 0;
384
385         /* Iterate each item in the string, looking for number or multiplier */
386         for (std::string::const_reverse_iterator i = str.rbegin(); i != str.rend(); ++i)
387         {
388                 /* Found a number, queue it onto the current number */
389                 if ((*i >= '0') && (*i <= '9'))
390                 {
391                         subtotal = subtotal + ((*i - '0') * times);
392                         times = times * 10;
393                 }
394                 else
395                 {
396                         /* Found something thats not a number, find out how much
397                          * it multiplies the built up number by, multiply the total
398                          * and reset the built up number.
399                          */
400                         if (subtotal)
401                                 total += subtotal * duration_multi[multiplier];
402
403                         /* Next subtotal please */
404                         subtotal = 0;
405                         multiplier = *i;
406                         times = 1;
407                 }
408         }
409         if (multiplier)
410         {
411                 total += subtotal * duration_multi[multiplier];
412                 subtotal = 0;
413         }
414         /* Any trailing values built up are treated as raw seconds */
415         return total + subtotal;
416 }
417
418 bool InspIRCd::ULine(const char* sserver)
419 {
420         if (!sserver)
421                 return false;
422         if (!*sserver)
423                 return true;
424
425         return (Config->ulines.find(sserver) != Config->ulines.end());
426 }
427
428 bool InspIRCd::SilentULine(const char* sserver)
429 {
430         std::map<irc::string,bool>::iterator n = Config->ulines.find(sserver);
431         if (n != Config->ulines.end())
432                 return n->second;
433         else return false;
434 }
435
436 std::string InspIRCd::TimeString(time_t curtime)
437 {
438         return std::string(ctime(&curtime),24);
439 }
440
441 // You should only pass a single character to this.
442 void InspIRCd::AddExtBanChar(char c)
443 {
444         std::string &tok = Config->data005;
445         std::string::size_type ebpos;
446
447         if ((ebpos = tok.find(" EXTBAN=,")) == std::string::npos)
448         {
449                 tok.append(" EXTBAN=,");
450                 tok.push_back(c);
451         }
452         else
453                 tok.insert(ebpos + 9, 1, c);
454 }