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