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