]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/helperfuncs.cpp
Remove next_call garbage.. It didn't really do much more than obfuscate things. InspI...
[user/henk/code/inspircd.git] / src / helperfuncs.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 #include "inspircd.h"
15 #include <stdarg.h>
16 #include "wildcard.h"
17 #include "xline.h"
18 #include "exitcodes.h"
19
20 static char TIMESTR[26];
21 static time_t LAST = 0;
22
23 /** Log()
24  *  Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level'
25  *  is greater than the configured loglevel.
26  */
27 void InspIRCd::Log(int level, const char* text, ...)
28 {
29         /* sanity check, just in case */
30         if (!this->Config || !this->Logger)
31                 return;
32
33         /* Do this check again here so that we save pointless vsnprintf calls */
34         if ((level < Config->LogLevel) && !Config->forcedebug)
35                 return;
36
37         va_list argsPtr;
38         char textbuffer[65536];
39
40         va_start(argsPtr, text);
41         vsnprintf(textbuffer, 65536, text, argsPtr);
42         va_end(argsPtr);
43
44         this->Log(level, std::string(textbuffer));
45 }
46
47 void InspIRCd::Log(int level, const std::string &text)
48 {
49         /* sanity check, just in case */
50         if (!this->Config || !this->Logger)
51                 return;
52
53         /* If we were given -debug we output all messages, regardless of configured loglevel */
54         if ((level < Config->LogLevel) && !Config->forcedebug)
55                 return;
56
57         if (Time() != LAST)
58         {
59                 time_t local = Time();
60                 struct tm *timeinfo = localtime(&local);
61
62                 strlcpy(TIMESTR,asctime(timeinfo),26);
63                 TIMESTR[24] = ':';
64                 LAST = Time();
65         }
66
67         if (Config->log_file && Config->writelog)
68         {
69                 std::string out = std::string(TIMESTR) + " " + text.c_str() + "\n";
70                 this->Logger->WriteLogLine(out);
71         }
72
73         if (Config->nofork)
74         {
75                 printf("%s %s\n", TIMESTR, text.c_str());
76         }
77 }
78
79 std::string InspIRCd::GetServerDescription(const char* servername)
80 {
81         std::string description;
82
83         FOREACH_MOD_I(this,I_OnGetServerDescription,OnGetServerDescription(servername,description));
84
85         if (!description.empty())
86         {
87                 return description;
88         }
89         else
90         {
91                 // not a remote server that can be found, it must be me.
92                 return Config->ServerDesc;
93         }
94 }
95
96 /* XXX - We don't use WriteMode for this because WriteMode is very slow and
97  * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
98  * the number of modes provided, e.g. if you send WriteMode 'og' to write to
99  * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
100  * uses the oper list, which means if you have 2000 users but only 5 opers,
101  * it iterates 5 times.
102  */
103 void InspIRCd::WriteOpers(const char* text, ...)
104 {
105         char textbuffer[MAXBUF];
106         va_list argsPtr;
107
108         va_start(argsPtr, text);
109         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
110         va_end(argsPtr);
111
112         this->WriteOpers(std::string(textbuffer));
113 }
114
115 void InspIRCd::WriteOpers(const std::string &text)
116 {
117         for (std::list<User*>::iterator i = this->all_opers.begin(); i != this->all_opers.end(); i++)
118         {
119                 User* a = *i;
120                 if (IS_LOCAL(a) && a->IsModeSet('s'))
121                 {
122                         // send server notices to all with +s
123                         a->WriteServ("NOTICE %s :%s",a->nick,text.c_str());
124                 }
125         }
126 }
127
128 void InspIRCd::ServerNoticeAll(char* text, ...)
129 {
130         if (!text)
131                 return;
132
133         char textbuffer[MAXBUF];
134         char formatbuffer[MAXBUF];
135         va_list argsPtr;
136         va_start (argsPtr, text);
137         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
138         va_end(argsPtr);
139
140         snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s",Config->ServerName,textbuffer);
141
142         for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
143         {
144                 User* t = *i;
145                 t->WriteServ(std::string(formatbuffer));
146         }
147 }
148
149 void InspIRCd::ServerPrivmsgAll(char* text, ...)
150 {
151         if (!text)
152                 return;
153
154         char textbuffer[MAXBUF];
155         char formatbuffer[MAXBUF];
156         va_list argsPtr;
157         va_start (argsPtr, text);
158         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
159         va_end(argsPtr);
160
161         snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
162
163         for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
164         {
165                 User* t = *i;
166                 t->WriteServ(std::string(formatbuffer));
167         }
168 }
169
170 void InspIRCd::WriteMode(const char* modes, int flags, const char* text, ...)
171 {
172         char textbuffer[MAXBUF];
173         int modelen;
174         va_list argsPtr;
175
176         if (!text || !modes || !flags)
177         {
178                 this->Log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
179                 return;
180         }
181
182         va_start(argsPtr, text);
183         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
184         va_end(argsPtr);
185         modelen = strlen(modes);
186
187         if (flags == WM_AND)
188         {
189                 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
190                 {
191                         User* t = *i;
192                         bool send_to_user = true;
193
194                         for (int n = 0; n < modelen; n++)
195                         {
196                                 if (!t->IsModeSet(modes[n]))
197                                 {
198                                         send_to_user = false;
199                                         break;
200                                 }
201                         }
202                         if (send_to_user)
203                         {
204                                 t->WriteServ("NOTICE %s :%s", t->nick, textbuffer);
205                         }
206                 }
207         }
208         else if (flags == WM_OR)
209         {
210                 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
211                 {
212                         User* t = *i;
213                         bool send_to_user = false;
214
215                         for (int n = 0; n < modelen; n++)
216                         {
217                                 if (t->IsModeSet(modes[n]))
218                                 {
219                                         send_to_user = true;
220                                         break;
221                                 }
222                         }
223
224                         if (send_to_user)
225                         {
226                                 t->WriteServ("NOTICE %s :%s", t->nick, textbuffer);
227                         }
228                 }
229         }
230 }
231
232 /* Find a user record by nickname and return a pointer to it */
233 User* InspIRCd::FindNick(const std::string &nick)
234 {
235         if (!nick.empty() && isdigit(*nick.begin()))
236                 return FindUUID(nick);
237
238         user_hash::iterator iter = clientlist->find(nick);
239
240         if (iter == clientlist->end())
241                 /* Couldn't find it */
242                 return NULL;
243
244         return iter->second;
245 }
246
247 User* InspIRCd::FindNick(const char* nick)
248 {
249         if (isdigit(*nick))
250                 return FindUUID(nick);
251
252         user_hash::iterator iter = clientlist->find(nick);
253         
254         if (iter == clientlist->end())
255                 return NULL;
256
257         return iter->second;
258 }
259
260 User* InspIRCd::FindNickOnly(const std::string &nick)
261 {
262         user_hash::iterator iter = clientlist->find(nick);
263
264         if (iter == clientlist->end())
265                 return NULL;
266
267         return iter->second;
268 }
269
270 User* InspIRCd::FindNickOnly(const char* nick)
271 {
272         user_hash::iterator iter = clientlist->find(nick);
273
274         if (iter == clientlist->end())
275                 return NULL;
276
277         return iter->second;
278 }
279
280 User *InspIRCd::FindUUID(const std::string &uid)
281 {
282         return FindUUID(uid.c_str());
283 }
284
285 User *InspIRCd::FindUUID(const char *uid)
286 {
287         user_hash::iterator finduuid = uuidlist->find(uid);
288
289         if (finduuid == uuidlist->end())
290                 return NULL;
291
292         return finduuid->second;
293 }
294
295 /* find a channel record by channel name and return a pointer to it */
296 Channel* InspIRCd::FindChan(const char* chan)
297 {
298         chan_hash::iterator iter = chanlist->find(chan);
299
300         if (iter == chanlist->end())
301                 /* Couldn't find it */
302                 return NULL;
303
304         return iter->second;
305 }
306
307 Channel* InspIRCd::FindChan(const std::string &chan)
308 {
309         chan_hash::iterator iter = chanlist->find(chan);
310
311         if (iter == chanlist->end())
312                 /* Couldn't find it */
313                 return NULL;
314
315         return iter->second;
316 }
317
318 /* Send an error notice to all users, registered or not */
319 void InspIRCd::SendError(const std::string &s)
320 {
321         for (std::vector<User*>::const_iterator i = this->local_users.begin(); i != this->local_users.end(); i++)
322         {
323                 if ((*i)->registered == REG_ALL)
324                 {
325                         (*i)->WriteServ("NOTICE %s :%s",(*i)->nick,s.c_str());
326                 }
327                 else
328                 {
329                         /* Unregistered connections receive ERROR, not a NOTICE */
330                         (*i)->Write("ERROR :" + s);
331                 }
332                 /* This might generate a whole load of EAGAIN, but we dont really
333                  * care about this, as if we call SendError something catastrophic
334                  * has occured anyway, and we wont receive the events for these.
335                  */
336                 (*i)->FlushWriteBuf();
337         }
338 }
339
340 /* this function counts all users connected, wether they are registered or NOT. */
341 int InspIRCd::UserCount()
342 {
343         return clientlist->size();
344 }
345
346 /* this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state */
347 int InspIRCd::RegisteredUserCount()
348 {
349         return clientlist->size() - this->UnregisteredUserCount();
350 }
351
352 /* return how many users have a given mode e.g. 'a' */
353 int InspIRCd::ModeCount(const char mode)
354 {
355         ModeHandler* mh = this->Modes->FindMode(mode, MODETYPE_USER);
356
357         if (mh)
358                 return mh->GetCount();
359         else
360                 return 0;
361 }
362
363 /* wrapper for readability */
364 int InspIRCd::InvisibleUserCount()
365 {
366         return ModeCount('i');
367 }
368
369 /* return how many users are opered */
370 int InspIRCd::OperCount()
371 {
372         return this->all_opers.size();
373 }
374
375 /* return how many users are unregistered */
376 int InspIRCd::UnregisteredUserCount()
377 {
378         return this->unregistered_count;
379 }
380
381 /* return channel count */
382 long InspIRCd::ChannelCount()
383 {
384         return chanlist->size();
385 }
386
387 /* return how many local registered users there are */
388 long InspIRCd::LocalUserCount()
389 {
390         /* Doesnt count unregistered clients */
391         return (local_users.size() - this->UnregisteredUserCount());
392 }
393
394 /* true for valid channel name, false else */
395 bool InspIRCd::IsChannel(const char *chname)
396 {
397         char *c;
398
399         /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
400         if (!chname || *chname != '#')
401         {
402                 return false;
403         }
404
405         c = (char *)chname + 1;
406         while (*c)
407         {
408                 switch (*c)
409                 {
410                         case ' ':
411                         case ',':
412                         case 7:
413                                 return false;
414                 }
415
416                 c++;
417         }
418                 
419         /* too long a name - note funky pointer arithmetic here. */
420         if ((c - chname) > CHANMAX)
421         {
422                         return false;
423         }
424
425         return true;
426 }
427
428 /* true for valid nickname, false else */
429 bool IsNickHandler::Call(const char* n)
430 {
431         if (!n || !*n)
432                 return false;
433  
434         int p = 0;
435         for (char* i = (char*)n; *i; i++, p++)
436         {
437                 if ((*i >= 'A') && (*i <= '}'))
438                 {
439                         /* "A"-"}" can occur anywhere in a nickname */
440                         continue;
441                 }
442
443                 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
444                 {
445                         /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
446                         continue;
447                 }
448
449                 /* invalid character! abort */
450                 return false;
451         }
452
453         /* too long? or not -- pointer arithmetic rocks */
454         return (p < NICKMAX - 1);
455 }
456
457 /* return true for good ident, false else */
458 bool IsIdentHandler::Call(const char* n)
459 {
460         if (!n || !*n)
461                 return false;
462
463         for (char* i = (char*)n; *i; i++)
464         {
465                 if ((*i >= 'A') && (*i <= '}'))
466                 {
467                         continue;
468                 }
469
470                 if (((*i >= '0') && (*i <= '9')) || (*i == '-') || (*i == '.'))
471                 {
472                         continue;
473                 }
474
475                 return false;
476         }
477
478         return true;
479 }
480
481 /* open the proper logfile */
482 bool InspIRCd::OpenLog(char** argv, int argc)
483 {
484         Config->MyDir = Config->GetFullProgDir();
485
486         if (!*this->LogFileName)
487         {
488                 if (Config->logpath.empty())
489                 {
490                         Config->logpath = Config->MyDir + "/ircd.log";
491                 }
492
493                 Config->log_file = fopen(Config->logpath.c_str(),"a+");
494         }
495         else
496         {
497                 Config->log_file = fopen(this->LogFileName,"a+");
498         }
499
500         if (!Config->log_file)
501         {
502                 this->Logger = NULL;
503                 return false;
504         }
505
506         this->Logger = new FileLogger(this, Config->log_file);
507         return true;
508 }
509
510 void InspIRCd::CheckRoot()
511 {
512         if (geteuid() == 0)
513         {
514                 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
515                 this->Log(DEFAULT,"Cant start as root");
516                 Exit(EXIT_STATUS_ROOT);
517         }
518 }
519
520 void InspIRCd::CheckDie()
521 {
522         if (*Config->DieValue)
523         {
524                 printf("WARNING: %s\n\n",Config->DieValue);
525                 this->Log(DEFAULT,"Died because of <die> tag: %s",Config->DieValue);
526                 Exit(EXIT_STATUS_DIETAG);
527         }
528 }
529
530 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const std::string &text)
531 {
532         std::string copy_text = text;
533
534         int MOD_RESULT = 0;
535         FOREACH_RESULT_I(this, I_OnWhoisLine, OnWhoisLine(user, dest, numeric, copy_text));
536
537         if (!MOD_RESULT)
538                 user->WriteServ("%d %s", numeric, copy_text.c_str());
539 }
540
541 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const char* format, ...)
542 {
543         char textbuffer[MAXBUF];
544         va_list argsPtr;
545         va_start (argsPtr, format);
546         vsnprintf(textbuffer, MAXBUF, format, argsPtr);
547         va_end(argsPtr);
548
549         this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));
550 }
551
552 /** Refactored by Brain, Jun 2007. Much faster with some clever O(1) array
553  * lookups and pointer maths.
554  */
555 long InspIRCd::Duration(const std::string &str)
556 {
557         unsigned char multiplier = 0;
558         long total = 0;
559         long times = 1;
560         long subtotal = 0;
561
562         /* Iterate each item in the string, looking for number or multiplier */
563         for (std::string::const_reverse_iterator i = str.rbegin(); i != str.rend(); ++i)
564         {
565                 /* Found a number, queue it onto the current number */
566                 if ((*i >= '0') && (*i <= '9'))
567                 {
568                         subtotal = subtotal + ((*i - '0') * times);
569                         times = times * 10;
570                 }
571                 else
572                 {
573                         /* Found something thats not a number, find out how much
574                          * it multiplies the built up number by, multiply the total
575                          * and reset the built up number.
576                          */
577                         if (subtotal)
578                                 total += subtotal * duration_multi[multiplier];
579
580                         /* Next subtotal please */
581                         subtotal = 0;
582                         multiplier = *i;
583                         times = 1;
584                 }
585         }
586         if (multiplier)
587         {
588                 total += subtotal * duration_multi[multiplier];
589                 subtotal = 0;
590         }
591         /* Any trailing values built up are treated as raw seconds */
592         return total + subtotal;
593 }
594
595 bool InspIRCd::ULine(const char* server)
596 {
597         if (!server)
598                 return false;
599         if (!*server)
600                 return true;
601
602         return (Config->ulines.find(server) != Config->ulines.end());
603 }
604
605 bool InspIRCd::SilentULine(const char* server)
606 {
607         std::map<irc::string,bool>::iterator n = Config->ulines.find(server);
608         if (n != Config->ulines.end())
609                 return n->second;
610         else return false;
611 }
612
613 std::string InspIRCd::TimeString(time_t curtime)
614 {
615         return std::string(ctime(&curtime),24);
616 }
617