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