]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/mode.cpp
Avoid throwing a std::out_of_range exception when given a ban without ! or @ in it...
[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 modeletter = mode_sequence.begin(); modeletter != mode_sequence.end(); modeletter++)
427                 {
428                         switch (*modeletter)
429                         {
430
431                                 log(DEBUG,"Iterate mode letter %c",*modeletter);
432
433                                 /* NB:
434                                  * For + and - mode characters, we don't just stick the character into the output sequence.
435                                  * This is because the user may do something dumb, like: +-+ooo or +oo-+. To prevent this
436                                  * appearing in the output sequence, we store a flag which says there was a state change,
437                                  * which is set on any + or -, however, the + or - that we finish on is only appended to
438                                  * the output stream in the event it is followed by a non "+ or -" character, such as o or v.
439                                  */
440                                 case '+':
441                                         /* The following expression prevents: +o+o nick nick, compressing it to +oo nick nick,
442                                          * however, will allow the + if it is the first item in the sequence, regardless.
443                                          */
444                                         if ((!adding) || (!output_sequence.length()))
445                                                 state_change = true;
446                                         adding = true;
447                                         continue;
448                                 break;
449                                 case '-':
450                                         if ((adding) || (!output_sequence.length()))
451                                                 state_change = true;
452                                         adding = false;
453                                         continue;
454                                 break;
455                                 default:
456
457                                         /**
458                                          * Watch carefully for the sleight of hand trick.
459                                          * 65 is the ascii value of 'A'. We take this from
460                                          * the char we're looking at to get a number between
461                                          * 1 and 127. We then logic-or it to get the hashed
462                                          * position, dependent on wether its a channel or
463                                          * a user mode. This is a little stranger, but a lot
464                                          * faster, than using a map of pairs.
465                                          */
466                                         handler_id = (*modeletter - 65) | mask;
467
468                                         if (modehandlers[handler_id])
469                                         {
470                                                 bool abort = false;
471
472                                                 log(DEBUG,"Found a ModeHandler* for mode %c",*modeletter);
473
474                                                 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
475                                                 {
476                                                         log(DEBUG,"Call a ModeWatcher*");
477                                                         if ((*watchers)->BeforeMode(user, targetuser, targetchannel, parameter, adding, type) == MODEACTION_DENY)
478                                                                 abort = true;
479                                                 }
480                                                 if ((modehandlers[handler_id]->GetModeType() == type) && (!abort))
481                                                 {
482                                                         log(DEBUG,"Modetype match, calling handler");
483
484                                                         if (modehandlers[handler_id]->GetNumParams(adding))
485                                                         {
486                                                                 log(DEBUG,"ModeHandler* for this mode says it has parameters. pcnt=%d parameter_counter=%d",pcnt,parameter_counter);
487
488                                                                 if (parameter_counter < pcnt)
489                                                                 {
490                                                                         parameter = parameters[parameter_counter++];
491                                                                 }
492                                                                 else
493                                                                 {
494                                                                         /* No parameter, continue to the next mode */
495                                                                         continue;
496                                                                 }
497                                                         }
498                                                         ModeAction ma = modehandlers[handler_id]->OnModeChange(user, targetuser, targetchannel, parameter, adding);
499                                                         if (ma == MODEACTION_ALLOW)
500                                                         {
501                                                                 log(DEBUG,"ModeAction was allow");
502
503                                                                 /* We're about to output a valid mode letter - was there previously a pending state-change? */
504                                                                 if (state_change)
505                                                                 {
506                                                                         log(DEBUG,"Appending state change");
507                                                                         output_sequence.append(adding ? "+" : "-");
508                                                                 }
509                                                                 
510                                                                 /* Add the mode letter */
511                                                                 output_sequence = output_sequence + *modeletter;
512                                                                 log(DEBUG,"Added mode letter to output sequence, sequence now: '%s'",output_sequence.c_str());
513
514                                                                 /* Is there a valid parameter for this mode? If so add it to the parameter list */
515                                                                 if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter != ""))
516                                                                 {
517                                                                         log(DEBUG,"Added parameter to parameter_list, list now: '%s'",parameter_list.str().c_str());
518                                                                         parameter_list << " " << parameter;
519                                                                 }
520
521                                                                 /* Call all the AfterMode events in the mode watchers for this mode */
522                                                                 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
523                                                                 {
524                                                                         log(DEBUG,"Called a ModeWatcher* after event");
525                                                                         (*watchers)->AfterMode(user, targetuser, targetchannel, parameter, adding, type);
526                                                                 }
527
528                                                                 /* Reset the state change flag */
529                                                                 state_change = false;
530                                                         }
531                                                 }
532                                         }
533                                 break;
534                         }
535                 }
536                 /* Was there at least one valid mode in the sequence? */
537                 if (output_sequence != "")
538                 {
539                         if (servermode)
540                         {
541                                 if (type == MODETYPE_CHANNEL)
542                                 {
543                                         WriteChannelWithServ(Config->ServerName,targetchannel,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
544                                 }
545                         }
546                         else
547                         {
548                                 if (type == MODETYPE_CHANNEL)
549                                 {
550                                         log(DEBUG,"Write output sequence and parameters to channel: %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
551                                         WriteChannel(targetchannel,user,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
552                                         FOREACH_MOD(I_OnMode,OnMode(user, targetchannel, TYPE_CHANNEL, output_sequence + parameter_list.str()));
553                                 }
554                         }
555                 }
556         }
557 }
558
559
560 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
561 {
562         if (!user)
563                 return;
564
565         ServerInstance->ModeGrok->Process(parameters, pcnt, user, false);
566
567         return;
568 }
569
570 void ModeParser::CleanMask(std::string &mask)
571 {
572         std::string::size_type pos_of_pling = mask.find_first_of('!');
573         std::string::size_type pos_of_at = mask.find_first_of('@');
574         std::string::size_type pos_of_dot = mask.find_first_of('.');
575         std::string::size_type pos_of_colon = mask.find_first_of(':'); /* Because ipv6 addresses are colon delimited */
576
577         if ((pos_of_pling == std::string::npos) && (pos_of_at == std::string::npos))
578         {
579                 /* Just a nick, or just a host */
580                 if ((pos_of_dot == std::string::npos) && (pos_of_colon == std::string::npos))
581                 {
582                         /* It has no '.' in it, it must be a nick. */
583                         mask.append("!*@*");
584                 }
585                 else
586                 {
587                         /* Got a dot in it? Has to be a host */
588                         mask = "*!*@" + mask;
589                 }
590         }
591         else if ((pos_of_pling == std::string::npos) && (pos_of_at != std::string::npos))
592         {
593                 /* Has an @ but no !, its a user@host */
594                  mask = "*!" + mask;
595         }
596         else if ((pos_of_pling != std::string::npos) && (pos_of_at == std::string::npos))
597         {
598                 /* Has a ! but no @, it must be a nick!ident */
599                 mask.append("@*");
600         }
601
602         /* Check for dumb stuff like *@*!*
603          * swap the two items over so that at least the n!u@h ordering
604          * is correct even if the elements may not be
605          */
606         if (((pos_of_pling != std::string::npos) && (pos_of_at != std::string::npos)) && (pos_of_pling > pos_of_at))
607         {
608                 mask.replace(pos_of_pling, 1, "@");
609                 mask.replace(pos_of_at, 1, "!");
610         }
611 }
612
613 bool ModeParser::AddMode(ModeHandler* mh, unsigned const char modeletter)
614 {
615         unsigned char mask = 0;
616         unsigned char pos = 0;
617
618         /* Yes, i know, this might let people declare modes like '_' or '^'.
619          * If they do that, thats their problem, and if i ever EVER see an
620          * official InspIRCd developer do that, i'll beat them with a paddle!
621          */
622         if ((modeletter < 'A') || (modeletter > 'z'))
623                 return false;
624
625         mh->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL;
626         pos = (modeletter-65) | mask;
627
628         if (modehandlers[pos])
629                 return false;
630
631         modehandlers[pos] = mh;
632         log(DEBUG,"ModeParser::AddMode: added mode %c",modeletter);
633         return true;
634 }
635
636 ModeParser::ModeParser()
637 {
638         /* Clear mode list */
639         memset(modehandlers, 0, sizeof(modehandlers));
640         memset(modewatchers, 0, sizeof(modewatchers));
641
642         /* Initialise the RFC mode letters */
643         this->AddMode(new ModeChannelSecret, 's');
644         this->AddMode(new ModeChannelPrivate, 'p');
645         this->AddMode(new ModeChannelBan, 'b');
646         this->AddMode(new ModeChannelModerated, 'm');
647 }
648