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