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