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