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