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