]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/helperfuncs.cpp
New logging implementation. Also write messages about InspIRCd::Log() being deprecate...
[user/henk/code/inspircd.git] / src / helperfuncs.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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 /* $Core: libIRCDhelper */
15
16 #include "inspircd.h"
17 #include "wildcard.h"
18 #include "xline.h"
19 #include "exitcodes.h"
20
21 /** Log()
22  *  Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level'
23  *  is greater than the configured loglevel.
24  */
25 void InspIRCd::Log(int level, const char* text, ...)
26 {
27         va_list argsPtr;
28         char textbuffer[65536];
29
30         va_start(argsPtr, text);
31         vsnprintf(textbuffer, 65536, text, argsPtr);
32         va_end(argsPtr);
33
34         this->Log(level, std::string(textbuffer));
35 }
36
37 void InspIRCd::Log(int level, const std::string &text)
38 {
39         this->Logs->Log("WARNING", DEFAULT, "Deprecated call to InspIRCd::Log()! - log message follows");
40         this->Logs->Log("DEPRECATED", level, text);
41 }
42
43 std::string InspIRCd::GetServerDescription(const char* servername)
44 {
45         std::string description;
46
47         FOREACH_MOD_I(this,I_OnGetServerDescription,OnGetServerDescription(servername,description));
48
49         if (!description.empty())
50         {
51                 return description;
52         }
53         else
54         {
55                 // not a remote server that can be found, it must be me.
56                 return Config->ServerDesc;
57         }
58 }
59
60 /* Find a user record by nickname and return a pointer to it */
61 User* InspIRCd::FindNick(const std::string &nick)
62 {
63         if (!nick.empty() && isdigit(*nick.begin()))
64                 return FindUUID(nick);
65
66         user_hash::iterator iter = this->Users->clientlist->find(nick);
67
68         if (iter == this->Users->clientlist->end())
69                 /* Couldn't find it */
70                 return NULL;
71
72         return iter->second;
73 }
74
75 User* InspIRCd::FindNick(const char* nick)
76 {
77         if (isdigit(*nick))
78                 return FindUUID(nick);
79
80         user_hash::iterator iter = this->Users->clientlist->find(nick);
81         
82         if (iter == this->Users->clientlist->end())
83                 return NULL;
84
85         return iter->second;
86 }
87
88 User* InspIRCd::FindNickOnly(const std::string &nick)
89 {
90         user_hash::iterator iter = this->Users->clientlist->find(nick);
91
92         if (iter == this->Users->clientlist->end())
93                 return NULL;
94
95         return iter->second;
96 }
97
98 User* InspIRCd::FindNickOnly(const char* nick)
99 {
100         user_hash::iterator iter = this->Users->clientlist->find(nick);
101
102         if (iter == this->Users->clientlist->end())
103                 return NULL;
104
105         return iter->second;
106 }
107
108 User *InspIRCd::FindUUID(const std::string &uid)
109 {
110         return FindUUID(uid.c_str());
111 }
112
113 User *InspIRCd::FindUUID(const char *uid)
114 {
115         user_hash::iterator finduuid = this->Users->uuidlist->find(uid);
116
117         if (finduuid == this->Users->uuidlist->end())
118                 return NULL;
119
120         return finduuid->second;
121 }
122
123 /* find a channel record by channel name and return a pointer to it */
124 Channel* InspIRCd::FindChan(const char* chan)
125 {
126         chan_hash::iterator iter = chanlist->find(chan);
127
128         if (iter == chanlist->end())
129                 /* Couldn't find it */
130                 return NULL;
131
132         return iter->second;
133 }
134
135 Channel* InspIRCd::FindChan(const std::string &chan)
136 {
137         chan_hash::iterator iter = chanlist->find(chan);
138
139         if (iter == chanlist->end())
140                 /* Couldn't find it */
141                 return NULL;
142
143         return iter->second;
144 }
145
146 /* Send an error notice to all users, registered or not */
147 void InspIRCd::SendError(const std::string &s)
148 {
149         for (std::vector<User*>::const_iterator i = this->Users->local_users.begin(); i != this->Users->local_users.end(); i++)
150         {
151                 if ((*i)->registered == REG_ALL)
152                 {
153                         (*i)->WriteServ("NOTICE %s :%s",(*i)->nick,s.c_str());
154                 }
155                 else
156                 {
157                         /* Unregistered connections receive ERROR, not a NOTICE */
158                         (*i)->Write("ERROR :" + s);
159                 }
160                 /* This might generate a whole load of EAGAIN, but we dont really
161                  * care about this, as if we call SendError something catastrophic
162                  * has occured anyway, and we wont receive the events for these.
163                  */
164                 (*i)->FlushWriteBuf();
165         }
166 }
167
168 /* return channel count */
169 long InspIRCd::ChannelCount()
170 {
171         return chanlist->size();
172 }
173
174 bool InspIRCd::IsValidMask(const std::string &mask)
175 {
176         char* dest = (char*)mask.c_str();
177         int exclamation = 0;
178         int atsign = 0;
179
180         for (char* i = dest; *i; i++)
181         {
182                 /* out of range character, bad mask */
183                 if (*i < 32 || *i > 126)
184                 {
185                         return false;
186                 }
187
188                 switch (*i)
189                 {
190                         case '!':
191                                 exclamation++;
192                                 break;
193                         case '@':
194                                 atsign++;
195                                 break;
196                 }
197         }
198
199         /* valid masks only have 1 ! and @ */
200         if (exclamation != 1 || atsign != 1)
201                 return false;
202
203         return true;
204 }
205
206 /* true for valid channel name, false else */
207 bool InspIRCd::IsChannel(const char *chname)
208 {
209         char *c;
210
211         /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
212         if (!chname || *chname != '#')
213         {
214                 return false;
215         }
216
217         c = (char *)chname + 1;
218         while (*c)
219         {
220                 switch (*c)
221                 {
222                         case ' ':
223                         case ',':
224                         case 7:
225                                 return false;
226                 }
227
228                 c++;
229         }
230                 
231         /* too long a name - note funky pointer arithmetic here. */
232         if ((c - chname) > CHANMAX)
233         {
234                         return false;
235         }
236
237         return true;
238 }
239
240 /* true for valid nickname, false else */
241 bool IsNickHandler::Call(const char* n)
242 {
243         if (!n || !*n)
244                 return false;
245  
246         int p = 0;
247         for (char* i = (char*)n; *i; i++, p++)
248         {
249                 if ((*i >= 'A') && (*i <= '}'))
250                 {
251                         /* "A"-"}" can occur anywhere in a nickname */
252                         continue;
253                 }
254
255                 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
256                 {
257                         /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
258                         continue;
259                 }
260
261                 /* invalid character! abort */
262                 return false;
263         }
264
265         /* too long? or not -- pointer arithmetic rocks */
266         return (p < NICKMAX - 1);
267 }
268
269 /* return true for good ident, false else */
270 bool IsIdentHandler::Call(const char* n)
271 {
272         if (!n || !*n)
273                 return false;
274
275         for (char* i = (char*)n; *i; i++)
276         {
277                 if ((*i >= 'A') && (*i <= '}'))
278                 {
279                         continue;
280                 }
281
282                 if (((*i >= '0') && (*i <= '9')) || (*i == '-') || (*i == '.'))
283                 {
284                         continue;
285                 }
286
287                 return false;
288         }
289
290         return true;
291 }
292
293 bool InspIRCd::IsSID(const std::string &str)
294 {
295         /* Returns true if the string given is exactly 3 characters long,
296          * starts with a digit, and the other two characters are A-Z or digits
297          */
298         return ((str.length() == 3) && isdigit(str[0]) &&
299                         ((str[1] >= 'A' && str[1] <= 'Z') || isdigit(str[1])) &&
300                          ((str[2] >= 'A' && str[2] <= 'Z') || isdigit(str[2])));
301 }
302
303 /* open the proper logfile */
304 bool InspIRCd::OpenLog(char**, int)
305 {
306         Config->MyDir = Config->GetFullProgDir();
307
308         if (!*this->LogFileName)
309         {
310                 if (Config->logpath.empty())
311                 {
312                         Config->logpath = Config->MyDir + "/ircd.log";
313                 }
314
315                 Config->log_file = fopen(Config->logpath.c_str(),"a+");
316         }
317         else
318         {
319                 Config->log_file = fopen(this->LogFileName,"a+");
320         }
321
322         if (!Config->log_file)
323         {
324                 return false;
325         }
326
327         FileLogStream *f = new FileLogStream(this, Config->log_file, "*");
328         this->Logs->AddLogType("*", f);
329         return true;
330 }
331
332 void InspIRCd::CheckRoot()
333 {
334         if (geteuid() == 0)
335         {
336                 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
337                 this->Log(DEFAULT,"Cant start as root");
338                 Exit(EXIT_STATUS_ROOT);
339         }
340 }
341
342 void InspIRCd::CheckDie()
343 {
344         if (*Config->DieValue)
345         {
346                 printf("WARNING: %s\n\n",Config->DieValue);
347                 this->Log(DEFAULT,"Died because of <die> tag: %s",Config->DieValue);
348                 Exit(EXIT_STATUS_DIETAG);
349         }
350 }
351
352 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const std::string &text)
353 {
354         std::string copy_text = text;
355
356         int MOD_RESULT = 0;
357         FOREACH_RESULT_I(this, I_OnWhoisLine, OnWhoisLine(user, dest, numeric, copy_text));
358
359         if (!MOD_RESULT)
360                 user->WriteServ("%d %s", numeric, copy_text.c_str());
361 }
362
363 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const char* format, ...)
364 {
365         char textbuffer[MAXBUF];
366         va_list argsPtr;
367         va_start (argsPtr, format);
368         vsnprintf(textbuffer, MAXBUF, format, argsPtr);
369         va_end(argsPtr);
370
371         this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));
372 }
373
374 /** Refactored by Brain, Jun 2008. Much faster with some clever O(1) array
375  * lookups and pointer maths.
376  */
377 long InspIRCd::Duration(const std::string &str)
378 {
379         unsigned char multiplier = 0;
380         long total = 0;
381         long times = 1;
382         long subtotal = 0;
383
384         /* Iterate each item in the string, looking for number or multiplier */
385         for (std::string::const_reverse_iterator i = str.rbegin(); i != str.rend(); ++i)
386         {
387                 /* Found a number, queue it onto the current number */
388                 if ((*i >= '0') && (*i <= '9'))
389                 {
390                         subtotal = subtotal + ((*i - '0') * times);
391                         times = times * 10;
392                 }
393                 else
394                 {
395                         /* Found something thats not a number, find out how much
396                          * it multiplies the built up number by, multiply the total
397                          * and reset the built up number.
398                          */
399                         if (subtotal)
400                                 total += subtotal * duration_multi[multiplier];
401
402                         /* Next subtotal please */
403                         subtotal = 0;
404                         multiplier = *i;
405                         times = 1;
406                 }
407         }
408         if (multiplier)
409         {
410                 total += subtotal * duration_multi[multiplier];
411                 subtotal = 0;
412         }
413         /* Any trailing values built up are treated as raw seconds */
414         return total + subtotal;
415 }
416
417 bool InspIRCd::ULine(const char* server)
418 {
419         if (!server)
420                 return false;
421         if (!*server)
422                 return true;
423
424         return (Config->ulines.find(server) != Config->ulines.end());
425 }
426
427 bool InspIRCd::SilentULine(const char* server)
428 {
429         std::map<irc::string,bool>::iterator n = Config->ulines.find(server);
430         if (n != Config->ulines.end())
431                 return n->second;
432         else return false;
433 }
434
435 std::string InspIRCd::TimeString(time_t curtime)
436 {
437         return std::string(ctime(&curtime),24);
438 }
439