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