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