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