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