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