]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/helperfuncs.cpp
Finally apply my patch simplifying RSQUIT. Fixes bug #452, reported by Mark. This...
[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 InspIRCd::IsChannel(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 InspIRCd::IsSID(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 (log reopening is done at OnReadConfig stage now instead of rehash) */
285         if (Config->nofork)
286         {
287                 this->Logs->SetupNoFork();
288         }
289         if (!Config->writelog) return true; // Skip opening default log if -nolog
290         Config->MyDir = Config->GetFullProgDir();
291
292         if (!*this->LogFileName)
293         {
294                 if (Config->logpath.empty())
295                 {
296                         Config->logpath = Config->MyDir + "/ircd.log";
297                 }
298
299                 Config->log_file = fopen(Config->logpath.c_str(),"a+");
300         }
301         else
302         {
303                 Config->log_file = fopen(this->LogFileName,"a+");
304         }
305
306         if (!Config->log_file)
307         {
308                 return false;
309         }
310
311         FileWriter* fw = new FileWriter(this, Config->log_file);
312         FileLogStream *f = new FileLogStream(this, (Config->forcedebug ? DEBUG : Config->LogLevel), fw);
313
314         this->Logs->AddLogType("*", f, true);
315
316         return true;
317 }
318
319 void InspIRCd::CheckRoot()
320 {
321         if (geteuid() == 0)
322         {
323                 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
324                 this->Logs->Log("STARTUP",DEFAULT,"Cant start as root");
325                 Exit(EXIT_STATUS_ROOT);
326         }
327 }
328
329 void InspIRCd::CheckDie()
330 {
331         if (*Config->DieValue)
332         {
333                 printf("WARNING: %s\n\n",Config->DieValue);
334                 this->Logs->Log("CONFIG",DEFAULT,"Died because of <die> tag: %s",Config->DieValue);
335                 Exit(EXIT_STATUS_DIETAG);
336         }
337 }
338
339 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const std::string &text)
340 {
341         std::string copy_text = text;
342
343         int MOD_RESULT = 0;
344         FOREACH_RESULT_I(this, I_OnWhoisLine, OnWhoisLine(user, dest, numeric, copy_text));
345
346         if (!MOD_RESULT)
347                 user->WriteServ("%d %s", numeric, copy_text.c_str());
348 }
349
350 void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const char* format, ...)
351 {
352         char textbuffer[MAXBUF];
353         va_list argsPtr;
354         va_start (argsPtr, format);
355         vsnprintf(textbuffer, MAXBUF, format, argsPtr);
356         va_end(argsPtr);
357
358         this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));
359 }
360
361 /** Refactored by Brain, Jun 2008. Much faster with some clever O(1) array
362  * lookups and pointer maths.
363  */
364 long InspIRCd::Duration(const std::string &str)
365 {
366         unsigned char multiplier = 0;
367         long total = 0;
368         long times = 1;
369         long subtotal = 0;
370
371         /* Iterate each item in the string, looking for number or multiplier */
372         for (std::string::const_reverse_iterator i = str.rbegin(); i != str.rend(); ++i)
373         {
374                 /* Found a number, queue it onto the current number */
375                 if ((*i >= '0') && (*i <= '9'))
376                 {
377                         subtotal = subtotal + ((*i - '0') * times);
378                         times = times * 10;
379                 }
380                 else
381                 {
382                         /* Found something thats not a number, find out how much
383                          * it multiplies the built up number by, multiply the total
384                          * and reset the built up number.
385                          */
386                         if (subtotal)
387                                 total += subtotal * duration_multi[multiplier];
388
389                         /* Next subtotal please */
390                         subtotal = 0;
391                         multiplier = *i;
392                         times = 1;
393                 }
394         }
395         if (multiplier)
396         {
397                 total += subtotal * duration_multi[multiplier];
398                 subtotal = 0;
399         }
400         /* Any trailing values built up are treated as raw seconds */
401         return total + subtotal;
402 }
403
404 bool InspIRCd::ULine(const char* sserver)
405 {
406         if (!sserver)
407                 return false;
408         if (!*sserver)
409                 return true;
410
411         return (Config->ulines.find(sserver) != Config->ulines.end());
412 }
413
414 bool InspIRCd::SilentULine(const char* sserver)
415 {
416         std::map<irc::string,bool>::iterator n = Config->ulines.find(sserver);
417         if (n != Config->ulines.end())
418                 return n->second;
419         else return false;
420 }
421
422 std::string InspIRCd::TimeString(time_t curtime)
423 {
424         return std::string(ctime(&curtime),24);
425 }
426