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