]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/mode.cpp
Save ourselves 3 dereferences
[user/henk/code/inspircd.git] / src / mode.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 "inspircd.h"
21 #include "configreader.h"
22 #include <unistd.h>
23 #include <sys/errno.h>
24 #include <time.h>
25 #include <string>
26 #include "hash_map.h"
27 #include <map>
28 #include <sstream>
29 #include <vector>
30 #include <deque>
31 #include "connection.h"
32 #include "users.h"
33 #include "ctables.h"
34 #include "globals.h"
35 #include "modules.h"
36 #include "dynamic.h"
37 #include "wildcard.h"
38 #include "message.h"
39 #include "commands.h"
40 #include "xline.h"
41 #include "inspstring.h"
42 #include "helperfuncs.h"
43 #include "mode.h"
44
45 /* +s (secret) */
46 #include "modes/cmode_s.h"
47 /* +p (private) */
48 #include "modes/cmode_p.h"
49 /* +b (bans) */
50 #include "modes/cmode_b.h"
51 /* +m (moderated) */
52 #include "modes/cmode_m.h"
53
54 extern int MODCOUNT;
55 extern std::vector<Module*> modules;
56 extern std::vector<ircd_module*> factory;
57 extern InspIRCd* ServerInstance;
58 extern ServerConfig* Config;
59
60 extern time_t TIME;
61
62 ModeHandler::ModeHandler(char modeletter, int parameters_on, int parameters_off, bool listmode, ModeType type, bool operonly)
63         : mode(modeletter), n_params_on(parameters_on), n_params_off(parameters_off), list(listmode), m_type(type), oper(operonly)
64 {
65 }
66
67 ModeHandler::~ModeHandler()
68 {
69 }
70
71 bool ModeHandler::IsListMode()
72 {
73         return list;
74 }
75
76 ModeType ModeHandler::GetModeType()
77 {
78         return m_type;
79 }
80
81 bool ModeHandler::NeedsOper()
82 {
83         return oper;
84 }
85
86 int ModeHandler::GetNumParams(bool adding)
87 {
88         return adding ? n_params_on : n_params_off;
89 }
90
91 char ModeHandler::GetModeChar()
92 {
93         return mode;
94 }
95
96 ModeAction ModeHandler::OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
97 {
98         return MODEACTION_DENY;
99 }
100
101 void ModeHandler::DisplayList(userrec* user, chanrec* channel)
102 {
103 }
104
105 bool ModeHandler::CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
106 {
107         return (ours < theirs);
108 }
109
110 ModeWatcher::ModeWatcher(char modeletter, ModeType type) : mode(modeletter), m_type(type)
111 {
112 }
113
114 ModeWatcher::~ModeWatcher()
115 {
116 }
117
118 char ModeWatcher::GetModeChar()
119 {
120         return mode;
121 }
122
123 ModeType ModeWatcher::GetModeType()
124 {
125         return m_type;
126 }
127
128 bool ModeWatcher::BeforeMode(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding, ModeType type)
129 {
130         return true;
131 }
132
133 void ModeWatcher::AfterMode(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter, bool adding, ModeType type)
134 {
135 }
136
137 userrec* ModeParser::SanityChecks(userrec *user,char *dest,chanrec *chan,int status)
138 {
139         userrec *d;
140         if ((!user) || (!dest) || (!chan) || (!*dest))
141         {
142                 return NULL;
143         }
144         d = Find(dest);
145         if (!d)
146         {
147                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
148                 return NULL;
149         }
150         return d;
151 }
152
153 char* ModeParser::Grant(userrec *d,chanrec *chan,int MASK)
154 {
155         if (!chan)
156                 return NULL;
157
158         for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
159         {
160                 if (((ucrec*)(*i))->channel == chan)
161                 {
162                         if (((ucrec*)(*i))->uc_modes & MASK)
163                         {
164                                 return NULL;
165                         }
166                         ((ucrec*)(*i))->uc_modes = ((ucrec*)(*i))->uc_modes | MASK;
167                         switch (MASK)
168                         {
169                                 case UCMODE_OP:
170                                         ((ucrec*)(*i))->channel->AddOppedUser(d);
171                                 break;
172                                 case UCMODE_HOP:
173                                         ((ucrec*)(*i))->channel->AddHalfoppedUser(d);
174                                 break;
175                                 case UCMODE_VOICE:
176                                         ((ucrec*)(*i))->channel->AddVoicedUser(d);
177                                 break;
178                         }
179                         log(DEBUG,"grant: %s %s",((ucrec*)(*i))->channel->name,d->nick);
180                         return d->nick;
181                 }
182         }
183         return NULL;
184 }
185
186 char* ModeParser::Revoke(userrec *d,chanrec *chan,int MASK)
187 {
188         if (!chan)
189                 return NULL;
190
191         for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
192         {
193                 if (((ucrec*)(*i))->channel == chan)
194                 {
195                         if ((((ucrec*)(*i))->uc_modes & MASK) == 0)
196                         {
197                                 return NULL;
198                         }
199                         ((ucrec*)(*i))->uc_modes ^= MASK;
200                         switch (MASK)
201                         {
202                                 case UCMODE_OP:
203                                         ((ucrec*)(*i))->channel->DelOppedUser(d);
204                                 break;
205                                 case UCMODE_HOP:
206                                         ((ucrec*)(*i))->channel->DelHalfoppedUser(d);
207                                 break;
208                                 case UCMODE_VOICE:
209                                         ((ucrec*)(*i))->channel->DelVoicedUser(d);
210                                 break;
211                         }
212                         log(DEBUG,"revoke: %s %s",((ucrec*)(*i))->channel->name,d->nick);
213                         return d->nick;
214                 }
215         }
216         return NULL;
217 }
218
219 char* ModeParser::GiveOps(userrec *user,char *dest,chanrec *chan,int status)
220 {
221         userrec *d = this->SanityChecks(user,dest,chan,status);
222         
223         if (d)
224         {
225                 if (IS_LOCAL(user))
226                 {
227                         int MOD_RESULT = 0;
228                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_OP));
229                         
230                         if (MOD_RESULT == ACR_DENY)
231                                 return NULL;
232                         if (MOD_RESULT == ACR_DEFAULT)
233                         {
234                                 if ((status < STATUS_OP) && (!is_uline(user->server)))
235                                 {
236                                         WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
237                                         return NULL;
238                                 }
239                         }
240                 }
241
242                 return this->Grant(d,chan,UCMODE_OP);
243         }
244         return NULL;
245 }
246
247 char* ModeParser::GiveHops(userrec *user,char *dest,chanrec *chan,int status)
248 {
249         userrec *d = this->SanityChecks(user,dest,chan,status);
250         
251         if (d)
252         {
253                 if (IS_LOCAL(user))
254                 {
255                         int MOD_RESULT = 0;
256                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_HALFOP));
257                 
258                         if (MOD_RESULT == ACR_DENY)
259                                 return NULL;
260                         if (MOD_RESULT == ACR_DEFAULT)
261                         {
262                                 if ((status < STATUS_OP) && (!is_uline(user->server)))
263                                 {
264                                         WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
265                                         return NULL;
266                                 }
267                         }
268                 }
269
270                 return this->Grant(d,chan,UCMODE_HOP);
271         }
272         return NULL;
273 }
274
275 char* ModeParser::GiveVoice(userrec *user,char *dest,chanrec *chan,int status)
276 {
277         userrec *d = this->SanityChecks(user,dest,chan,status);
278         
279         if (d)
280         {
281                 if (IS_LOCAL(user))
282                 {
283                         int MOD_RESULT = 0;
284                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_VOICE));
285                         
286                         if (MOD_RESULT == ACR_DENY)
287                                 return NULL;
288                         if (MOD_RESULT == ACR_DEFAULT)
289                         {
290                                 if ((status < STATUS_HOP) && (!is_uline(user->server)))
291                                 {
292                                         WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
293                                         return NULL;
294                                 }
295                         }
296                 }
297
298                 return this->Grant(d,chan,UCMODE_VOICE);
299         }
300         return NULL;
301 }
302
303 char* ModeParser::TakeOps(userrec *user,char *dest,chanrec *chan,int status)
304 {
305         userrec *d = this->SanityChecks(user,dest,chan,status);
306         
307         if (d)
308         {
309                 if (IS_LOCAL(user))
310                 {
311                         int MOD_RESULT = 0;
312                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEOP));
313                         
314                         if (MOD_RESULT == ACR_DENY)
315                                 return NULL;
316                         if (MOD_RESULT == ACR_DEFAULT)
317                         {
318                                 if ((status < STATUS_OP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
319                                 {
320                                         WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
321                                         return NULL;
322                                 }
323                         }
324                 }
325
326                 return this->Revoke(d,chan,UCMODE_OP);
327         }
328         return NULL;
329 }
330
331 char* ModeParser::TakeHops(userrec *user,char *dest,chanrec *chan,int status)
332 {
333         userrec *d = this->SanityChecks(user,dest,chan,status);
334         
335         if (d)
336         {
337                 if (IS_LOCAL(user))
338                 {
339                         int MOD_RESULT = 0;
340                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEHALFOP));
341                         
342                         if (MOD_RESULT == ACR_DENY)
343                                 return NULL;
344                         if (MOD_RESULT == ACR_DEFAULT)
345                         {
346                                 /* Tweak by Brain suggested by w00t, allow a halfop to dehalfop themselves */
347                                 if ((user != d) && ((status < STATUS_OP) && (!is_uline(user->server))))
348                                 {
349                                         WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
350                                         return NULL;
351                                 }
352                         }
353                 }
354
355                 return this->Revoke(d,chan,UCMODE_HOP);
356         }
357         return NULL;
358 }
359
360 char* ModeParser::TakeVoice(userrec *user,char *dest,chanrec *chan,int status)
361 {
362         userrec *d = this->SanityChecks(user,dest,chan,status);
363
364         if (d)  
365         {
366                 if (IS_LOCAL(user))
367                 {
368                         int MOD_RESULT = 0;
369                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEVOICE));
370                         
371                         if (MOD_RESULT == ACR_DENY)
372                                 return NULL;
373                         if (MOD_RESULT == ACR_DEFAULT)
374                         {
375                                 if ((status < STATUS_HOP) && (!is_uline(user->server)))
376                                 {
377                                         WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
378                                         return NULL;
379                                 }
380                         }
381                 }
382
383                 return this->Revoke(d,chan,UCMODE_VOICE);
384         }
385         return NULL;
386 }
387
388 void ModeParser::Process(char **parameters, int pcnt, userrec *user, bool servermode)
389 {
390         std::string target = parameters[0];
391         ModeType type = MODETYPE_USER;
392         unsigned char mask = 0;
393         chanrec* targetchannel = FindChan(parameters[0]);
394         userrec* targetuser  = Find(parameters[0]);
395
396         log(DEBUG,"ModeParser::Process start");
397
398         if (pcnt > 1)
399         {
400                 if (targetchannel)
401                 {
402                         log(DEBUG,"Target type is CHANNEL");
403                         type = MODETYPE_CHANNEL;
404                         mask = MASK_CHANNEL;
405                 }
406                 else if (targetuser)
407                 {
408                         log(DEBUG,"Target type is USER");
409                         type = MODETYPE_USER;
410                         mask = MASK_USER;
411                 }
412                 else
413                 {
414                         /* No such nick/channel */
415                         log(DEBUG,"Target type is UNKNOWN, bailing");
416                         return;
417                 }
418                 std::string mode_sequence = parameters[1];
419                 std::string parameter = "";
420                 std::ostringstream parameter_list;
421                 std::string output_sequence = "";
422                 bool adding = true, state_change = false;
423                 int handler_id = 0;
424                 int parameter_counter = 2; /* Index of first parameter */
425
426                 for (std::string::const_iterator letter = mode_sequence.begin(); letter != mode_sequence.end(); letter++)
427                 {
428                         unsigned char modechar = *letter;
429
430                         switch (modechar)
431                         {
432
433                                 log(DEBUG,"Iterate mode letter %c",modechar);
434
435                                 /* NB:
436                                  * For + and - mode characters, we don't just stick the character into the output sequence.
437                                  * This is because the user may do something dumb, like: +-+ooo or +oo-+. To prevent this
438                                  * appearing in the output sequence, we store a flag which says there was a state change,
439                                  * which is set on any + or -, however, the + or - that we finish on is only appended to
440                                  * the output stream in the event it is followed by a non "+ or -" character, such as o or v.
441                                  */
442                                 case '+':
443                                         /* The following expression prevents: +o+o nick nick, compressing it to +oo nick nick,
444                                          * however, will allow the + if it is the first item in the sequence, regardless.
445                                          */
446                                         if ((!adding) || (!output_sequence.length()))
447                                                 state_change = true;
448                                         adding = true;
449                                         continue;
450                                 break;
451                                 case '-':
452                                         if ((adding) || (!output_sequence.length()))
453                                                 state_change = true;
454                                         adding = false;
455                                         continue;
456                                 break;
457                                 default:
458
459                                         /**
460                                          * Watch carefully for the sleight of hand trick.
461                                          * 65 is the ascii value of 'A'. We take this from
462                                          * the char we're looking at to get a number between
463                                          * 1 and 127. We then logic-or it to get the hashed
464                                          * position, dependent on wether its a channel or
465                                          * a user mode. This is a little stranger, but a lot
466                                          * faster, than using a map of pairs.
467                                          */
468                                         handler_id = (modechar - 65) | mask;
469
470                                         if (modehandlers[handler_id])
471                                         {
472                                                 bool abort = false;
473
474                                                 log(DEBUG,"Found a ModeHandler* for mode %c",modechar);
475
476                                                 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
477                                                 {
478                                                         log(DEBUG,"Call a ModeWatcher*");
479                                                         if ((*watchers)->BeforeMode(user, targetuser, targetchannel, parameter, adding, type) == MODEACTION_DENY)
480                                                                 abort = true;
481                                                 }
482                                                 if ((modehandlers[handler_id]->GetModeType() == type) && (!abort))
483                                                 {
484                                                         log(DEBUG,"Modetype match, calling handler");
485
486                                                         if (modehandlers[handler_id]->GetNumParams(adding))
487                                                         {
488                                                                 log(DEBUG,"ModeHandler* for this mode says it has parameters. pcnt=%d parameter_counter=%d",pcnt,parameter_counter);
489
490                                                                 if (parameter_counter < pcnt)
491                                                                 {
492                                                                         parameter = parameters[parameter_counter++];
493                                                                 }
494                                                                 else
495                                                                 {
496                                                                         /* No parameter, continue to the next mode */
497                                                                         continue;
498                                                                 }
499                                                         }
500                                                         ModeAction ma = modehandlers[handler_id]->OnModeChange(user, targetuser, targetchannel, parameter, adding);
501
502                                                         if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter == ""))
503                                                         {
504                                                                 /* The handler nuked the parameter and they are supposed to have one.
505                                                                  * We CANT continue now, even if they actually returned MODEACTION_ALLOW,
506                                                                  * so we bail to the next mode character.
507                                                                  */
508                                                                 continue;
509                                                         }
510
511                                                         if (ma == MODEACTION_ALLOW)
512                                                         {
513                                                                 log(DEBUG,"ModeAction was allow");
514
515                                                                 /* We're about to output a valid mode letter - was there previously a pending state-change? */
516                                                                 if (state_change)
517                                                                 {
518                                                                         log(DEBUG,"Appending state change");
519                                                                         output_sequence.append(adding ? "+" : "-");
520                                                                 }
521                                                                 
522                                                                 /* Add the mode letter */
523                                                                 output_sequence.push_back(modechar);
524                                                                 log(DEBUG,"Added mode letter to output sequence, sequence now: '%s'",output_sequence.c_str());
525
526                                                                 /* Is there a valid parameter for this mode? If so add it to the parameter list */
527                                                                 if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter != ""))
528                                                                 {
529                                                                         log(DEBUG,"Added parameter to parameter_list, list now: '%s'",parameter_list.str().c_str());
530                                                                         parameter_list << " " << parameter;
531                                                                 }
532
533                                                                 /* Call all the AfterMode events in the mode watchers for this mode */
534                                                                 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
535                                                                 {
536                                                                         log(DEBUG,"Called a ModeWatcher* after event");
537                                                                         (*watchers)->AfterMode(user, targetuser, targetchannel, parameter, adding, type);
538                                                                 }
539
540                                                                 /* Reset the state change flag */
541                                                                 state_change = false;
542                                                         }
543                                                 }
544                                         }
545                                 break;
546                         }
547                 }
548                 /* Was there at least one valid mode in the sequence? */
549                 if (output_sequence != "")
550                 {
551                         if (servermode)
552                         {
553                                 if (type == MODETYPE_CHANNEL)
554                                 {
555                                         WriteChannelWithServ(Config->ServerName,targetchannel,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
556                                 }
557                         }
558                         else
559                         {
560                                 if (type == MODETYPE_CHANNEL)
561                                 {
562                                         log(DEBUG,"Write output sequence and parameters to channel: %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
563                                         WriteChannel(targetchannel,user,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
564                                         FOREACH_MOD(I_OnMode,OnMode(user, targetchannel, TYPE_CHANNEL, output_sequence + parameter_list.str()));
565                                 }
566                         }
567                 }
568         }
569 }
570
571
572 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
573 {
574         if (!user)
575                 return;
576
577         ServerInstance->ModeGrok->Process(parameters, pcnt, user, false);
578
579         return;
580 }
581
582 void ModeParser::CleanMask(std::string &mask)
583 {
584         std::string::size_type pos_of_pling = mask.find_first_of('!');
585         std::string::size_type pos_of_at = mask.find_first_of('@');
586         std::string::size_type pos_of_dot = mask.find_first_of('.');
587         std::string::size_type pos_of_colon = mask.find_first_of(':'); /* Because ipv6 addresses are colon delimited */
588
589         if ((pos_of_pling == std::string::npos) && (pos_of_at == std::string::npos))
590         {
591                 /* Just a nick, or just a host */
592                 if ((pos_of_dot == std::string::npos) && (pos_of_colon == std::string::npos))
593                 {
594                         /* It has no '.' in it, it must be a nick. */
595                         mask.append("!*@*");
596                 }
597                 else
598                 {
599                         /* Got a dot in it? Has to be a host */
600                         mask = "*!*@" + mask;
601                 }
602         }
603         else if ((pos_of_pling == std::string::npos) && (pos_of_at != std::string::npos))
604         {
605                 /* Has an @ but no !, its a user@host */
606                  mask = "*!" + mask;
607         }
608         else if ((pos_of_pling != std::string::npos) && (pos_of_at == std::string::npos))
609         {
610                 /* Has a ! but no @, it must be a nick!ident */
611                 mask.append("@*");
612         }
613 }
614
615 bool ModeParser::AddMode(ModeHandler* mh, unsigned const char modeletter)
616 {
617         unsigned char mask = 0;
618         unsigned char pos = 0;
619
620         /* Yes, i know, this might let people declare modes like '_' or '^'.
621          * If they do that, thats their problem, and if i ever EVER see an
622          * official InspIRCd developer do that, i'll beat them with a paddle!
623          */
624         if ((modeletter < 'A') || (modeletter > 'z'))
625                 return false;
626
627         mh->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL;
628         pos = (modeletter-65) | mask;
629
630         if (modehandlers[pos])
631                 return false;
632
633         modehandlers[pos] = mh;
634         log(DEBUG,"ModeParser::AddMode: added mode %c",modeletter);
635         return true;
636 }
637
638 ModeParser::ModeParser()
639 {
640         /* Clear mode list */
641         memset(modehandlers, 0, sizeof(modehandlers));
642         memset(modewatchers, 0, sizeof(modewatchers));
643
644         /* Initialise the RFC mode letters */
645         this->AddMode(new ModeChannelSecret, 's');
646         this->AddMode(new ModeChannelPrivate, 'p');
647         this->AddMode(new ModeChannelBan, 'b');
648         this->AddMode(new ModeChannelModerated, 'm');
649 }
650