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