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