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