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