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