]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/users.cpp
Refactored user modes to work like the channel modes - core and module data now the...
[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 = *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         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         memset(modes,0,sizeof(modes));
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                         if (errno != EAGAIN)
421                                 this->SetWriteError(strerror(errno));
422                 }
423                 else
424                 {
425                         // advance the queue
426                         tb += n_sent;
427                         this->sendq = tb;
428                         // update the user's stats counters
429                         this->bytes_out += n_sent;
430                         this->cmds_out++;
431                 }
432         }
433 }
434
435 void userrec::SetWriteError(const std::string &error)
436 {
437         log(DEBUG,"Setting error string for %s to '%s'",this->nick,error.c_str());
438         // don't try to set the error twice, its already set take the first string.
439         if (this->WriteError == "")
440                 this->WriteError = error;
441 }
442
443 const char* userrec::GetWriteError()
444 {
445         return this->WriteError.c_str();
446 }
447
448 void AddOper(userrec* user)
449 {
450         log(DEBUG,"Oper added to optimization list");
451         all_opers.push_back(user);
452 }
453
454 void DeleteOper(userrec* user)
455 {
456         for (std::vector<userrec*>::iterator a = all_opers.begin(); a < all_opers.end(); a++)
457         {
458                 if (*a == user)
459                 {
460                         log(DEBUG,"Oper removed from optimization list");
461                         all_opers.erase(a);
462                         return;
463                 }
464         }
465 }
466
467 void kill_link(userrec *user,const char* r)
468 {
469         user_hash::iterator iter = clientlist.find(user->nick);
470
471 /*
472  * I'm pretty sure returning here is causing a desync when part of the net thinks a user is gone,
473  * and another part doesn't. We want to broadcast the quit/kill before bailing so the net stays in sync.
474  *
475  * I can't imagine this blowing up, so I'm commenting it out. We still check
476  * before playing with a bad iterator below in our if(). DISCUSS THIS BEFORE YOU DO ANYTHING. --w00t
477  *
478  *      if (iter == clientlist.end())
479  *              return;
480  */
481
482         char reason[MAXBUF];
483
484         strlcpy(reason,r,MAXQUIT-1);
485         log(DEBUG,"kill_link: %s %d '%s'",user->nick,user->fd,reason);
486         
487         if (IS_LOCAL(user))
488                 Write(user->fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,reason);
489
490         if (user->registered == 7)
491         {
492                 purge_empty_chans(user);
493                 FOREACH_MOD(I_OnUserQuit,OnUserQuit(user,reason));
494                 WriteCommonExcept(user,"QUIT :%s",reason);
495         }
496
497         if (IS_LOCAL(user))
498                 user->FlushWriteBuf();
499
500         FOREACH_MOD(I_OnUserDisconnect,OnUserDisconnect(user));
501
502         if (IS_LOCAL(user))
503         {
504                 if (Config->GetIOHook(user->port))
505                 {
506                         try
507                         {
508                                 Config->GetIOHook(user->port)->OnRawSocketClose(user->fd);
509                         }
510                         catch (ModuleException& modexcept)
511                         {
512                                 log(DEBUG,"Module exception cought: %s",modexcept.GetReason());
513                         }
514                 }
515                 
516                 ServerInstance->SE->DelFd(user->fd);
517                 user->CloseSocket();
518         }
519
520         /*
521          * this must come before the WriteOpers so that it doesnt try to fill their buffer with anything
522          * if they were an oper with +s.
523          *
524          * XXX -
525          * In the current implementation, we only show local quits, as we only show local connects. With 
526          * the proposed implmentation of snomasks however, this will likely change in the (near?) future.
527          */
528         if (user->registered == 7)
529         {
530                 if (IS_LOCAL(user))
531                         WriteOpers("*** Client exiting: %s!%s@%s [%s]",user->nick,user->ident,user->host,reason);
532                 AddWhoWas(user);
533         }
534
535         if (iter != clientlist.end())
536         {
537                 log(DEBUG,"deleting user hash value %lx",(unsigned long)user);
538                 if (IS_LOCAL(user))
539                 {
540                         fd_ref_table[user->fd] = NULL;
541                         if (find(local_users.begin(),local_users.end(),user) != local_users.end())
542                         {
543                                 local_users.erase(find(local_users.begin(),local_users.end(),user));
544                                 log(DEBUG,"Delete local user");
545                         }
546                 }
547                 clientlist.erase(iter);
548                 DELETE(user);
549         }
550 }
551
552 WhoWasGroup::WhoWasGroup(userrec* user) : host(NULL), dhost(NULL), ident(NULL), server(NULL), gecos(NULL), signon(user->signon)
553 {
554         this->host = strdup(user->host);
555         this->dhost = strdup(user->dhost);
556         this->ident = strdup(user->ident);
557         this->server = user->server;
558         this->gecos = strdup(user->fullname);
559 }
560
561 WhoWasGroup::~WhoWasGroup()
562 {
563         if (host)
564                 free(host);
565         if (dhost)
566                 free(dhost);
567         if (ident)
568                 free(ident);
569         if (gecos)
570                 free(gecos);
571 }
572
573 /* adds or updates an entry in the whowas list */
574 void AddWhoWas(userrec* u)
575 {
576         whowas_users::iterator iter = whowas.find(u->nick);
577         
578         if (iter == whowas.end())
579         {
580                 whowas_set* n = new whowas_set;
581                 WhoWasGroup *a = new WhoWasGroup(u);
582                 n->push_back(a);
583                 whowas[u->nick] = n;
584         }
585         else
586         {
587                 whowas_set* group = (whowas_set*)iter->second;
588                 
589                 if (group->size() > 10)
590                 {
591                         WhoWasGroup *a = (WhoWasGroup*)*(group->begin());
592                         DELETE(a);
593                         group->pop_front();
594                 }
595                 
596                 WhoWasGroup *a = new WhoWasGroup(u);
597                 group->push_back(a);
598         }
599 }
600
601 /* every hour, run this function which removes all entries over 3 days */
602 void MaintainWhoWas(time_t TIME)
603 {
604         for (whowas_users::iterator iter = whowas.begin(); iter != whowas.end(); iter++)
605         {
606                 whowas_set* n = (whowas_set*)iter->second;
607                 if (n->size())
608                 {
609                         while ((n->begin() != n->end()) && ((*n->begin())->signon < TIME - 259200)) // 3 days
610                         {
611                                 WhoWasGroup *a = *(n->begin());
612                                 DELETE(a);
613                                 n->erase(n->begin());
614                         }
615                 }
616         }
617 }
618
619 /* add a client connection to the sockets list */
620 void AddClient(int socket, int port, bool iscached, in_addr ip4)
621 {
622         std::string tempnick = ConvToStr(socket) + "-unknown";
623         user_hash::iterator iter = clientlist.find(tempnick);
624         const char *ipaddr = inet_ntoa(ip4);
625         int j = 0;
626
627         /*
628          * fix by brain.
629          * as these nicknames are 'RFC impossible', we can be sure nobody is going to be
630          * using one as a registered connection. As they are per fd, we can also safely assume
631          * that we wont have collisions. Therefore, if the nick exists in the list, its only
632          * used by a dead socket, erase the iterator so that the new client may reclaim it.
633          * this was probably the cause of 'server ignores me when i hammer it with reconnects'
634          * issue in earlier alphas/betas
635          */
636         if (iter != clientlist.end())
637         {
638                 userrec* goner = iter->second;
639                 DELETE(goner);
640                 clientlist.erase(iter);
641         }
642
643         log(DEBUG,"AddClient: %d %d %s",socket,port,ipaddr);
644         
645         clientlist[tempnick] = new userrec();
646         clientlist[tempnick]->fd = socket;
647         strlcpy(clientlist[tempnick]->nick,tempnick.c_str(),NICKMAX-1);
648
649         /* Smarter than your average bear^H^H^H^Hset of strlcpys. */
650         for (char* temp = (char*)ipaddr; *temp && j < 64; temp++, j++)
651                 clientlist[tempnick]->dhost[j] = clientlist[tempnick]->host[j] = *temp;
652         clientlist[tempnick]->dhost[j] = clientlist[tempnick]->host[j] = 0;
653
654         clientlist[tempnick]->server = (char*)FindServerNamePtr(Config->ServerName);
655         /* We don't need range checking here, we KNOW 'unknown\0' will fit into the ident field. */
656         strcpy(clientlist[tempnick]->ident, "unknown");
657
658         clientlist[tempnick]->registered = 0;
659         clientlist[tempnick]->signon = TIME + Config->dns_timeout;
660         clientlist[tempnick]->lastping = 1;
661         clientlist[tempnick]->ip4 = ip4;
662         clientlist[tempnick]->port = port;
663
664         // set the registration timeout for this user
665         unsigned long class_regtimeout = 90;
666         int class_flood = 0;
667         long class_threshold = 5;
668         long class_sqmax = 262144;      // 256kb
669         long class_rqmax = 4096;        // 4k
670
671         for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
672         {
673                 if ((i->type == CC_ALLOW) && (match(ipaddr,i->host.c_str())))
674                 {
675                         class_regtimeout = (unsigned long)i->registration_timeout;
676                         class_flood = i->flood;
677                         clientlist[tempnick]->pingmax = i->pingtime;
678                         class_threshold = i->threshold;
679                         class_sqmax = i->sendqmax;
680                         class_rqmax = i->recvqmax;
681                         break;
682                 }
683         }
684
685         clientlist[tempnick]->nping = TIME+clientlist[tempnick]->pingmax + Config->dns_timeout;
686         clientlist[tempnick]->timeout = TIME+class_regtimeout;
687         clientlist[tempnick]->flood = class_flood;
688         clientlist[tempnick]->threshold = class_threshold;
689         clientlist[tempnick]->sendqmax = class_sqmax;
690         clientlist[tempnick]->recvqmax = class_rqmax;
691
692         fd_ref_table[socket] = clientlist[tempnick];
693         local_users.push_back(clientlist[tempnick]);
694
695         if (local_users.size() > Config->SoftLimit)
696         {
697                 kill_link(clientlist[tempnick],"No more connections allowed");
698                 return;
699         }
700
701         if (local_users.size() >= MAXCLIENTS)
702         {
703                 kill_link(clientlist[tempnick],"No more connections allowed");
704                 return;
705         }
706
707         /*
708          * XXX -
709          * this is done as a safety check to keep the file descriptors within range of fd_ref_table.
710          * its a pretty big but for the moment valid assumption:
711          * file descriptors are handed out starting at 0, and are recycled as theyre freed.
712          * therefore if there is ever an fd over 65535, 65536 clients must be connected to the
713          * irc server at once (or the irc server otherwise initiating this many connections, files etc)
714          * which for the time being is a physical impossibility (even the largest networks dont have more
715          * than about 10,000 users on ONE server!)
716          */
717         if ((unsigned)socket >= MAX_DESCRIPTORS)
718         {
719                 kill_link(clientlist[tempnick],"Server is full");
720                 return;
721         }
722         char* e = matches_exception(ipaddr);
723         if (!e)
724         {
725                 char* r = matches_zline(ipaddr);
726                 if (r)
727                 {
728                         char reason[MAXBUF];
729                         snprintf(reason,MAXBUF,"Z-Lined: %s",r);
730                         kill_link(clientlist[tempnick],reason);
731                         return;
732                 }
733         }
734
735         if (socket > -1)
736         {
737                 ServerInstance->SE->AddFd(socket,true,X_ESTAB_CLIENT);
738         }
739
740         WriteServ(clientlist[tempnick]->fd,"NOTICE Auth :*** Looking up your hostname...");
741 }
742
743 long FindMatchingGlobal(userrec* user)
744 {
745         long x = 0;
746         for (user_hash::const_iterator a = clientlist.begin(); a != clientlist.end(); a++)
747         {
748                 if (a->second->ip4.s_addr == user->ip4.s_addr)
749                         x++;
750         }
751         return x;
752 }
753
754 long FindMatchingLocal(userrec* user)
755 {
756         long x = 0;
757         for (std::vector<userrec*>::const_iterator a = local_users.begin(); a != local_users.end(); a++)
758         {
759                 userrec* comp = (userrec*)(*a);
760                 if (comp->ip4.s_addr == user->ip4.s_addr)
761                         x++;
762         }
763         return x;
764 }
765
766 void FullConnectUser(userrec* user, CullList* Goners)
767 {
768         ServerInstance->stats->statsConnects++;
769         user->idle_lastmsg = TIME;
770         log(DEBUG,"ConnectUser: %s",user->nick);
771
772         ConnectClass a = GetClass(user);
773         
774         if (a.type == CC_DENY)
775         {
776                 Goners->AddItem(user,"Unauthorised connection");
777                 return;
778         }
779         
780         if ((*(a.pass.c_str())) && (!user->haspassed))
781         {
782                 Goners->AddItem(user,"Invalid password");
783                 return;
784         }
785         
786         if (FindMatchingLocal(user) > a.maxlocal)
787         {
788                 Goners->AddItem(user,"No more connections allowed from your host via this connect class (local)");
789                 WriteOpers("*** WARNING: maximum LOCAL connections (%ld) exceeded for IP %s",a.maxlocal,(char*)inet_ntoa(user->ip4));
790                 return;
791         }
792         else if (FindMatchingGlobal(user) > a.maxglobal)
793         {
794                 Goners->AddItem(user,"No more connections allowed from your host via this connect class (global)");
795                 WriteOpers("*** WARNING: maximum GLOBAL connections (%ld) exceeded for IP %s",a.maxglobal,(char*)inet_ntoa(user->ip4));
796                 return;
797         }
798
799         char match_against[MAXBUF];
800         snprintf(match_against,MAXBUF,"%s@%s",user->ident,user->host);
801         char* e = matches_exception(match_against);
802         
803         if (!e)
804         {
805                 char* r = matches_gline(match_against);
806                 
807                 if (r)
808                 {
809                         char reason[MAXBUF];
810                         snprintf(reason,MAXBUF,"G-Lined: %s",r);
811                         Goners->AddItem(user,reason);
812                         return;
813                 }
814                 
815                 r = matches_kline(match_against);
816                 
817                 if (r)
818                 {
819                         char reason[MAXBUF];
820                         snprintf(reason,MAXBUF,"K-Lined: %s",r);
821                         Goners->AddItem(user,reason);
822                         return;
823                 }
824         }
825
826
827         WriteServ(user->fd,"NOTICE Auth :Welcome to \002%s\002!",Config->Network);
828         WriteServ(user->fd,"001 %s :Welcome to the %s IRC Network %s!%s@%s",user->nick,Config->Network,user->nick,user->ident,user->host);
829         WriteServ(user->fd,"002 %s :Your host is %s, running version %s",user->nick,Config->ServerName,VERSION);
830         WriteServ(user->fd,"003 %s :This server was created %s %s",user->nick,__TIME__,__DATE__);
831         WriteServ(user->fd,"004 %s %s %s iowghrasxRVSCWBG lvhopsmntikrcaqbegIOLQRSKVHGCNT vhobeIaqglk",user->nick,Config->ServerName,VERSION);
832         
833         // anfl @ #ratbox, efnet reminded me that according to the RFC this cant contain more than 13 tokens per line...
834         // so i'd better split it :)
835         std::stringstream out(Config->data005);
836         std::string token = "";
837         std::string line5 = "";
838         int token_counter = 0;
839         
840         while (!out.eof())
841         {
842                 out >> token;
843                 line5 = line5 + token + " ";
844                 token_counter++;
845                 
846                 if ((token_counter >= 13) || (out.eof() == true))
847                 {
848                         WriteServ(user->fd,"005 %s %s:are supported by this server",user->nick,line5.c_str());
849                         line5 = "";
850                         token_counter = 0;
851                 }
852         }
853         
854         ShowMOTD(user);
855
856         /*
857          * fix 3 by brain, move registered = 7 below these so that spurious modes and host
858          * changes dont go out onto the network and produce 'fake direction'.
859          */
860         FOREACH_MOD(I_OnUserConnect,OnUserConnect(user));
861         FOREACH_MOD(I_OnGlobalConnect,OnGlobalConnect(user));
862         user->registered = 7;
863         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));
864 }
865
866 /** ReHashNick()
867  * re-allocates a nick in the user_hash after they change nicknames,
868  * returns a pointer to the new user as it may have moved
869  */
870 userrec* ReHashNick(char* Old, char* New)
871 {
872         //user_hash::iterator newnick;
873         user_hash::iterator oldnick = clientlist.find(Old);
874
875         log(DEBUG,"ReHashNick: %s %s",Old,New);
876
877         if (!strcasecmp(Old,New))
878         {
879                 log(DEBUG,"old nick is new nick, skipping");
880                 return oldnick->second;
881         }
882
883         if (oldnick == clientlist.end())
884                 return NULL; /* doesnt exist */
885
886         log(DEBUG,"ReHashNick: Found hashed nick %s",Old);
887
888         userrec* olduser = oldnick->second;
889         clientlist[New] = olduser;
890         clientlist.erase(oldnick);
891
892         log(DEBUG,"ReHashNick: Nick rehashed as %s",New);
893
894         return clientlist[New];
895 }
896
897 void force_nickchange(userrec* user,const char* newnick)
898 {
899         char nick[MAXBUF];
900         int MOD_RESULT = 0;
901
902         *nick = 0;
903
904         FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(user,newnick));
905         
906         if (MOD_RESULT)
907         {
908                 ServerInstance->stats->statsCollisions++;
909                 kill_link(user,"Nickname collision");
910                 return;
911         }
912         
913         if (matches_qline(newnick))
914         {
915                 ServerInstance->stats->statsCollisions++;
916                 kill_link(user,"Nickname collision");
917                 return;
918         }
919
920         if (user)
921         {
922                 if (newnick)
923                 {
924                         strlcpy(nick,newnick,MAXBUF-1);
925                 }
926
927                 if (user->registered == 7)
928                 {
929                         char* pars[1];
930                         
931                         pars[0] = nick;
932                         std::string cmd = "NICK";
933                         ServerInstance->Parser->CallHandler(cmd,pars,1,user);
934                 }
935         }
936 }