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