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