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