]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/helperfuncs.cpp
3f60cac80419acc443aab3e34f4bef9fe4bc03e8
[user/henk/code/inspircd.git] / src / helperfuncs.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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: libIRCDhelper */
15
16 #include "inspircd.h"
17 #include "wildcard.h"
18 #include "xline.h"
19 #include "exitcodes.h"
20
21 static char TIMESTR[26];
22 static time_t LAST = 0;
23
24 /** Log()
25  *  Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level'
26  *  is greater than the configured loglevel.
27  */
28 void InspIRCd::Log(int level, const char* text, ...)
29 {
30         /* sanity check, just in case */
31         if (!this->Config || !this->Logger)
32                 return;
33
34         /* Do this check again here so that we save pointless vsnprintf calls */
35         if ((level < Config->LogLevel) && !Config->forcedebug)
36                 return;
37
38         va_list argsPtr;
39         char textbuffer[65536];
40
41         va_start(argsPtr, text);
42         vsnprintf(textbuffer, 65536, text, argsPtr);
43         va_end(argsPtr);
44
45         this->Log(level, std::string(textbuffer));
46 }
47
48 void InspIRCd::Log(int level, const std::string &text)
49 {
50         /* sanity check, just in case */
51         if (!this->Config || !this->Logger)
52                 return;
53
54         /* If we were given -debug we output all messages, regardless of configured loglevel */
55         if ((level < Config->LogLevel) && !Config->forcedebug)
56                 return;
57
58         if (Time() != LAST)
59         {
60                 time_t local = Time();
61                 struct tm *timeinfo = localtime(&local);
62
63                 strlcpy(TIMESTR,asctime(timeinfo),26);
64                 TIMESTR[24] = ':';
65                 LAST = Time();
66         }
67
68         if (Config->log_file && Config->writelog)
69         {
70                 std::string out = std::string(TIMESTR) + " " + text.c_str() + "\n";
71                 this->Logger->WriteLogLine(out);
72         }
73
74         if (Config->nofork)
75         {
76                 printf("%s %s\n", TIMESTR, text.c_str());
77         }
78 }
79
80 std::string InspIRCd::GetServerDescription(const char* servername)
81 {
82         std::string description;
83
84         FOREACH_MOD_I(this,I_OnGetServerDescription,OnGetServerDescription(servername,description));
85
86         if (!description.empty())
87         {
88                 return description;
89         }
90         else
91         {
92                 // not a remote server that can be found, it must be me.
93                 return Config->ServerDesc;
94         }
95 }
96
97 /* Find a user record by nickname and return a pointer to it */
98 User* InspIRCd::FindNick(const std::string &nick)
99 {
100         if (!nick.empty() && isdigit(*nick.begin()))
101                 return FindUUID(nick);
102
103         user_hash::iterator iter = this->Users->clientlist->find(nick);
104
105         if (iter == this->Users->clientlist->end())
106                 /* Couldn't find it */
107                 return NULL;
108
109         return iter->second;
110 }
111
112 User* InspIRCd::FindNick(const char* nick)
113 {
114         if (isdigit(*nick))
115                 return FindUUID(nick);
116
117         user_hash::iterator iter = this->Users->clientlist->find(nick);
118         
119         if (iter == this->Users->clientlist->end())
120                 return NULL;
121
122         return iter->second;
123 }
124
125 User* InspIRCd::FindNickOnly(const std::string &nick)
126 {
127         user_hash::iterator iter = this->Users->clientlist->find(nick);
128
129         if (iter == this->Users->clientlist->end())
130                 return NULL;
131
132         return iter->second;
133 }
134
135 User* InspIRCd::FindNickOnly(const char* nick)
136 {
137         user_hash::iterator iter = this->Users->clientlist->find(nick);
138
139         if (iter == this->Users->clientlist->end())
140                 return NULL;
141
142         return iter->second;
143 }
144
145 User *InspIRCd::FindUUID(const std::string &uid)
146 {
147         return FindUUID(uid.c_str());
148 }
149
150 User *InspIRCd::FindUUID(const char *uid)
151 {
152         user_hash::iterator finduuid = this->Users->uuidlist->find(uid);
153
154         if (finduuid == this->Users->uuidlist->end())
155                 return NULL;
156
157         return finduuid->second;
158 }
159
160 /* find a channel record by channel name and return a pointer to it */
161 Channel* InspIRCd::FindChan(const char* chan)
162 {
163         chan_hash::iterator iter = chanlist->find(chan);
164
165         if (iter == chanlist->end())
166                 /* Couldn't find it */
167                 return NULL;
168
169         return iter->second;
170 }
171
172 Channel* InspIRCd::FindChan(const std::string &chan)
173 {
174         chan_hash::iterator iter = chanlist->find(chan);
175
176         if (iter == chanlist->end())
177                 /* Couldn't find it */
178                 return NULL;
179
180         return iter->second;
181 }
182
183 /* Send an error notice to all users, registered or not */
184 void InspIRCd::SendError(const std::string &s)
185 {
186         for (std::vector<User*>::const_iterator i = this->Users->local_users.begin(); i != this->Users->local_users.end(); i++)
187         {
188                 if ((*i)->registered == REG_ALL)
189                 {
190                         (*i)->WriteServ("NOTICE %s :%s",(*i)->nick,s.c_str());
191                 }
192                 else
193                 {
194                         /* Unregistered connections receive ERROR, not a NOTICE */
195                         (*i)->Write("ERROR :" + s);
196                 }
197                 /* This might generate a whole load of EAGAIN, but we dont really
198                  * care about this, as if we call SendError something catastrophic
199                  * has occured anyway, and we wont receive the events for these.
200                  */
201                 (*i)->FlushWriteBuf();
202         }
203 }
204
205 /* return channel count */
206 long InspIRCd::ChannelCount()
207 {
208         return chanlist->size();
209 }
210
211 bool InspIRCd::IsValidMask(const std::string &mask)
212 {
213         char* dest = (char*)mask.c_str();
214         int exclamation = 0;
215         int atsign = 0;
216
217         for (char* i = dest; *i; i++)
218         {
219                 /* out of range character, bad mask */
220                 if (*i < 32 || *i > 126)
221                 {
222                         return false;
223                 }
224
225                 switch (*i)
226                 {
227                         case '!':
228                                 exclamation++;
229                                 break;
230                         case '@':
231                                 atsign++;
232                                 break;
233                 }
234         }
235
236         /* valid masks only have 1 ! and @ */
237         if (exclamation != 1 || atsign != 1)
238                 return false;
239
240         return true;
241 }
242
243 /* true for valid channel name, false else */
244 bool InspIRCd::IsChannel(const char *chname)
245 {
246         char *c;
247
248         /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
249         if (!chname || *chname != '#')
250         {
251                 return false;
252         }
253
254         c = (char *)chname + 1;
255         while (*c)
256         {
257                 switch (*c)
258                 {
259                         case ' ':
260                         case ',':
261                         case 7:
262                                 return false;
263                 }
264
265                 c++;
266         }
267                 
268         /* too long a name - note funky pointer arithmetic here. */
269         if ((c - chname) > CHANMAX)
270         {
271                         return false;
272         }
273
274         return true;
275 }
276
277 /* true for valid nickname, false else */
278 bool IsNickHandler::Call(const char* n)
279 {
280         if (!n || !*n)
281                 return false;
282  
283         int p = 0;
284         for (char* i = (char*)n; *i; i++, p++)
285         {
286                 if ((*i >= 'A') && (*i <= '}'))
287                 {
288                         /* "A"-"}" can occur anywhere in a nickname */
289                         continue;
290                 }
291
292                 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
293                 {
294                         /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
295                         continue;
296                 }
297
298                 /* invalid character! abort */
299                 return false;
300         }
301
302         /* too long? or not -- pointer arithmetic rocks */
303         return (p < NICKMAX - 1);
304 }
305
306 /* return true for good ident, false else */
307 bool IsIdentHandler::Call(const char* n)
308 {
309         if (!n || !*n)
310                 return false;
311
312         for (char* i = (char*)n; *i; i++)
313         {
314                 if ((*i >= 'A') && (*i <= '}'))
315                 {
316                         continue;
317                 }
318
319                 if (((*i >= '0') && (*i <= '9')) || (*i == '-') || (*i == '.'))
320                 {
321                         continue;
322                 }
323
324                 return false;
325         }
326
327         return true;
328 }
329
330 bool InspIRCd::IsSID(const std::string &str)
331 {
332         /* Returns true if the string given is exactly 3 characters long,
333          * starts with a digit, and the other two characters are A-Z or digits
334          */
335         return ((str.length() == 3) && isdigit(str[0]) &&
336                         ((str[1] >= 'A' && str[1] <= 'Z') || isdigit(str[1])) &&
337                          ((str[2] >= 'A' && str[2] <= 'Z') || isdigit(str[2])));
338 }
339
340 /* open the proper logfile */
341 bool InspIRCd::OpenLog(char**, int)
342 {
343         Config->MyDir = Config->GetFullProgDir();
344
345         if (!*this->LogFileName)
346         {
347                 if (Config->logpath.empty())
348                 {
349                         Config->logpath = Config->MyDir + "/ircd.log";
350                 }
351
352                 Config->log_file = fopen(Config->logpath.c_str(),"a+");
353         }
354         else
355         {
356                 Config->log_file = fopen(this->LogFileName,"a+");
357         }
358
359         if (!Config->log_file)
360         {
361                 this->Logger = NULL;
362                 return false;
363         }
364
365         this->Logger = new FileLogger(this, Config->log_file);
366         return true;
367 }
368
369 void InspIRCd::CheckRoot()
370 {
371         if (geteuid() == 0)
372         {
373                 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
374                 this->Log(DEFAULT,"Cant start as root");
375                 Exit(EXIT_STATUS_ROOT);
376         }
377 }
378
379 void InspIRCd::CheckDie()
380 {
381         if (*Config->DieValue)
382         {
383                 printf("WARNING: %s\n\n",Config->DieValue);
384                 this->Log(DEFAULT,"Died because of <die> tag: %s",Config->DieValue);
385                 Exit(EXIT_STATUS_DIETAG);
386         }
387 }
388
389 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const std::string &text)
390 {
391         std::string copy_text = text;
392
393         int MOD_RESULT = 0;
394         FOREACH_RESULT_I(this, I_OnWhoisLine, OnWhoisLine(user, dest, numeric, copy_text));
395
396         if (!MOD_RESULT)
397                 user->WriteServ("%d %s", numeric, copy_text.c_str());
398 }
399
400 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const char* format, ...)
401 {
402         char textbuffer[MAXBUF];
403         va_list argsPtr;
404         va_start (argsPtr, format);
405         vsnprintf(textbuffer, MAXBUF, format, argsPtr);
406         va_end(argsPtr);
407
408         this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));
409 }
410
411 /** Refactored by Brain, Jun 2008. Much faster with some clever O(1) array
412  * lookups and pointer maths.
413  */
414 long InspIRCd::Duration(const std::string &str)
415 {
416         unsigned char multiplier = 0;
417         long total = 0;
418         long times = 1;
419         long subtotal = 0;
420
421         /* Iterate each item in the string, looking for number or multiplier */
422         for (std::string::const_reverse_iterator i = str.rbegin(); i != str.rend(); ++i)
423         {
424                 /* Found a number, queue it onto the current number */
425                 if ((*i >= '0') && (*i <= '9'))
426                 {
427                         subtotal = subtotal + ((*i - '0') * times);
428                         times = times * 10;
429                 }
430                 else
431                 {
432                         /* Found something thats not a number, find out how much
433                          * it multiplies the built up number by, multiply the total
434                          * and reset the built up number.
435                          */
436                         if (subtotal)
437                                 total += subtotal * duration_multi[multiplier];
438
439                         /* Next subtotal please */
440                         subtotal = 0;
441                         multiplier = *i;
442                         times = 1;
443                 }
444         }
445         if (multiplier)
446         {
447                 total += subtotal * duration_multi[multiplier];
448                 subtotal = 0;
449         }
450         /* Any trailing values built up are treated as raw seconds */
451         return total + subtotal;
452 }
453
454 bool InspIRCd::ULine(const char* server)
455 {
456         if (!server)
457                 return false;
458         if (!*server)
459                 return true;
460
461         return (Config->ulines.find(server) != Config->ulines.end());
462 }
463
464 bool InspIRCd::SilentULine(const char* server)
465 {
466         std::map<irc::string,bool>::iterator n = Config->ulines.find(server);
467         if (n != Config->ulines.end())
468                 return n->second;
469         else return false;
470 }
471
472 std::string InspIRCd::TimeString(time_t curtime)
473 {
474         return std::string(ctime(&curtime),24);
475 }
476