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