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