]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/helperfuncs.cpp
Give some actual information when the log file fails to open
[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         if (flags == WM_AND)
185         {
186                 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
187                 {
188                         userrec* t = *i;
189                         bool send_to_user = true;
190
191                         for (int n = 0; n < modelen; n++)
192                         {
193                                 if (!t->modes[modes[n]-65])
194                                 {
195                                         send_to_user = false;
196                                         break;
197                                 }
198                         }
199                         if (send_to_user)
200                                 t->WriteServ("NOTICE %s :%s",t->nick,textbuffer);
201                 }
202         }
203         else
204         if (flags == WM_OR)
205         {
206                 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
207                 {
208                         userrec* t = *i;
209                         bool send_to_user = false;
210
211                         for (int n = 0; n < modelen; n++)
212                         {
213                                 if (t->modes[modes[n]-65])
214                                 {
215                                         send_to_user = true;
216                                         break;
217                                 }
218                         }
219                         if (send_to_user)
220                                 t->WriteServ("NOTICE %s :%s",t->nick,textbuffer);
221                 }
222         }
223 }
224
225 /* Find a user record by nickname and return a pointer to it */
226
227 userrec* InspIRCd::FindNick(const std::string &nick)
228 {
229         user_hash::iterator iter = clientlist.find(nick);
230
231         if (iter == clientlist.end())
232                 /* Couldn't find it */
233                 return NULL;
234
235         return iter->second;
236 }
237
238 userrec* InspIRCd::FindNick(const char* nick)
239 {
240         user_hash::iterator iter;
241
242         if (!nick)
243                 return NULL;
244
245         iter = clientlist.find(nick);
246         
247         if (iter == clientlist.end())
248                 return NULL;
249
250         return iter->second;
251 }
252
253 /* find a channel record by channel name and return a pointer to it */
254
255 chanrec* InspIRCd::FindChan(const char* chan)
256 {
257         chan_hash::iterator iter;
258
259         if (!chan)
260                 return NULL;
261
262         iter = chanlist.find(chan);
263
264         if (iter == chanlist.end())
265                 /* Couldn't find it */
266                 return NULL;
267
268         return iter->second;
269 }
270
271 chanrec* InspIRCd::FindChan(const std::string &chan)
272 {
273         chan_hash::iterator iter = chanlist.find(chan);
274
275         if (iter == chanlist.end())
276                 /* Couldn't find it */
277                 return NULL;
278
279         return iter->second;
280 }
281
282
283 /*
284  * sends out an error notice to all connected clients (not to be used
285  * lightly!)
286  */
287 void InspIRCd::SendError(const char *s)
288 {
289         for (std::vector<userrec*>::const_iterator i = this->local_users.begin(); i != this->local_users.end(); i++)
290         {
291                 userrec* t = (userrec*)(*i);
292                 if (t->registered == REG_ALL)
293                 {
294                         t->WriteServ("NOTICE %s :%s",t->nick,s);
295                 }
296                 else
297                 {
298                         // fix - unregistered connections receive ERROR, not NOTICE
299                         t->Write("ERROR :%s",s);
300                 }
301         }
302 }
303
304 // this function counts all users connected, wether they are registered or NOT.
305 int InspIRCd::UserCount()
306 {
307         return clientlist.size();
308 }
309
310 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
311 int InspIRCd::RegisteredUserCount()
312 {
313         int c = 0;
314
315         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
316         {
317                 c += (i->second->registered == REG_ALL);
318         }
319
320         return c;
321 }
322
323 int InspIRCd::InvisibleUserCount()
324 {
325         int c = 0;
326
327         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
328         {
329                 c += ((i->second->registered == REG_ALL) && (i->second->modes[UM_INVISIBLE]));
330         }
331
332         return c;
333 }
334
335 int InspIRCd::OperCount()
336 {
337         int c = 0;
338
339         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
340         {
341                 if (*(i->second->oper))
342                         c++;
343         }
344         return c;
345 }
346
347 int InspIRCd::UnregisteredUserCount()
348 {
349         int c = 0;
350
351         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
352         {
353                 userrec* t = (userrec*)(*i);
354                 if (t->registered != REG_ALL)
355                         c++;
356         }
357
358         return c;
359 }
360
361 long InspIRCd::ChannelCount()
362 {
363         return chanlist.size();
364 }
365
366 long InspIRCd::LocalUserCount()
367 {
368         int c = 0;
369
370         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
371         {
372                 userrec* t = (userrec*)(*i);
373                 if (t->registered == REG_ALL)
374                         c++;
375         }
376
377         return c;
378 }
379
380 bool InspIRCd::IsChannel(const char *chname)
381 {
382         char *c;
383
384         /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
385         if (!chname || *chname != '#')
386         {
387                 return false;
388         }
389
390         c = (char *)chname + 1;
391         while (*c)
392         {
393                 switch (*c)
394                 {
395                         case ' ':
396                         case ',':
397                         case 7:
398                                 return false;
399                 }
400
401                 c++;
402         }
403                 
404         /* too long a name - note funky pointer arithmetic here. */
405         if ((c - chname) > CHANMAX)
406         {
407                         return false;
408         }
409
410         return true;
411 }
412
413 bool InspIRCd::IsNick(const char* n)
414 {
415         if (!n || !*n)
416                 return false;
417  
418         int p = 0;
419         for (char* i = (char*)n; *i; i++, p++)
420         {
421                 if ((*i >= 'A') && (*i <= '}'))
422                 {
423                         /* "A"-"}" can occur anywhere in a nickname */
424                         continue;
425                 }
426
427                 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
428                 {
429                         /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
430                         continue;
431                 }
432
433                 /* invalid character! abort */
434                 return false;
435         }
436
437         /* too long? or not -- pointer arithmetic rocks */
438         return (p < NICKMAX - 1);
439 }
440
441 void InspIRCd::OpenLog(char** argv, int argc)
442 {
443         if (!*this->LogFileName)
444         {
445                 if (Config->logpath == "")
446                 {
447                         Config->logpath = ServerConfig::GetFullProgDir(argv,argc) + "/ircd.log";
448                 }
449
450                 Config->log_file = fopen(Config->logpath.c_str(),"a+");
451         }
452         else
453         {
454                 Config->log_file = fopen(this->LogFileName,"a+");
455         }
456
457         if (!Config->log_file)
458         {
459                 printf("ERROR: Could not write to logfile %s: %s\n\n", Config->logpath.c_str(), strerror(errno));
460                 Exit(ERROR);
461         }
462
463         this->Logger = new FileLogger(this, Config->log_file);
464 }
465
466 void InspIRCd::CheckRoot()
467 {
468         if (geteuid() == 0)
469         {
470                 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
471                 this->Log(DEFAULT,"Cant start as root");
472                 Exit(ERROR);
473         }
474 }
475
476 void InspIRCd::CheckDie()
477 {
478         if (*Config->DieValue)
479         {
480                 printf("WARNING: %s\n\n",Config->DieValue);
481                 this->Log(DEFAULT,"Died because of <die> tag: %s",Config->DieValue);
482                 Exit(ERROR);
483         }
484 }
485
486 /* We must load the modules AFTER initializing the socket engine, now */
487 void InspIRCd::LoadAllModules()
488 {
489         char configToken[MAXBUF];
490         Config->module_names.clear();
491         this->ModCount = -1;
492
493         for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "module"); count++)
494         {
495                 Config->ConfValue(Config->config_data, "module","name",count,configToken,MAXBUF);
496                 printf("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
497                 
498                 if (!this->LoadModule(configToken))             
499                 {
500                         this->Log(DEFAULT,"There was an error loading a module: %s", this->ModuleError());
501                         printf("\nThere was an error loading a module: %s\n\n",this->ModuleError());
502                         Exit(ERROR);
503                 }
504         }
505         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");
506         this->Log(DEFAULT,"Total loaded modules: %d", this->ModCount+1);
507 }
508