]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/users.cpp
Fixed some weirdass whowas stuff
[user/henk/code/inspircd.git] / src / users.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include "inspircd_config.h" 
20 #include "channels.h"
21 #include "connection.h"
22 #include "users.h"
23 #include "inspircd.h"
24 #include <stdio.h>
25 #ifdef THREADED_DNS
26 #include <pthread.h>
27 #include <signal.h>
28 #endif
29 #include "inspstring.h"
30 #include "commands.h"
31 #include "helperfuncs.h"
32 #include "typedefs.h"
33 #include "socketengine.h"
34 #include "hashcomp.h"
35 #include "message.h"
36 #include "wildcard.h"
37 #include "xline.h"
38 #include "cull_list.h"
39
40 extern InspIRCd* ServerInstance;
41 extern int WHOWAS_STALE;
42 extern int WHOWAS_MAX;
43 extern std::vector<Module*> modules;
44 extern std::vector<ircd_module*> factory;
45 extern std::vector<InspSocket*> module_sockets;
46 extern int MODCOUNT;
47 extern InspSocket* socket_ref[MAX_DESCRIPTORS];
48 extern time_t TIME;
49 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
50 extern ServerConfig *Config;
51 extern user_hash clientlist;
52 extern whowas_hash whowas;
53 extern std::vector<userrec*> local_users;
54
55 std::vector<userrec*> all_opers;
56
57 template<typename T> inline string ConvToStr(const T &in)
58 {
59         stringstream tmp;
60         if (!(tmp << in)) return string();
61         return tmp.str();
62 }
63
64 userrec::userrec()
65 {
66         // the PROPER way to do it, AVOID bzero at *ALL* costs
67         *password = *nick = *ident = *host = *dhost = *fullname = *modes = *awaymsg = *oper = 0;
68         server = (char*)FindServerNamePtr(Config->ServerName);
69         reset_due = TIME;
70         lines_in = fd = lastping = signon = idle_lastmsg = nping = registered = 0;
71         timeout = flood = port = bytes_in = bytes_out = cmds_in = cmds_out = 0;
72         haspassed = dns_done = false;
73         recvq = "";
74         sendq = "";
75         chans.clear();
76         invites.clear();
77 }
78
79 userrec::~userrec()
80 {
81 }
82
83 void userrec::MakeHost(char* nhost)
84 {
85         /* This is much faster than snprintf */
86         char* t = nhost;
87         for(char* n = ident; *n; n++)
88                 *t++ = *n;
89         *t++ = '@';
90         for(char* n = host; *n; n++)
91                 *t++ = *n;
92         *t = 0;
93 }
94
95 void userrec::CloseSocket()
96 {
97         shutdown(this->fd,2);
98         close(this->fd);
99 }
100  
101 char* userrec::GetFullHost()
102 {
103         static char result[MAXBUF];
104         char* t = result;
105         for(char* n = nick; *n; n++)
106                 *t++ = *n;
107         *t++ = '!';
108         for(char* n = ident; *n; n++)
109                 *t++ = *n;
110         *t++ = '@';
111         for(char* n = dhost; *n; n++)
112                 *t++ = *n;
113         *t = 0;
114         return result;
115 }
116
117 char* userrec::MakeWildHost()
118 {
119         static char nresult[MAXBUF];
120         char* t = nresult;
121         *t++ = '*';     *t++ = '!';
122         *t++ = '*';     *t++ = '@';
123         for(char* n = dhost; *n; n++)
124                 *t++ = *n;
125         *t = 0;
126         return nresult;
127 }
128
129 int userrec::ReadData(void* buffer, size_t size)
130 {
131         if (this->fd > -1)
132         {
133                 return read(this->fd, buffer, size);
134         }
135         else return 0;
136 }
137
138
139 char* userrec::GetFullRealHost()
140 {
141         static char fresult[MAXBUF];
142         char* t = fresult;
143         for(char* n = nick; *n; n++)
144                 *t++ = *n;
145         *t++ = '!';
146         for(char* n = ident; *n; n++)
147                 *t++ = *n;
148         *t++ = '@';
149         for(char* n = host; *n; n++)
150                 *t++ = *n;
151         *t = 0;
152         return fresult;
153 }
154
155 bool userrec::IsInvited(irc::string &channel)
156 {
157         for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
158         {
159                 irc::string compare = i->channel;
160                 if (compare == channel)
161                 {
162                         return true;
163                 }
164         }
165         return false;
166 }
167
168 InvitedList* userrec::GetInviteList()
169 {
170         return &invites;
171 }
172
173 void userrec::InviteTo(irc::string &channel)
174 {
175         Invited i;
176         i.channel = channel;
177         invites.push_back(i);
178 }
179
180 void userrec::RemoveInvite(irc::string &channel)
181 {
182         log(DEBUG,"Removing invites");
183         if (invites.size())
184         {
185                 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
186                 {
187                         irc::string compare = i->channel;
188                         if (compare == channel)
189                         {
190                                 invites.erase(i);
191                                 return;
192                         }
193                 }
194         }
195 }
196
197 bool userrec::HasPermission(std::string &command)
198 {
199         char TypeName[MAXBUF],Classes[MAXBUF],ClassName[MAXBUF],CommandList[MAXBUF];
200         char* mycmd;
201         char* savept;
202         char* savept2;
203         
204         // users on u-lined servers can completely bypass
205         // all permissions based checks.
206         //
207         // of course, if this is sent to a remote server and this
208         // server is not ulined there, then that other server
209         // silently drops the command.
210         if (is_uline(this->server))
211                 return true;
212         
213         // are they even an oper at all?
214         if (*this->oper)
215         {
216                 for (int j =0; j < Config->ConfValueEnum("type",&Config->config_f); j++)
217                 {
218                         Config->ConfValue("type","name",j,TypeName,&Config->config_f);
219                         if (!strcmp(TypeName,this->oper))
220                         {
221                                 Config->ConfValue("type","classes",j,Classes,&Config->config_f);
222                                 char* myclass = strtok_r(Classes," ",&savept);
223                                 while (myclass)
224                                 {
225                                         for (int k =0; k < Config->ConfValueEnum("class",&Config->config_f); k++)
226                                         {
227                                                 Config->ConfValue("class","name",k,ClassName,&Config->config_f);
228                                                 if (!strcmp(ClassName,myclass))
229                                                 {
230                                                         Config->ConfValue("class","commands",k,CommandList,&Config->config_f);
231                                                         mycmd = strtok_r(CommandList," ",&savept2);
232                                                         while (mycmd)
233                                                         {
234                                                                 if ((!strcasecmp(mycmd,command.c_str())) || (*mycmd == '*'))
235                                                                 {
236                                                                         return true;
237                                                                 }
238                                                                 mycmd = strtok_r(NULL," ",&savept2);
239                                                         }
240                                                 }
241                                         }
242                                         myclass = strtok_r(NULL," ",&savept);
243                                 }
244                         }
245                 }
246         }
247         return false;
248 }
249
250
251 bool userrec::AddBuffer(std::string a)
252 {
253         std::string b = "";
254         for (unsigned int i = 0; i < a.length(); i++)
255                 if ((a[i] != '\r') && (a[i] != '\0') && (a[i] != 7))
256                         b = b + a[i];
257         std::stringstream stream(recvq);
258         stream << b;
259         recvq = stream.str();
260         unsigned int i = 0;
261         // count the size of the first line in the buffer.
262         while (i < recvq.length())
263         {
264                 if (recvq[i++] == '\n')
265                         break;
266         }
267         if (recvq.length() > (unsigned)this->recvqmax)
268         {
269                 this->SetWriteError("RecvQ exceeded");
270                 WriteOpers("*** User %s RecvQ of %d exceeds connect class maximum of %d",this->nick,recvq.length(),this->recvqmax);
271         }
272         // return false if we've had more than 600 characters WITHOUT
273         // a carriage return (this is BAD, drop the socket)
274         return (i < 600);
275 }
276
277 bool userrec::BufferIsReady()
278 {
279         unsigned int t = recvq.length();
280         for (unsigned int i = 0; i < t; i++)
281                 if (recvq[i] == '\n')
282                         return true;
283         return false;
284 }
285
286 void userrec::ClearBuffer()
287 {
288         recvq = "";
289 }
290
291 std::string userrec::GetBuffer()
292 {
293         if (recvq == "")
294                 return "";
295         char* line = (char*)recvq.c_str();
296         std::string ret = "";
297         while ((*line != '\n') && (*line))
298         {
299                 ret = ret + *line;
300                 line++;
301         }
302         if ((*line == '\n') || (*line == '\r'))
303                 line++;
304         recvq = line;
305         return ret;
306 }
307
308 void userrec::AddWriteBuf(std::string data)
309 {
310         if (this->GetWriteError() != "")
311                 return;
312         if (sendq.length() + data.length() > (unsigned)this->sendqmax)
313         {
314                 /* Fix by brain - Set the error text BEFORE calling writeopers, because
315                  * if we dont it'll recursively  call here over and over again trying
316                  * to repeatedly add the text to the sendq!
317                  */
318                 this->SetWriteError("SendQ exceeded");
319                 WriteOpers("*** User %s SendQ of %d exceeds connect class maximum of %d",this->nick,sendq.length() + data.length(),this->sendqmax);
320                 return;
321         }
322         std::stringstream stream;
323         stream << sendq << data;
324         sendq = stream.str();
325 }
326
327 // send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it)
328 void userrec::FlushWriteBuf()
329 {
330         if ((sendq.length()) && (this->fd != FD_MAGIC_NUMBER))
331         {
332                 char* tb = (char*)this->sendq.c_str();
333                 int n_sent = write(this->fd,tb,this->sendq.length());
334                 if (n_sent == -1)
335                 {
336                         this->SetWriteError(strerror(errno));
337                 }
338                 else
339                 {
340                         // advance the queue
341                         tb += n_sent;
342                         this->sendq = tb;
343                         // update the user's stats counters
344                         this->bytes_out += n_sent;
345                         this->cmds_out++;
346                 }
347         }
348 }
349
350 void userrec::SetWriteError(std::string error)
351 {
352         log(DEBUG,"Setting error string for %s to '%s'",this->nick,error.c_str());
353         // don't try to set the error twice, its already set take the first string.
354         if (this->WriteError == "")
355                 this->WriteError = error;
356 }
357
358 std::string userrec::GetWriteError()
359 {
360         return this->WriteError;
361 }
362
363 void AddOper(userrec* user)
364 {
365         log(DEBUG,"Oper added to optimization list");
366         all_opers.push_back(user);
367 }
368
369 void DeleteOper(userrec* user)
370 {
371         for (std::vector<userrec*>::iterator a = all_opers.begin(); a < all_opers.end(); a++)
372         {
373                 if (*a == user)
374                 {
375                         log(DEBUG,"Oper removed from optimization list");
376                         all_opers.erase(a);
377                         return;
378                 }
379         }
380 }
381
382 void kill_link(userrec *user,const char* r)
383 {
384         user_hash::iterator iter = clientlist.find(user->nick);
385
386         char reason[MAXBUF];
387
388         strlcpy(reason,r,MAXBUF);
389
390         if (strlen(reason)>MAXQUIT)
391         {
392                 reason[MAXQUIT-1] = '\0';
393         }
394
395         log(DEBUG,"kill_link: %s '%s'",user->nick,reason);
396         Write(user->fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,reason);
397         log(DEBUG,"closing fd %d",user->fd);
398
399         if (user->registered == 7) {
400                 purge_empty_chans(user);
401                 FOREACH_MOD(I_OnUserQuit,OnUserQuit(user,reason));
402                 WriteCommonExcept(user,"QUIT :%s",reason);
403         }
404
405         user->FlushWriteBuf();
406
407         FOREACH_MOD(I_OnUserDisconnect,OnUserDisconnect(user));
408
409         if (user->fd > -1)
410         {
411                 if (Config->GetIOHook(user->port))
412                 {
413                         try
414                         {
415                                 Config->GetIOHook(user->port)->OnRawSocketClose(user->fd);
416                         }
417                         catch (ModuleException& modexcept)
418                         {
419                                 log(DEBUG,"Module exception cought: %s",modexcept.GetReason()); \
420                         }
421                 }
422                 ServerInstance->SE->DelFd(user->fd);
423                 user->CloseSocket();
424         }
425
426         // this must come before the WriteOpers so that it doesnt try to fill their buffer with anything
427         // if they were an oper with +s.
428         if (user->registered == 7) {
429                 // fix by brain: only show local quits because we only show local connects (it just makes SENSE)
430                 if (user->fd > -1)
431                         WriteOpers("*** Client exiting: %s!%s@%s [%s]",user->nick,user->ident,user->host,reason);
432                 AddWhoWas(user);
433         }
434
435         if (iter != clientlist.end())
436         {
437                 log(DEBUG,"deleting user hash value %lx",(unsigned long)user);
438                 if (user->fd > -1)
439                 {
440                         fd_ref_table[user->fd] = NULL;
441                         if (find(local_users.begin(),local_users.end(),user) != local_users.end())
442                         {
443                                 local_users.erase(find(local_users.begin(),local_users.end(),user));
444                                 log(DEBUG,"Delete local user");
445                         }
446                 }
447                 clientlist.erase(iter);
448         }
449         delete user;
450 }
451
452 void kill_link_silent(userrec *user,const char* r)
453 {
454         user_hash::iterator iter = clientlist.find(user->nick);
455
456         char reason[MAXBUF];
457
458         strlcpy(reason,r,MAXBUF);
459
460         if (strlen(reason)>MAXQUIT)
461         {
462                 reason[MAXQUIT-1] = '\0';
463         }
464
465         log(DEBUG,"kill_link: %s '%s'",user->nick,reason);
466         Write(user->fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,reason);
467         log(DEBUG,"closing fd %lu",(unsigned long)user->fd);
468
469         user->FlushWriteBuf();
470
471         if (user->registered == 7) {
472                 FOREACH_MOD(I_OnUserQuit,OnUserQuit(user,reason));
473                 WriteCommonExcept(user,"QUIT :%s",reason);
474         }
475
476         FOREACH_MOD(I_OnUserDisconnect,OnUserDisconnect(user));
477
478         if (user->fd > -1)
479         {
480                 if (Config->GetIOHook(user->port))
481                 {
482                         try
483                         {
484                                 Config->GetIOHook(user->port)->OnRawSocketClose(user->fd);
485                         }
486                         catch (ModuleException& modexcept)
487                         {
488                                 log(DEBUG,"Module exception cought: %s",modexcept.GetReason()); \
489                         }
490                 }
491                 ServerInstance->SE->DelFd(user->fd);
492                 user->CloseSocket();
493         }
494
495         if (user->registered == 7) {
496                 purge_empty_chans(user);
497         }
498
499         if (iter != clientlist.end())
500         {
501                 log(DEBUG,"deleting user hash value %lu",(unsigned long)user);
502                 if (user->fd > -1)
503                 {
504                         fd_ref_table[user->fd] = NULL;
505                         if (find(local_users.begin(),local_users.end(),user) != local_users.end())
506                         {
507                                 log(DEBUG,"Delete local user");
508                                 local_users.erase(find(local_users.begin(),local_users.end(),user));
509                         }
510                 }
511                 clientlist.erase(iter);
512         }
513         delete user;
514 }
515
516
517 /* adds or updates an entry in the whowas list */
518 void AddWhoWas(userrec* u)
519 {
520         whowas_hash::iterator iter = whowas.find(u->nick);
521         WhoWasUser *a = new WhoWasUser();
522         strlcpy(a->nick,u->nick,NICKMAX-1);
523         strlcpy(a->ident,u->ident,IDENTMAX);
524         strlcpy(a->dhost,u->dhost,160);
525         strlcpy(a->host,u->host,160);
526         strlcpy(a->fullname,u->fullname,MAXGECOS);
527         if (u->server)
528                 strlcpy(a->server,u->server,256);
529         a->signon = u->signon;
530
531         /* MAX_WHOWAS:   max number of /WHOWAS items
532          * WHOWAS_STALE: number of hours before a WHOWAS item is marked as stale and
533          *               can be replaced by a newer one
534          */
535
536         if (iter == whowas.end())
537         {
538                 if (whowas.size() >= (unsigned)WHOWAS_MAX)
539                 {
540                         for (whowas_hash::iterator i = whowas.begin(); i != whowas.end(); i++)
541                         {
542                                 // 3600 seconds in an hour ;)
543                                 if ((i->second->signon)<(TIME-(WHOWAS_STALE*3600)))
544                                 {
545                                         // delete an old one
546                                         if (i->second) delete i->second;
547                                         whowas.erase(i);
548                                         // replace with new one
549                                         whowas[a->nick] = a;
550                                         log(DEBUG,"added WHOWAS entry, purged an old record");
551                                         return;
552                                 }
553                         }
554                         // no space left and user doesnt exist. Don't leave ram in use!
555                         delete a;
556                 }
557                 else
558                 {
559                         log(DEBUG,"added fresh WHOWAS entry");
560                         whowas[a->nick] = a;
561                 }
562         }
563         else
564         {
565                 log(DEBUG,"updated WHOWAS entry");
566                 if (iter->second) delete iter->second;
567                 iter->second = a;
568         }
569 }
570
571 /* add a client connection to the sockets list */
572 void AddClient(int socket, int port, bool iscached, in_addr ip4)
573 {
574         string tempnick;
575         char tn2[MAXBUF];
576         user_hash::iterator iter;
577
578         tempnick = ConvToStr(socket) + "-unknown";
579         sprintf(tn2,"%d-unknown",socket);
580
581         iter = clientlist.find(tempnick);
582
583         // fix by brain.
584         // as these nicknames are 'RFC impossible', we can be sure nobody is going to be
585         // using one as a registered connection. As theyre per fd, we can also safely assume
586         // that we wont have collisions. Therefore, if the nick exists in the list, its only
587         // used by a dead socket, erase the iterator so that the new client may reclaim it.
588         // this was probably the cause of 'server ignores me when i hammer it with reconnects'
589         // issue in earlier alphas/betas
590         if (iter != clientlist.end())
591         {
592                 userrec* goner = iter->second;
593                 delete goner;
594                 clientlist.erase(iter);
595         }
596
597         /*
598          * It is OK to access the value here this way since we know
599          * it exists, we just created it above.
600          *
601          * At NO other time should you access a value in a map or a
602          * hash_map this way.
603          */
604         clientlist[tempnick] = new userrec();
605
606         char *ipaddr = (char*)inet_ntoa(ip4);
607
608         log(DEBUG,"AddClient: %d %d %s",socket,port,ipaddr);
609
610         clientlist[tempnick]->fd = socket;
611         strlcpy(clientlist[tempnick]->nick, tn2,NICKMAX-1);
612         /* We don't know the host yet, dns lookup could still be going on,
613          * so instead we just put the ip address here, for now.
614          */
615         strlcpy(clientlist[tempnick]->host, ipaddr, 160);
616         strlcpy(clientlist[tempnick]->dhost, ipaddr, 160);
617         clientlist[tempnick]->server = (char*)FindServerNamePtr(Config->ServerName);
618         strlcpy(clientlist[tempnick]->ident, "unknown",IDENTMAX);
619         clientlist[tempnick]->registered = 0;
620         clientlist[tempnick]->signon = TIME + Config->dns_timeout;
621         clientlist[tempnick]->lastping = 1;
622         clientlist[tempnick]->ip4 = ip4;
623         clientlist[tempnick]->port = port;
624
625         // set the registration timeout for this user
626         unsigned long class_regtimeout = 90;
627         int class_flood = 0;
628         long class_threshold = 5;
629         long class_sqmax = 262144;      // 256kb
630         long class_rqmax = 4096;        // 4k
631
632         for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
633         {
634                 if (match(ipaddr,i->host.c_str()) && (i->type == CC_ALLOW))
635                 {
636                         class_regtimeout = (unsigned long)i->registration_timeout;
637                         class_flood = i->flood;
638                         clientlist[tempnick]->pingmax = i->pingtime;
639                         class_threshold = i->threshold;
640                         class_sqmax = i->sendqmax;
641                         class_rqmax = i->recvqmax;
642                         break;
643                 }
644         }
645
646         clientlist[tempnick]->nping = TIME+clientlist[tempnick]->pingmax + Config->dns_timeout;
647         clientlist[tempnick]->timeout = TIME+class_regtimeout;
648         clientlist[tempnick]->flood = class_flood;
649         clientlist[tempnick]->threshold = class_threshold;
650         clientlist[tempnick]->sendqmax = class_sqmax;
651         clientlist[tempnick]->recvqmax = class_rqmax;
652
653         ucrec a;
654         a.channel = NULL;
655         a.uc_modes = 0;
656         for (int i = 0; i < MAXCHANS; i++)
657                 clientlist[tempnick]->chans.push_back(a);
658
659         if (local_users.size() > Config->SoftLimit)
660         {
661                 kill_link(clientlist[tempnick],"No more connections allowed");
662                 return;
663         }
664
665         if (local_users.size() >= MAXCLIENTS)
666         {
667                 kill_link(clientlist[tempnick],"No more connections allowed");
668                 return;
669         }
670
671         // this is done as a safety check to keep the file descriptors within range of fd_ref_table.
672         // its a pretty big but for the moment valid assumption:
673         // file descriptors are handed out starting at 0, and are recycled as theyre freed.
674         // therefore if there is ever an fd over 65535, 65536 clients must be connected to the
675         // irc server at once (or the irc server otherwise initiating this many connections, files etc)
676         // which for the time being is a physical impossibility (even the largest networks dont have more
677         // than about 10,000 users on ONE server!)
678         if ((unsigned)socket > MAX_DESCRIPTORS)
679         {
680                 kill_link(clientlist[tempnick],"Server is full");
681                 return;
682         }
683         char* e = matches_exception(ipaddr);
684         if (!e)
685         {
686                 char* r = matches_zline(ipaddr);
687                 if (r)
688                 {
689                         char reason[MAXBUF];
690                         snprintf(reason,MAXBUF,"Z-Lined: %s",r);
691                         kill_link(clientlist[tempnick],reason);
692                         return;
693                 }
694         }
695         fd_ref_table[socket] = clientlist[tempnick];
696         local_users.push_back(clientlist[tempnick]);
697         ServerInstance->SE->AddFd(socket,true,X_ESTAB_CLIENT);
698
699         WriteServ(clientlist[tempnick]->fd,"NOTICE Auth :*** Looking up your hostname...");
700 }
701
702 long FindMatchingGlobal(userrec* user)
703 {
704         long x = 0;
705         for (user_hash::const_iterator a = clientlist.begin(); a != clientlist.end(); a++)
706         {
707                 if (a->second->ip4.s_addr == user->ip4.s_addr)
708                         x++;
709         }
710         return x;
711 }
712
713 long FindMatchingLocal(userrec* user)
714 {
715         long x = 0;
716         for (std::vector<userrec*>::const_iterator a = local_users.begin(); a != local_users.end(); a++)
717         {
718                 userrec* comp = (userrec*)(*a);
719                 if (comp->ip4.s_addr == user->ip4.s_addr)
720                         x++;
721         }
722         return x;
723 }
724
725 void FullConnectUser(userrec* user, CullList* Goners)
726 {
727         ServerInstance->stats->statsConnects++;
728         user->idle_lastmsg = TIME;
729         log(DEBUG,"ConnectUser: %s",user->nick);
730
731         ConnectClass a = GetClass(user);
732         
733         if (a.type == CC_DENY)
734         {
735                 Goners->AddItem(user,"Unauthorised connection");
736                 return;
737         }
738         if ((*(a.pass.c_str())) && (!user->haspassed))
739         {
740                 Goners->AddItem(user,"Invalid password");
741                 return;
742         }
743         if (FindMatchingLocal(user) > a.maxlocal)
744         {
745                 Goners->AddItem(user,"No more connections allowed from your host via this connect class (local)");
746                 WriteOpers("*** WARNING: maximum LOCAL connections (%ld) exceeded for IP %s",a.maxlocal,(char*)inet_ntoa(user->ip4));
747                 return;
748         }
749         else if (FindMatchingGlobal(user) > a.maxglobal)
750         {
751                 Goners->AddItem(user,"No more connections allowed from your host via this connect class (global)");
752                 WriteOpers("*** WARNING: maximum GLOBAL connections (%ld) exceeded for IP %s",a.maxglobal,(char*)inet_ntoa(user->ip4));
753                 return;
754         }
755
756         char match_against[MAXBUF];
757         snprintf(match_against,MAXBUF,"%s@%s",user->ident,user->host);
758         char* e = matches_exception(match_against);
759         if (!e)
760         {
761                 char* r = matches_gline(match_against);
762                 if (r)
763                 {
764                         char reason[MAXBUF];
765                         snprintf(reason,MAXBUF,"G-Lined: %s",r);
766                         Goners->AddItem(user,reason);
767                         return;
768                 }
769                 r = matches_kline(user->host);
770                 if (r)
771                 {
772                         char reason[MAXBUF];
773                         snprintf(reason,MAXBUF,"K-Lined: %s",r);
774                         Goners->AddItem(user,reason);
775                         return;
776                 }
777         }
778
779
780         WriteServ(user->fd,"NOTICE Auth :Welcome to \002%s\002!",Config->Network);
781         WriteServ(user->fd,"001 %s :Welcome to the %s IRC Network %s!%s@%s",user->nick,Config->Network,user->nick,user->ident,user->host);
782         WriteServ(user->fd,"002 %s :Your host is %s, running version %s",user->nick,Config->ServerName,VERSION);
783         WriteServ(user->fd,"003 %s :This server was created %s %s",user->nick,__TIME__,__DATE__);
784         WriteServ(user->fd,"004 %s %s %s iowghrasxRVSCWBG lvhopsmntikrcaqbegIOLQRSKVHGCNT vhobeIaqglk",user->nick,Config->ServerName,VERSION);
785         // anfl @ #ratbox, efnet reminded me that according to the RFC this cant contain more than 13 tokens per line...
786         // so i'd better split it :)
787         std::stringstream out(Config->data005);
788         std::string token = "";
789         std::string line5 = "";
790         int token_counter = 0;
791         while (!out.eof())
792         {
793                 out >> token;
794                 line5 = line5 + token + " ";
795                 token_counter++;
796                 if ((token_counter >= 13) || (out.eof() == true))
797                 {
798                         WriteServ(user->fd,"005 %s %s:are supported by this server",user->nick,line5.c_str());
799                         line5 = "";
800                         token_counter = 0;
801                 }
802         }
803         ShowMOTD(user);
804
805         // fix 3 by brain, move registered = 7 below these so that spurious modes and host changes dont go out
806         // onto the network and produce 'fake direction'
807         FOREACH_MOD(I_OnUserConnect,OnUserConnect(user));
808         FOREACH_MOD(I_OnGlobalConnect,OnGlobalConnect(user));
809         user->registered = 7;
810         WriteOpers("*** Client connecting on port %lu: %s!%s@%s [%s]",(unsigned long)user->port,user->nick,user->ident,user->host,(char*)inet_ntoa(user->ip4));
811 }
812
813 /* re-allocates a nick in the user_hash after they change nicknames,
814  * returns a pointer to the new user as it may have moved */
815
816 userrec* ReHashNick(char* Old, char* New)
817 {
818         //user_hash::iterator newnick;
819         user_hash::iterator oldnick = clientlist.find(Old);
820
821         log(DEBUG,"ReHashNick: %s %s",Old,New);
822
823         if (!strcasecmp(Old,New))
824         {
825                 log(DEBUG,"old nick is new nick, skipping");
826                 return oldnick->second;
827         }
828
829         if (oldnick == clientlist.end()) return NULL; /* doesnt exist */
830
831         log(DEBUG,"ReHashNick: Found hashed nick %s",Old);
832
833         userrec* olduser = oldnick->second;
834         clientlist[New] = olduser;
835         clientlist.erase(oldnick);
836
837         log(DEBUG,"ReHashNick: Nick rehashed as %s",New);
838
839         return clientlist[New];
840 }
841
842 void force_nickchange(userrec* user,const char* newnick)
843 {
844         char nick[MAXBUF];
845         int MOD_RESULT = 0;
846
847         *nick = 0;
848
849         FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(user,newnick));
850         if (MOD_RESULT) {
851                 ServerInstance->stats->statsCollisions++;
852                 kill_link(user,"Nickname collision");
853                 return;
854         }
855         if (matches_qline(newnick))
856         {
857                 ServerInstance->stats->statsCollisions++;
858                 kill_link(user,"Nickname collision");
859                 return;
860         }
861
862         if (user)
863         {
864                 if (newnick)
865                 {
866                         strlcpy(nick,newnick,MAXBUF);
867                 }
868                 if (user->registered == 7)
869                 {
870                         char* pars[1];
871                         pars[0] = nick;
872                         std::string cmd = "NICK";
873                         ServerInstance->Parser->CallHandler(cmd,pars,1,user);
874                 }
875         }
876 }
877