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