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