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