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